@@ -2,10 +2,14 @@ import { getCategories, getProduct, getProducts } from "../api/productApi";
22import { initialProductState , productStore , PRODUCT_ACTIONS } from "../stores" ;
33import { router } from "../router" ;
44
5- export const loadProductsAndCategories = async ( queryParams = null ) => {
6- const query = queryParams || router . query || { } ;
7- router . query = { ...query , current : undefined } ;
8- productStore . dispatch ( {
5+ export const loadProductsAndCategories = async ( queryParams = null , ctx = null ) => {
6+ const usingCtx = ctx && ctx . store && ctx . router ;
7+ const activeRouter = usingCtx ? ctx . router : router ;
8+ const activeStore = usingCtx ? ctx . store : productStore ;
9+
10+ const query = queryParams || activeRouter . query || { } ;
11+ activeRouter . query = { ...query , current : undefined } ;
12+ activeStore . dispatch ( {
913 type : PRODUCT_ACTIONS . SETUP ,
1014 payload : {
1115 ...initialProductState ,
@@ -21,10 +25,10 @@ export const loadProductsAndCategories = async (queryParams = null) => {
2125 pagination : { total } ,
2226 } ,
2327 categories ,
24- ] = await Promise . all ( [ getProducts ( router . query ) , getCategories ( ) ] ) ;
28+ ] = await Promise . all ( [ getProducts ( activeRouter . query ) , getCategories ( ) ] ) ;
2529
2630 // 페이지 리셋이면 새로 설정, 아니면 기존에 추가
27- productStore . dispatch ( {
31+ activeStore . dispatch ( {
2832 type : PRODUCT_ACTIONS . SETUP ,
2933 payload : {
3034 products,
@@ -35,7 +39,7 @@ export const loadProductsAndCategories = async (queryParams = null) => {
3539 } ,
3640 } ) ;
3741 } catch ( error ) {
38- productStore . dispatch ( {
42+ activeStore . dispatch ( {
3943 type : PRODUCT_ACTIONS . SET_ERROR ,
4044 payload : error . message ,
4145 } ) ;
@@ -46,27 +50,30 @@ export const loadProductsAndCategories = async (queryParams = null) => {
4650/**
4751 * 상품 목록 로드 (새로고침)
4852 */
49- export const loadProducts = async ( resetList = true ) => {
53+ export const loadProducts = async ( resetList = true , ctx = null ) => {
54+ const usingCtx = ctx && ctx . store && ctx . router ;
55+ const activeRouter = usingCtx ? ctx . router : router ;
56+ const activeStore = usingCtx ? ctx . store : productStore ;
5057 try {
51- productStore . dispatch ( {
58+ activeStore . dispatch ( {
5259 type : PRODUCT_ACTIONS . SETUP ,
5360 payload : { loading : true , status : "pending" , error : null } ,
5461 } ) ;
5562
5663 const {
5764 products,
5865 pagination : { total } ,
59- } = await getProducts ( router . query ) ;
66+ } = await getProducts ( activeRouter . query ) ;
6067 const payload = { products, totalCount : total } ;
6168
6269 // 페이지 리셋이면 새로 설정, 아니면 기존에 추가
6370 if ( resetList ) {
64- productStore . dispatch ( { type : PRODUCT_ACTIONS . SET_PRODUCTS , payload } ) ;
71+ activeStore . dispatch ( { type : PRODUCT_ACTIONS . SET_PRODUCTS , payload } ) ;
6572 return ;
6673 }
67- productStore . dispatch ( { type : PRODUCT_ACTIONS . ADD_PRODUCTS , payload } ) ;
74+ activeStore . dispatch ( { type : PRODUCT_ACTIONS . ADD_PRODUCTS , payload } ) ;
6875 } catch ( error ) {
69- productStore . dispatch ( {
76+ activeStore . dispatch ( {
7077 type : PRODUCT_ACTIONS . SET_ERROR ,
7178 payload : error . message ,
7279 } ) ;
@@ -77,60 +84,68 @@ export const loadProducts = async (resetList = true) => {
7784/**
7885 * 다음 페이지 로드 (무한 스크롤)
7986 */
80- export const loadMoreProducts = async ( ) => {
81- const state = productStore . getState ( ) ;
87+ export const loadMoreProducts = async ( ctx = null ) => {
88+ const usingCtx = ctx && ctx . store && ctx . router ;
89+ const activeRouter = usingCtx ? ctx . router : router ;
90+ const activeStore = usingCtx ? ctx . store : productStore ;
91+ const state = activeStore . getState ( ) ;
8292 const hasMore = state . products . length < state . totalCount ;
8393
8494 if ( ! hasMore || state . loading ) {
8595 return ;
8696 }
8797
88- router . query = { current : Number ( router . query . current ?? 1 ) + 1 } ;
89- await loadProducts ( false ) ;
98+ activeRouter . query = { current : Number ( activeRouter . query . current ?? 1 ) + 1 } ;
99+ await loadProducts ( false , ctx ) ;
90100} ;
91101/**
92102 * 상품 검색
93103 */
94- export const searchProducts = ( search ) => {
95- router . query = { search, current : 1 } ;
104+ export const searchProducts = ( search , ctx = null ) => {
105+ const activeRouter = ctx ?. router || router ;
106+ activeRouter . query = { search, current : 1 } ;
96107} ;
97108
98109/**
99110 * 카테고리 필터 설정
100111 */
101- export const setCategory = ( categoryData ) => {
102- router . query = { ...categoryData , current : 1 } ;
112+ export const setCategory = ( categoryData , ctx = null ) => {
113+ const activeRouter = ctx ?. router || router ;
114+ activeRouter . query = { ...categoryData , current : 1 } ;
103115} ;
104116
105117/**
106118 * 정렬 옵션 변경
107119 */
108- export const setSort = ( sort ) => {
109- router . query = { sort, current : 1 } ;
120+ export const setSort = ( sort , ctx = null ) => {
121+ const activeRouter = ctx ?. router || router ;
122+ activeRouter . query = { sort, current : 1 } ;
110123} ;
111124
112125/**
113126 * 페이지당 상품 수 변경
114127 */
115- export const setLimit = ( limit ) => {
116- router . query = { limit, current : 1 } ;
128+ export const setLimit = ( limit , ctx = null ) => {
129+ const activeRouter = ctx ?. router || router ;
130+ activeRouter . query = { limit, current : 1 } ;
117131} ;
118132
119133/**
120134 * 상품 상세 페이지용 상품 조회 및 관련 상품 로드
121135 */
122- export const loadProductDetailForPage = async ( productId ) => {
136+ export const loadProductDetailForPage = async ( productId , ctx = null ) => {
137+ const activeStore = ctx ?. store || productStore ;
123138 try {
124- const currentProduct = productStore . getState ( ) . currentProduct ;
139+ const currentProduct = activeStore . getState ( ) . currentProduct ;
125140 if ( productId === currentProduct ?. productId ) {
126141 // 관련 상품 로드 (같은 category2 기준)
127142 if ( currentProduct . category2 ) {
128- await loadRelatedProducts ( currentProduct . category2 , productId ) ;
143+ await loadRelatedProducts ( currentProduct . category2 , productId , ctx ) ;
129144 }
130145 return ;
131146 }
132147 // 현재 상품 클리어
133- productStore . dispatch ( {
148+ activeStore . dispatch ( {
134149 type : PRODUCT_ACTIONS . SETUP ,
135150 payload : {
136151 ...initialProductState ,
@@ -143,18 +158,18 @@ export const loadProductDetailForPage = async (productId) => {
143158 const product = await getProduct ( productId ) ;
144159
145160 // 현재 상품 설정
146- productStore . dispatch ( {
161+ activeStore . dispatch ( {
147162 type : PRODUCT_ACTIONS . SET_CURRENT_PRODUCT ,
148163 payload : product ,
149164 } ) ;
150165
151166 // 관련 상품 로드 (같은 category2 기준)
152167 if ( product . category2 ) {
153- await loadRelatedProducts ( product . category2 , productId ) ;
168+ await loadRelatedProducts ( product . category2 , productId , ctx ) ;
154169 }
155170 } catch ( error ) {
156171 console . error ( "상품 상세 페이지 로드 실패:" , error ) ;
157- productStore . dispatch ( {
172+ activeStore . dispatch ( {
158173 type : PRODUCT_ACTIONS . SET_ERROR ,
159174 payload : error . message ,
160175 } ) ;
@@ -165,7 +180,8 @@ export const loadProductDetailForPage = async (productId) => {
165180/**
166181 * 관련 상품 로드 (같은 카테고리의 다른 상품들)
167182 */
168- export const loadRelatedProducts = async ( category2 , excludeProductId ) => {
183+ export const loadRelatedProducts = async ( category2 , excludeProductId , ctx = null ) => {
184+ const activeStore = ctx ?. store || productStore ;
169185 try {
170186 const params = {
171187 category2,
@@ -178,14 +194,14 @@ export const loadRelatedProducts = async (category2, excludeProductId) => {
178194 // 현재 상품 제외
179195 const relatedProducts = response . products . filter ( ( product ) => product . productId !== excludeProductId ) ;
180196
181- productStore . dispatch ( {
197+ activeStore . dispatch ( {
182198 type : PRODUCT_ACTIONS . SET_RELATED_PRODUCTS ,
183199 payload : relatedProducts ,
184200 } ) ;
185201 } catch ( error ) {
186202 console . error ( "관련 상품 로드 실패:" , error ) ;
187203 // 관련 상품 로드 실패는 전체 페이지에 영향주지 않도록 조용히 처리
188- productStore . dispatch ( {
204+ activeStore . dispatch ( {
189205 type : PRODUCT_ACTIONS . SET_RELATED_PRODUCTS ,
190206 payload : [ ] ,
191207 } ) ;
0 commit comments