From 2c2885f57e9eb15e8c7d00c3a543a71c1d6ba83e Mon Sep 17 00:00:00 2001
From: it-fixcomart <it@fixcomart.co.id>
Date: Thu, 25 Jul 2024 14:04:43 +0700
Subject: <iman> update search handle for category

---
 src/lib/product/components/ProductSearch.jsx | 83 ++++++++++++++++++----------
 1 file changed, 53 insertions(+), 30 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 b5c1e87d..413f4fb2 100644
--- a/src/lib/product/components/ProductSearch.jsx
+++ b/src/lib/product/components/ProductSearch.jsx
@@ -42,9 +42,56 @@ const ProductSearch = ({
   const [search, setSearch] = useState(query?.q || '*');
   const [limit, setLimit] = useState(query?.limit || 30);
   const [orderBy, setOrderBy] = useState(router.query?.orderBy || 'popular');
+  const [finalQuery, setFinalQuery] = useState({});
+  const [queryFinal, setQueryFinal] = useState({});
+  const [dataCategoriesProduct, setDataCategoriesProduct] = useState([])
+  const categoryId = getIdFromSlug(prefixUrl)
   if (defaultBrand) query.brand = defaultBrand.toLowerCase();
+  useEffect(() => {
+    const loadProduct = async () => {
+      const getCategoriesId = await odooApi('GET', `/api/v1/category/numFound?parent_id=${categoryId}`);
+      if (getCategoriesId) {
+        setDataCategoriesProduct(getCategoriesId);
+      }
+    };
+    loadProduct();
+  }, [categoryId]);
+
+  const collectIds = (category) => {
+    const ids = [];
+    function recurse(cat) {
+      if (cat && cat.id) {
+        ids.push(cat.id);
+      }
+      if (Array.isArray(cat.children)) {
+        cat.children.forEach(recurse);
+      }
+    }
+    recurse(category);
+    return ids;
+  };
+
+  useEffect(() => {
+    const ids = collectIds(dataCategoriesProduct);
+    const newQuery = {
+      fq: `category_id_i:(${ids.join(' OR ')})`,
+      page: 1,
+      brand : router.query.brand? router.query.brand : '',
+    };
+    setFinalQuery(newQuery);
+  }, [dataCategoriesProduct]);
+
+  useEffect(() => {
+    if (prefixUrl.includes('category')) {
+      setQueryFinal({ ...finalQuery, q, limit, orderBy });
+      // setQueryFinal({ ...query, q, limit, orderBy });
+    } else {
+      setQueryFinal({ ...query, q, limit, orderBy });
+    }
+  }, [prefixUrl, finalQuery, q, limit, orderBy]);
+
   const { productSearch } = useProductSearch({
-    query: { ...query, q, limit, orderBy },
+    query: queryFinal,
     operation: 'AND',
   });
   const [products, setProducts] = useState(null);
@@ -73,12 +120,7 @@ const ProductSearch = ({
   const productRows = limit;
   const productFound = productSearch.data?.response.numFound;
   const [dataCategories, setDataCategories] = useState([])
-  const [dataCategoriesProduct, setDataCategoriesProduct] = useState([])
-  const dataId = []
-  // console.log("dataCategories",dataCategories)
-
-  const categoryId = getIdFromSlug(prefixUrl)
-
+  
   useEffect(() => {
     if (productFound == 0 && query.q && !spellings) {
       searchSpellApi({ query: query.q }).then((response) => {
@@ -134,31 +176,10 @@ const ProductSearch = ({
      }
     }
     loadCategories()
-
-    const loadProduct = async () => {
-      const getCategoriesId = await odooApi('GET', `/api/v1/category/numFound?parent_id=${categoryId}`)
-      console.log("getCategoriesId",getCategoriesId)
-     if(getCategoriesId){
-      setDataCategoriesProduct(getCategoriesId)
-     }
-    }
-    loadProduct()
   }, [])
-  collectIds(dataCategoriesProduct);
 
-// Fungsi rekursif untuk menambahkan ID dari kategori dan subkategori ke array dataId
-function collectIds(category) {
-  if (category && category.id) {
-    dataId.push(category.id);
-  }
-  if (Array.isArray(category.children)) {
-    for (const child of category.children) {
-      collectIds(child);
-    }
-  }
-}
+  
 
-console.log("dataID",dataId)
 
   const brands = [];
   for (
@@ -304,7 +325,9 @@ console.log("dataID",dataId)
   };
 
   const isNotReadyStockPage = router.asPath !== '/shop/search?orderBy=stock';
-  console.log("query?.category?.split(',')",query.fq)
+  
+  
+
   return (
     <>
       <MobileView>
-- 
cgit v1.2.3

