From 5e2a8d315fa0bc58a18d4df5a995e402388995c9 Mon Sep 17 00:00:00 2001
From: it-fixcomart <it@fixcomart.co.id>
Date: Thu, 22 Aug 2024 13:10:29 +0700
Subject: <iman> update category dropdown

---
 src/lib/category/components/PopularBrand.jsx | 45 ++++++++++++++++++----------
 1 file changed, 29 insertions(+), 16 deletions(-)

(limited to 'src/lib/category/components/PopularBrand.jsx')

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);
-- 
cgit v1.2.3

