From 802fd0e3b06a9faf43eff46a3999b004ddb6fcdb Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Wed, 1 Feb 2023 11:12:24 +0700 Subject: no message --- src/pages/index.js | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/pages/index.js b/src/pages/index.js index fd737190..6b6344e6 100644 --- a/src/pages/index.js +++ b/src/pages/index.js @@ -27,7 +27,7 @@ export default function Home({ heroBanners }) { const [manufactures, setManufactures] = useState(null); const [popularProducts, setPopularProducts] = useState(null); const [categoryProductIds, setCategoryProductIds] = useState(null); - const [categoryProducts, setCategoryProducts] = useState(null); + const [categoryProducts, setCategoryProducts] = useState([]); useEffect(() => { const getManufactures = async () => { @@ -51,19 +51,25 @@ export default function Home({ heroBanners }) { } } getCategoryProductIds(); + }, [ categoryProductIds ]); + useEffect(() => { const getCategoryProducts = async () => { - if (categoryProductIds) { - let collectionCategoryProducts = []; - for (const id in categoryProductIds) { - const dataCategoryProducts = await apiOdoo('GET', `/api/v1/categories_homepage?id=${id}`); - collectionCategoryProducts.push(...dataCategoryProducts); - setCategoryProducts(collectionCategoryProducts); + const currentCategoryId = categoryProductIds ? categoryProductIds[categoryProducts.length] : false; + if (currentCategoryId) { + const isAdded = categoryProducts.findIndex((categoryProduct) => categoryProduct.id == currentCategoryId); + console.log(isAdded); + if (isAdded < 0) { + const dataCategoryProducts = await apiOdoo('GET', `/api/v1/categories_homepage?id=${currentCategoryId}`); + setCategoryProducts((categoryProducts) => ([ + ...categoryProducts, + ...dataCategoryProducts + ])); } } } getCategoryProducts(); - }, [ categoryProductIds ]); + }, [ categoryProducts, categoryProductIds ]); return ( <> -- cgit v1.2.3