diff options
| author | it-fixcomart <it@fixcomart.co.id> | 2024-08-22 13:10:29 +0700 |
|---|---|---|
| committer | it-fixcomart <it@fixcomart.co.id> | 2024-08-22 13:10:29 +0700 |
| commit | 5e2a8d315fa0bc58a18d4df5a995e402388995c9 (patch) | |
| tree | 09602b444795a4c85a5e5a422662939333466ced /src/lib/category/components/PopularBrand.jsx | |
| parent | 24cc59486efbe9caebfb10228283ee515afb5934 (diff) | |
<iman> update category dropdown
Diffstat (limited to 'src/lib/category/components/PopularBrand.jsx')
| -rw-r--r-- | src/lib/category/components/PopularBrand.jsx | 45 |
1 files changed, 29 insertions, 16 deletions
diff --git a/src/lib/category/components/PopularBrand.jsx b/src/lib/category/components/PopularBrand.jsx index f0b12e2c..4777fded 100644 --- a/src/lib/category/components/PopularBrand.jsx +++ b/src/lib/category/components/PopularBrand.jsx @@ -18,28 +18,41 @@ const PopularBrand = ({ category }) => { const fetchTopBrands = async () => { try { const items = await fetchPopulerProductSolr(`category_id_ids:(${category?.categoryDataIds?.join(' OR ')})`); - // console.log("id",items) - // Fungsi untuk deduplikasi dan mengambil 12 nama brand teratas const getTop12UniqueBrands = (prod) => { - const brandSet = new Set(); - const topBrands = []; - + const brandMap = new Map(); + for (const product of prod) { - if (!brandSet.has(product.manufacture_name)) { - brandSet.add(product.manufacture_name); - topBrands.push({ name: product.manufacture_name, id: product.manufacture_id }); - }else{ + const { manufacture_name, manufacture_id, qty_sold } = product; + + if (brandMap.has(manufacture_name)) { + // Update the existing brand's qty_sold + brandMap.set(manufacture_name, { + name: manufacture_name, + id: manufacture_id, + qty_sold: brandMap.get(manufacture_name).qty_sold + qty_sold + }); + } else { + // Add a new brand to the map + brandMap.set(manufacture_name, { + name: manufacture_name, + id: manufacture_id, + qty_sold + }); } - if (topBrands.length === 18) break; } - return topBrands; - } - - // Menggunakan hasil pencarian produk + + // Convert the map to an array and sort by qty_sold in descending order + const sortedBrands = Array.from(brandMap.values()).sort((a, b) => b.qty_sold - a.qty_sold); + + // Return the top 12 brands + return sortedBrands.slice(0, 18); + }; + + // Using the fetched products const products = items; const top12UniqueBrands = getTop12UniqueBrands(products); - - // console.log('top12UniqueBrands', top12UniqueBrands); + + // Set the top 12 brands to the state setTopBrands(top12UniqueBrands); } catch (error) { console.error("Error fetching data from Solr", error); |
