diff options
| author | Rafi Zadanly <zadanlyr@gmail.com> | 2023-02-01 11:12:24 +0700 |
|---|---|---|
| committer | Rafi Zadanly <zadanlyr@gmail.com> | 2023-02-01 11:12:24 +0700 |
| commit | 802fd0e3b06a9faf43eff46a3999b004ddb6fcdb (patch) | |
| tree | 09041238dbff563476eceb043203055befcae88a /src/pages | |
| parent | 432ba9f520120e43b0c580627692576d3ef0486f (diff) | |
no message
Diffstat (limited to 'src/pages')
| -rw-r--r-- | src/pages/index.js | 22 |
1 files changed, 14 insertions, 8 deletions
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 ( <> |
