From 807931ee9bcb063157ceb8368e5ee0941450c6ca Mon Sep 17 00:00:00 2001
From: it-fixcomart <it@fixcomart.co.id>
Date: Tue, 6 Aug 2024 14:32:41 +0700
Subject: <iman> add quotation pop up

---
 .../components/elements/Navbar/NavbarDesktop.jsx   | 58 ++++++++++++++++------
 1 file changed, 43 insertions(+), 15 deletions(-)

(limited to 'src/core/components/elements/Navbar/NavbarDesktop.jsx')

diff --git a/src/core/components/elements/Navbar/NavbarDesktop.jsx b/src/core/components/elements/Navbar/NavbarDesktop.jsx
index 7d9e4264..49c4cd6e 100644
--- a/src/core/components/elements/Navbar/NavbarDesktop.jsx
+++ b/src/core/components/elements/Navbar/NavbarDesktop.jsx
@@ -5,7 +5,9 @@ import { createSlug } from '@/core/utils/slug';
 import whatsappUrl from '@/core/utils/whatsappUrl';
 import IndoteknikLogo from '@/images/logo.png';
 import Cardheader from '@/lib/cart/components/Cartheader';
+import Quotationheader from '@/lib/quotation/components/QuotationHeader'
 import Category from '@/lib/category/components/Category';
+import { useProductQuotationContext } from '@/contexts/ProductQuotationContext'
 import {
   ChevronDownIcon,
   DocumentCheckIcon,
@@ -28,6 +30,7 @@ import {
   useDisclosure,
 } from '@chakra-ui/react';
 import style from "./style/NavbarDesktop.module.css";
+import useTransactions from '@/lib/transaction/hooks/useTransactions';
 
 const Search = dynamic(() => import('./Search'), { ssr: false });
 const TopBanner = dynamic(() => import('./TopBanner'), { ssr: false });
@@ -37,6 +40,8 @@ const NavbarDesktop = () => {
   const auth = useAuth();
 
   const [cartCount, setCartCount] = useState(0);
+  const [quotationCount, setQuotationCount] = useState(0);
+  // const { setProductQuotation, refreshQuotation, setRefreshQuotation, isLoading, setIsloading } = useProductQuotationContext()
 
   const [templateWA, setTemplateWA] = useState(null);
   const [payloadWA, setPayloadWa] = useState(null);
@@ -47,6 +52,16 @@ const NavbarDesktop = () => {
   const { product } = useProductContext();
   const { isOpen, onOpen, onClose } = useDisclosure();
 
+  const query = {
+    context: 'quotation',
+    site:
+      (auth?.webRole === null && auth?.site ? auth.site : null),
+  };
+  
+  const { transactions } = useTransactions({query});
+  const pendingTransactions = transactions?.data?.saleOrders.filter(transaction => transaction.status === 'draft');
+  
+  
   useEffect(() => {
     if (router.pathname === '/shop/product/[slug]') {
       setPayloadWa({
@@ -55,11 +70,11 @@ const NavbarDesktop = () => {
         url: createSlug('/shop/product/', product?.name, product?.id, true),
       });
       setTemplateWA('product');
-
+      
       setUrlPath(router.asPath);
     }
   }, [product, router]);
-
+  
   useEffect(() => {
     const handleCartChange = () => {
       const cart = async () => {
@@ -69,14 +84,36 @@ const NavbarDesktop = () => {
       cart();
     };
     handleCartChange();
-
+    
     window.addEventListener('localStorageChange', handleCartChange);
-
+    
     return () => {
       window.removeEventListener('localStorageChange', handleCartChange);
     };
   }, []);
+  
+  // useEffect(() => {
+  //   setProductQuotation(pendingTransactions)
+  // }, [transactions, ])
+  // console.log("Pending Transactions", pendingTransactions); 
+  // console.log("pendingTransactions.length", pendingTransactions.length); 
+
+  useEffect(() => {
+    const handleQuotationChange = () => {
+      const quotation = async () => {
+        setQuotationCount(pendingTransactions?.length);
+      };
+      quotation();
+    };
+    handleQuotationChange();
 
+    window.addEventListener('localStorageChange', handleQuotationChange);
+
+    return () => {
+      window.removeEventListener('localStorageChange', handleQuotationChange);
+    };
+  }, []);
+  console.log("quotationCount",quotationCount)
   return (
     <DesktopView>
       <TopBanner />
@@ -139,17 +176,8 @@ const NavbarDesktop = () => {
             <Search />
           </div>
           <div className='flex gap-x-4 items-center'>
-            <Link
-              href='/my/transactions'
-              target='_blank'
-              rel='noreferrer'
-              className='flex items-center gap-x-2 !text-gray_r-12/80'
-            >
-              <DocumentCheckIcon className='w-7' />
-              Daftar
-              <br />
-              Quotation
-            </Link>
+
+            <Quotationheader quotationCount={quotationCount} />
 
             <Cardheader cartCount={cartCount} />
 
-- 
cgit v1.2.3

