From f1910544cf6df50bcb175b66b604f5903f6ae3fa Mon Sep 17 00:00:00 2001
From: it-fixcomart <it@fixcomart.co.id>
Date: Sat, 27 Jul 2024 13:34:00 +0700
Subject: <iman> update category management for lob

---
 src/lib/product/components/ProductSearch.jsx | 98 +++++++++++++++++++++-------
 1 file changed, 76 insertions(+), 22 deletions(-)

(limited to 'src/lib/product/components/ProductSearch.jsx')

diff --git a/src/lib/product/components/ProductSearch.jsx b/src/lib/product/components/ProductSearch.jsx
index 735ef58e..2da84d63 100644
--- a/src/lib/product/components/ProductSearch.jsx
+++ b/src/lib/product/components/ProductSearch.jsx
@@ -37,6 +37,7 @@ const ProductSearch = ({
   brand = null,
 }) => {
   const router = useRouter();
+   
   const { page = 1 } = query;
   const [q, setQ] = useState(query?.q || '*');
   const [search, setSearch] = useState(query?.q || '*');
@@ -45,18 +46,35 @@ const ProductSearch = ({
   const [finalQuery, setFinalQuery] = useState({});
   const [queryFinal, setQueryFinal] = useState({});
   const [dataCategoriesProduct, setDataCategoriesProduct] = useState([])
+  const [dataCategoriesLob, setDataCategoriesLob] = useState([])
   const categoryId = getIdFromSlug(prefixUrl)
+  console.log("categoryId",categoryId ) 
+  const [data, setData] = useState([])
+  const [dataLob, setDataLob] = useState([]);
   if (defaultBrand) query.brand = defaultBrand.toLowerCase();
+  const dataIdCategories = []
   useEffect(() => {
-    const loadProduct = async () => {
-      const getCategoriesId = await odooApi('GET', `/api/v1/category/numFound?parent_id=${categoryId}`);
-      if (getCategoriesId) {
-        setDataCategoriesProduct(getCategoriesId);
-      }
-    };
-    loadProduct();
+    if(prefixUrl.includes('category')){
+      const loadProduct = async () => {
+        const getCategoriesId = await odooApi('GET', `/api/v1/category/numFound?parent_id=${categoryId}`);
+        if (getCategoriesId) {
+          setDataCategoriesProduct(getCategoriesId);
+        }
+      };
+      loadProduct();
+    }else if(prefixUrl.includes('lob')){
+      const loadProduct = async () => {
+        const lobData = await odooApi('GET', `/api/v1/lob_homepage?lob_id=${categoryId}`);
+        console.log("lobData",lobData ) 
+        if (lobData) {
+          setDataLob(lobData);
+        }
+      };
+      loadProduct();
+      console.log("dataLob",dataLob ) 
+    }
   }, [categoryId]);
-
+  
   const collectIds = (category) => {
     const ids = [];
     function recurse(cat) {
@@ -70,26 +88,61 @@ const ProductSearch = ({
     recurse(category);
     return ids;
   };
-
+  
   useEffect(() => {
-    const ids = collectIds(dataCategoriesProduct);
-    const newQuery = {
-      fq: `category_id_ids:(${ids.join(' OR ')})`,
-      page: 1,
-      brand : router.query.brand? router.query.brand : '',
-    };
-    console.log("newQuery",newQuery)
-    setFinalQuery(newQuery);
-  }, [dataCategoriesProduct, prefixUrl, prefixUrl, finalQuery, q, limit, orderBy]);
+    if(prefixUrl.includes('category')){
+      const ids = collectIds(dataCategoriesProduct);
+      const newQuery = {
+        fq: `category_id_ids:(${ids.join(' OR ')})`,
+        page,
+        brand : router.query.brand? router.query.brand : '',
+        category : router.query.category? router.query.category : '',
+      };
+      setFinalQuery(newQuery);
+    } else if (prefixUrl.includes('lob')){
+      console.log("prefixUrl",prefixUrl) 
+      const fetchCategoryData = async () => {
+        if (dataLob[0]?.categoryIds) {
+          console.log("dataLob[0]?.categoryIds",dataLob[0]?.categoryIds) 
+          for (const cate of dataLob[0].categoryIds) {
+            console.log("cate",cate) 
+            const getCategoriesId = await odooApi('GET', `/api/v1/category/numFound?parent_id=${cate.id}`);
+            console.log("getCategoriesId",getCategoriesId) 
+            if (getCategoriesId) {
+              const ids = collectIds(getCategoriesId);
+              console.log("ids",ids) 
+              dataIdCategories.push(ids)
+              console.log("dataIdCategories",dataIdCategories) 
+            }
+          }
+          
+          console.log("dataIdCategories2",dataIdCategories) 
+          // setData(dataIdCategories)
+          // console.log("data",data) 
+          const mergedArray = dataIdCategories.flat();
+          console.log("mergedArray",mergedArray) 
+          const newQuery = {
+            fq: `category_id_ids:(${mergedArray.join(' OR ')})`,
+            page,
+            brand : router.query.brand? router.query.brand : '',
+            category : router.query.category? router.query.category : '',
+          };
+          console.log("newQuery",newQuery) 
+          setFinalQuery(newQuery);
+          console.log("final query",finalQuery)
+        }
+      };
+      fetchCategoryData();
+    }
+  }, [dataCategoriesProduct, dataLob]);
 
   useEffect(() => {
-    if (prefixUrl.includes('category')) {
+    if (prefixUrl.includes('category') || prefixUrl.includes('lob')) {
       setQueryFinal({ ...finalQuery, q, limit, orderBy });
-      // setQueryFinal({ ...query, q, limit, orderBy });
     } else {
       setQueryFinal({ ...query, q, limit, orderBy });
     }
-  }, [prefixUrl, finalQuery, q, limit, orderBy]);
+  }, [prefixUrl,dataCategoriesProduct, query, finalQuery]);
 
   const { productSearch } = useProductSearch({
     query: queryFinal,
@@ -179,7 +232,8 @@ const ProductSearch = ({
     loadCategories()
   }, [])
 
-  console.log("queryFinal", queryFinal)
+  // console.log("query asal ", query)
+  // console.log("queryFinal", queryFinal)
 
 
   const brands = [];
-- 
cgit v1.2.3

