@@ -5,36 +5,64 @@ import { useState } from "../../core/state/useState";
55import { ProductFilter } from "../../domains/product/components/ProductList/ProductFilter" ;
66import { ProductList } from "../../domains/product/components/ProductList/ProductList" ;
77import { Product } from "../../types" ;
8+ import { on } from "../../core/on" ;
89
910export const ProductListPage = component ( ( ) => {
1011 const $products = useState < Product [ ] > ( [ ] ) ;
1112 const $isLoading = useState ( true ) ;
1213 const $itemsPerPage = useState ( 20 ) ;
14+ const $error = useState < string | null > ( null ) ;
15+
16+ const fetchProducts = async ( ) => {
17+ try {
18+ $isLoading . set ( true ) ;
19+ $error . set ( null ) ;
20+
21+ // throw new Error("테스트 에러입니다!");
22+
23+ const response = await getProducts ( {
24+ page : 1 ,
25+ limit : $itemsPerPage . get ( ) ,
26+ sort : "price_asc" ,
27+ } ) ;
28+
29+ $products . set ( response . products ) ;
30+ $isLoading . set ( false ) ;
31+ } catch ( error ) {
32+ console . log ( "상품 로딩 실패: " , error ) ;
33+
34+ $error . set ( error instanceof Error ? error . message : "상품을 불러오는데 실패했습니다." ) ;
35+ $isLoading . set ( false ) ;
36+ }
37+ } ;
1338
1439 useEffect ( ( ) => {
15- ( async ( ) => {
16- try {
17- const response = await getProducts ( {
18- page : 1 ,
19- limit : $itemsPerPage . get ( ) ,
20- sort : "price_asc" ,
21- } ) ;
22-
23- $products . set ( response . products ) ;
24- $isLoading . set ( false ) ;
25- } catch ( error ) {
26- console . log ( "상품 로딩 실패: " , error ) ;
27- $isLoading . set ( false ) ;
28- }
29- } ) ( ) ;
40+ fetchProducts ( ) ;
3041 } , [ $itemsPerPage ] ) ;
3142
3243 return html `
33- < main class ="max-w-md mx-auto px-4 py-4 ">
34- <!-- 검색 및 필터 -->
35- ${ ProductFilter ( { $itemsPerPage } ) }
36- <!-- 상품 목록 -->
37- ${ ProductList ( { $products, $isLoading, $itemsPerPage } ) }
44+ < main class ="h-full max-w-md mx-auto px-4 py-4 ">
45+ ${ $error . ref ( ( error ) => {
46+ if ( error ) {
47+ return html ` < div class ="h-full w-full flex flex-col gap-4 items-center justify-center text-center font-bold ">
48+ < p class ="font-medium "> ${ $error } </ p >
49+ < button
50+ class ="bg-blue-500 p-3 rounded-md flex-shrink-0 text-white hover:text-gray-200 "
51+ ${ on ( "click" , ( ) => {
52+ fetchProducts ( ) ;
53+ } ) }
54+ >
55+ 다시 시도
56+ </ button >
57+ </ div > ` ;
58+ }
59+ return html `
60+ <!-- 검색 및 필터 -->
61+ ${ ProductFilter ( { $itemsPerPage } ) }
62+ <!-- 상품 목록 -->
63+ ${ ProductList ( { $products, $isLoading, $itemsPerPage } ) }
64+ ` ;
65+ } ) }
3866 </ main >
3967 ` ;
4068} ) ;
0 commit comments