From e84bffbab1168ef4bee3e81861e7fbffce7d59ee Mon Sep 17 00:00:00 2001 From: "hyunwoo93.cha" Date: Fri, 16 Feb 2024 22:23:22 +0900 Subject: [PATCH] =?UTF-8?q?[mainActions,=20mainReducer]=20=EB=B0=B1?= =?UTF-8?q?=EA=B7=B8=EB=9D=BC=EC=9A=B4=EB=93=9C=20=EB=8D=B0=EC=9D=B4?= =?UTF-8?q?=ED=84=B0=20=ED=8E=98=EC=B9=AD=20=EB=A1=9C=EC=A7=81=20=EC=A0=81?= =?UTF-8?q?=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/actions/mainActions.js | 95 ++++++++++++------- .../src/reducers/mainReducer.js | 29 +++++- 2 files changed, 85 insertions(+), 39 deletions(-) diff --git a/com.twin.app.shoptime/src/actions/mainActions.js b/com.twin.app.shoptime/src/actions/mainActions.js index 8eebe349..b2d063d4 100644 --- a/com.twin.app.shoptime/src/actions/mainActions.js +++ b/com.twin.app.shoptime/src/actions/mainActions.js @@ -1,10 +1,8 @@ -import { URLS } from '../api/apiConfig'; -import { TAxios } from '../api/TAxios'; -import { CATEGORY_DATA_MAX_RESULTS_LIMIT } from '../utils/Config'; -import { types } from './actionTypes'; - -// 서브카테고리 조회 IF-LGSP-051 -let getSubCategoryKey = null; +import { URLS } from "../api/apiConfig"; +import { TAxios } from "../api/TAxios"; +import { CATEGORY_DATA_MAX_RESULTS_LIMIT } from "../utils/Config"; +import * as HelperMethods from "../utils/helperMethods"; +import { types } from "./actionTypes"; export const getMainCategoryDetail = (props) => (dispatch, getState) => { const { patnrId, prdtId } = props; @@ -32,37 +30,66 @@ export const getMainCategoryDetail = (props) => (dispatch, getState) => { onFail ); }; -export const getSubCategory = (params, key) => (dispatch, getState) => { - const { lgCatCd, patnrIdList, tabType, pageSize, pageNo, filterType } = - params; - const maxResults = CATEGORY_DATA_MAX_RESULTS_LIMIT; - let currentKey = key; - const onSuccess = (response) => { - console.log("getSubCategory onSuccess ", response.data); +// 서브카테고리 조회 IF-LGSP-051 +let getSubCategoryKey = null; +export const getSubCategory = + (params, pageNo = 1, update = true, key) => + (dispatch, getState) => { + const { lgCatCd, patnrIdList, tabType, filterType } = params; + let pageSize = params.pageSize || CATEGORY_DATA_MAX_RESULTS_LIMIT; - dispatch({ - type: types.GET_SUB_CATEGORY, - payload: response.data.data, - }); + let currentKey = key; + const onSuccess = (response) => { + console.log("getSubCategory onSuccess ", response.data); + + if (pageNo === 1) { + getSubCategoryKey = new Date(); + currentKey = getSubCategoryKey; + + dispatch({ + type: types.GET_SUB_CATEGORY, + payload: response.data.data, + }); + } else if (getSubCategoryKey === currentKey) { + dispatch({ + type: types.GET_SUB_CATEGORY, + payload: response.data.data, + append: true, + }); + } + + const totalInfo = + response.data.data.categoryShowInfos || + response.data.data.categoryItemInfos; + + const totalPage = Math.ceil(totalInfo[0].total / pageSize); + + if (getSubCategoryKey === currentKey && pageNo < totalPage && update) { + // TODO: 지연 시간 주는 게 아니라, 데이터 받아오는 양을 늘려야 함, 로직 수정 필요 + + HelperMethods.wait(500).then(() => { + dispatch(getSubCategory(params, pageNo + 1, true, currentKey)); + }); + } + }; + + const onFail = (error) => { + console.error("getSubCategory onFail", error); + }; + + TAxios( + dispatch, + getState, + "get", + URLS.GET_SUB_CATEGORY, + { lgCatCd, patnrIdList, pageSize, pageNo, tabType, filterType }, + {}, + onSuccess, + onFail + ); }; - const onFail = (error) => { - console.error("getSubCategory onFail", error); - }; - - TAxios( - dispatch, - getState, - "get", - URLS.GET_SUB_CATEGORY, - { lgCatCd, patnrIdList, tabType, pageSize, pageNo, filterType }, - {}, - onSuccess, - onFail - ); -}; - // TOP20 영상 목록 조회 IF-LGSP-069 export const getTop20Show = () => (dispatch, getState) => { const onSuccess = (response) => { diff --git a/com.twin.app.shoptime/src/reducers/mainReducer.js b/com.twin.app.shoptime/src/reducers/mainReducer.js index 7eb8e74e..f4796d4a 100644 --- a/com.twin.app.shoptime/src/reducers/mainReducer.js +++ b/com.twin.app.shoptime/src/reducers/mainReducer.js @@ -8,11 +8,30 @@ const initialState = { export const mainReducer = (state = initialState, action) => { switch (action.type) { - case types.GET_SUB_CATEGORY: - return { - ...state, - subCategoryData: action.payload, - }; + case types.GET_SUB_CATEGORY: { + const dataType = action.payload.categoryShowInfos + ? "categoryShowInfos" + : "categoryItemInfos"; + const newResults = action.payload[dataType]; + + if (action.append) { + const existingData = state.subCategoryData[dataType] || []; + const updatedData = existingData.concat(newResults); + + return { + ...state, + subCategoryData: { + ...state.subCategoryData, + [dataType]: updatedData, + }, + }; + } else { + return { + ...state, + subCategoryData: action.payload, + }; + } + } case types.GET_TOP_20_SHOW: return {