From 2e71abba4ca5b83cc1fec229d4d85961c4b56d71 Mon Sep 17 00:00:00 2001
From: it-fixcomart <it@fixcomart.co.id>
Date: Thu, 25 Jul 2024 08:27:05 +0700
Subject: <iman> update category management

---
 src/lib/home/components/CategoryDynamic.jsx | 50 ++++++++++++++++++++---------
 1 file changed, 35 insertions(+), 15 deletions(-)

(limited to 'src/lib/home/components/CategoryDynamic.jsx')

diff --git a/src/lib/home/components/CategoryDynamic.jsx b/src/lib/home/components/CategoryDynamic.jsx
index fa1df286..bbba2ceb 100644
--- a/src/lib/home/components/CategoryDynamic.jsx
+++ b/src/lib/home/components/CategoryDynamic.jsx
@@ -2,38 +2,58 @@ import React, { useEffect, useState } from 'react';
 import useCategoryManagement from '../hooks/useCategoryManagement';
 import NextImage from 'next/image';
 import Link from "next/link";
-import router from 'next/router';
 import { createSlug } from '@/core/utils/slug';
+import odooApi from '@/core/api/odooApi';
 
 const CategoryDynamic = () => {
   const { categoryManagement } = useCategoryManagement();
+  const [categoryData, setCategoryData] = useState({});
+  const [subCategoryData, setSubCategoryData] = useState({});
+  
+  useEffect(() => {
+    const fetchCategoryData = async () => {
+      if (categoryManagement && categoryManagement.data) {
+        const updatedCategoryData = {};
+        const updatedSubCategoryData = {};
+        
+        for (const category of categoryManagement.data) {
+          // Calculate level 1 products
+          const countLevel1 = await odooApi('GET', `/api/v1/category/numFound?parent_id=${category.categoryIdI}`);
+          // console.log("countLevel1.child",countLevel1)
+          
+          updatedCategoryData[category.categoryIdI] = countLevel1.numFound;
+          
+          
+          // Calculate level 2 products for each sub-category
+          for (const subCategory of countLevel1.children) {
+            updatedSubCategoryData[subCategory.id] = subCategory.numFound;
+          }
+        }
 
-  const calculateLevel3Products = (category) => {
-    return category.childFrontendIdI.reduce((total, child) => total + (child.numFound || 0), 0);
-  };
+        setCategoryData(updatedCategoryData);
+        setSubCategoryData(updatedSubCategoryData);
+      }
+    };
 
-  const calculateLevel2Products = (category) => {
-    return category.categories.reduce((total, subCategory) => {
-      const level3Products = calculateLevel3Products(subCategory);
-      return total + (subCategory.numFound || 0) + level3Products;
-    }, 0);
-  };
+    fetchCategoryData();
+  }, [categoryManagement, categoryData]);
+ 
 
   return (
     <div>
       {categoryManagement && categoryManagement.data?.map((category) => {
-        const countLevel2 = calculateLevel2Products(category);
-
+        const countLevel1 = categoryData[category.categoryIdI] || 0;
+        
         return (
           <div key={category.id}>
             <div className='bagian-judul flex flex-row justify-start items-center gap-3 mb-4 mt-4'>
               <div className='font-semibold sm:text-h-lg mr-2'>{category.name}</div>
-              <p className='text-gray_r-10 text-sm'>{countLevel2} Produk tersedia</p>
+              <p className='text-gray_r-10 text-sm'>{countLevel1} Produk tersedia</p>
               <Link href={createSlug('/shop/category/', category?.name, category?.categoryIdI)} className="!text-red-500 font-semibold">Lihat Semua</Link>
             </div>
             <div className='grid grid-cols-3 gap-2'>
               {category.categories.map((subCategory) => {
-                const countLevel3 = calculateLevel3Products(subCategory);
+                const countLevel2 = subCategoryData[subCategory.idLevel2] || 0;
 
                 return (
                   <div key={subCategory.id} className='border rounded justify-start items-start'>
@@ -48,7 +68,7 @@ const CategoryDynamic = () => {
                         />
                         <div className='bagian-judul flex flex-col justify-center items-start gap-2 ml-2'>
                           <div className='font-semibold text-lg mr-2'>{subCategory.name}</div>
-                          <p className='text-gray_r-10 text-sm'>{(subCategory.numFound || 0) + countLevel3} Produk</p>
+                          <p className='text-gray_r-10 text-sm'>{countLevel2} Produk tersedia</p>
                           <Link href={createSlug('/shop/category/', subCategory?.name, subCategory?.idLevel2)} className="!text-red-500 font-semibold">Lihat Semua</Link>
                         </div>
                       </div>
-- 
cgit v1.2.3

