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

