From cf3bcd1f3d1e0649204a786e9532a099772dad85 Mon Sep 17 00:00:00 2001
From: it-fixcomart <it@fixcomart.co.id>
Date: Sat, 31 Aug 2024 09:42:34 +0700
Subject: <iman> update cart pop up

---
 src-migrate/pages/shop/cart/index.tsx | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

(limited to 'src-migrate/pages/shop/cart/index.tsx')

diff --git a/src-migrate/pages/shop/cart/index.tsx b/src-migrate/pages/shop/cart/index.tsx
index f33dde09..a2e477da 100644
--- a/src-migrate/pages/shop/cart/index.tsx
+++ b/src-migrate/pages/shop/cart/index.tsx
@@ -1,6 +1,6 @@
 import style from './cart.module.css';
 
-import React, { useEffect, useMemo, useState } from 'react';
+import React, { useEffect, useMemo, useRef, useState } from 'react';
 import Link from 'next/link';
 import { Button, Checkbox, Spinner, Tooltip } from '@chakra-ui/react';
 import { toast } from 'react-hot-toast';
@@ -28,10 +28,12 @@ const CartPage = () => {
   const [buttonSelectNow, setButtonSelectNow] = useState(true);
   const [isLoad, setIsLoad] = useState<boolean>(false)
   const [isLoadDelete, setIsLoadDelete] = useState<boolean>(false)
-  const { loadCart, cart, summary } = useCartStore();
+  const { loadCart, cart, summary, } = useCartStore();
   const useDivvice = useDevice();
   const { setRefreshCart } = useProductCartContext()
   const [isTop, setIsTop] = useState(true);
+  const [hasChanged, setHasChanged] = useState(false);
+  const prevCartRef = useRef<CartItem[] | null>(null);
 
   useEffect(() => {
     const handleScroll = () => {
-- 
cgit v1.2.3


From ae6033238a4e7765bce7cbe96a2662f3c00b67c1 Mon Sep 17 00:00:00 2001
From: it-fixcomart <it@fixcomart.co.id>
Date: Sat, 31 Aug 2024 09:51:26 +0700
Subject: <iman> upadate new cart pop up

---
 src-migrate/pages/shop/cart/index.tsx | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

(limited to 'src-migrate/pages/shop/cart/index.tsx')

diff --git a/src-migrate/pages/shop/cart/index.tsx b/src-migrate/pages/shop/cart/index.tsx
index a2e477da..c9ff86b7 100644
--- a/src-migrate/pages/shop/cart/index.tsx
+++ b/src-migrate/pages/shop/cart/index.tsx
@@ -28,7 +28,7 @@ const CartPage = () => {
   const [buttonSelectNow, setButtonSelectNow] = useState(true);
   const [isLoad, setIsLoad] = useState<boolean>(false)
   const [isLoadDelete, setIsLoadDelete] = useState<boolean>(false)
-  const { loadCart, cart, summary, } = useCartStore();
+  const { loadCart, cart, summary, updateCartItem} = useCartStore();
   const useDivvice = useDevice();
   const { setRefreshCart } = useProductCartContext()
   const [isTop, setIsTop] = useState(true);
-- 
cgit v1.2.3


From a76376b74c02ee6875b68b6cfb59090a159df40a Mon Sep 17 00:00:00 2001
From: it-fixcomart <it@fixcomart.co.id>
Date: Sat, 31 Aug 2024 11:44:49 +0700
Subject: <iman> update new cart pop up

---
 src-migrate/pages/shop/cart/index.tsx | 35 ++++++++++++++++++++++++++++++-----
 1 file changed, 30 insertions(+), 5 deletions(-)

(limited to 'src-migrate/pages/shop/cart/index.tsx')

diff --git a/src-migrate/pages/shop/cart/index.tsx b/src-migrate/pages/shop/cart/index.tsx
index c9ff86b7..2fa53681 100644
--- a/src-migrate/pages/shop/cart/index.tsx
+++ b/src-migrate/pages/shop/cart/index.tsx
@@ -28,7 +28,7 @@ const CartPage = () => {
   const [buttonSelectNow, setButtonSelectNow] = useState(true);
   const [isLoad, setIsLoad] = useState<boolean>(false)
   const [isLoadDelete, setIsLoadDelete] = useState<boolean>(false)
-  const { loadCart, cart, summary, updateCartItem} = useCartStore();
+  const { loadCart, cart, summary, updateCartItem } = useCartStore();
   const useDivvice = useDevice();
   const { setRefreshCart } = useProductCartContext()
   const [isTop, setIsTop] = useState(true);
@@ -101,6 +101,31 @@ const CartPage = () => {
     return cart.products.every(item => item.selected);
   }, [cart]);
 
+
+  useEffect(() => {
+    const updateCartItems = async () => {
+      if (typeof auth === 'object' && cart) {
+        const upsertPromises = cart.products.map(item => 
+          upsertUserCart({
+            userId: auth.id,
+            type: item.cart_type,
+            id: item.id,
+            qty: item.quantity,
+            selected: item.selected
+          })
+        );
+        try {
+          await Promise.all(upsertPromises);
+          await loadCart(auth.id);
+        } catch (error) {
+          console.error('Failed to update cart items:', error);
+        }
+      }
+    };
+
+    updateCartItems();
+  }, [hasChanged]);
+
   const handleCheckout = () => {
     router.push('/shop/checkout');
   }
@@ -151,7 +176,7 @@ const CartPage = () => {
 
   return (
     <>
-      <div className={`${isTop ? 'border-b-[0px]' : 'border-b-[1px]'} sticky top-[180px] bg-white py-4 border-gray-300 z-50 w-3/4`}>
+      <div className={`${isTop ? 'border-b-[0px]' : 'border-b-[1px]'} sticky top-[157px] bg-white py-4 border-gray-300 z-50 w-3/4`}>
         <div className={`${style['title']}`}>Keranjang Belanja</div>
         <div className='h-2' />
         <div className={`flex items-center object-center justify-between `}>
@@ -169,7 +194,7 @@ const CartPage = () => {
               />
             )}
               <p className='p-2 text-caption-2'>
-                {hasSelectedAll ? "Unchek all" : "Select all"}
+                {hasSelectedAll ? "Uncheck all" : "Select all"}
               </p>
           </div>
             <div className='delate all flex items-center object-center'>
@@ -183,7 +208,7 @@ const CartPage = () => {
                       variant='outline' 
                       colorScheme='red'
                       w='full'
-                      isDisabled={!hasSelected || hasSelectNoPrice}
+                      isDisabled={!hasSelected}
                       onClick={handleDelete}
                     >
                       {isLoadDelete && <Spinner size='xs' />}
@@ -290,4 +315,4 @@ const CartPage = () => {
   );
 };
 
-export default CartPage;
+export default CartPage;
\ No newline at end of file
-- 
cgit v1.2.3

