From e4bbb9cf1b1918ce33e6c2ee22acf17b49d0fcd1 Mon Sep 17 00:00:00 2001
From: it-fixcomart <it@fixcomart.co.id>
Date: Fri, 2 Aug 2024 11:19:10 +0700
Subject: <iman> add delete all cart button

---
 src-migrate/pages/shop/cart/index.tsx | 58 +++++++++++++++++++++++++++--------
 1 file changed, 45 insertions(+), 13 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 0eb9c554..cb0156f1 100644
--- a/src-migrate/pages/shop/cart/index.tsx
+++ b/src-migrate/pages/shop/cart/index.tsx
@@ -2,7 +2,7 @@ import style from './cart.module.css';
 
 import React, { useEffect, useMemo, useState } from 'react';
 import Link from 'next/link';
-import { Button, Checkbox, Tooltip } from '@chakra-ui/react';
+import { Button, Checkbox, Spinner, Tooltip } from '@chakra-ui/react';
 import { toast } from 'react-hot-toast';
 import { useRouter } from 'next/router';
 import { getAuth } from '~/libs/auth';
@@ -24,6 +24,7 @@ const CartPage = () => {
   const [isSelectedAll, setIsSelectedAll] = useState(false);
   const [isButtonChek, setIsButtonChek] = useState(false);
   const [buttonSelectNow, setButtonSelectNow] = useState(true);
+  const [isLoad, setIsLoad] = useState<boolean>(false)
 
   const { loadCart, cart, summary } = useCartStore();
   const useDivvice = useDevice();
@@ -69,7 +70,7 @@ const CartPage = () => {
 
   const handleChange = async (e: React.ChangeEvent<HTMLInputElement>) => {
     if (typeof auth !== 'object' || !cart) return;
-
+    setIsLoad(true)
     const newSelected = e.target.checked;
     setIsSelectedAll(newSelected);
 
@@ -83,23 +84,54 @@ const CartPage = () => {
       });
     }
     await loadCart(auth.id);
+    setIsLoad(false)
+  }
+
+  const handleDelete = () => {
+    console.log("delete data");
   }
 
   return (
     <>
       <div className={style['title']}>Keranjang Belanja</div>
       <div className='h-2' />
-      <div className='flex items-center object-center mt-2'>
-        <Checkbox
-          borderColor='gray.600'
-          colorScheme='red'
-          size='lg'
-          isChecked={hasSelectedAll}
-          onChange={handleChange}
-        />
-        <p className='p-2 text-caption-2'>
-          {hasSelectedAll ? "Unchek all" : "Select all"}
-        </p>
+      <div className='flex items-center object-center justify-between mt-2 mb-2 w-3/4'>
+        <div className='flex items-center object-center'>
+          {isLoad && (
+            <Spinner className='my-auto' size='sm' />
+          )}
+          {!isLoad && (
+            <Checkbox
+              borderColor='gray.600'
+              colorScheme='red'
+              size='lg'
+              isChecked={hasSelectedAll}
+              onChange={handleChange}
+            />
+          )}
+            <p className='p-2 text-caption-2'>
+              {hasSelectedAll ? "Unchek all" : "Select all"}
+            </p>
+        </div>
+          <div className='delate all flex items-center object-center'>
+                <Tooltip
+                  label={clsxm({
+                    'Tidak ada item yang dipilih': !hasSelected,
+                    'Terdapat item yang tidak ada harga': hasSelectNoPrice,
+                  })}
+                >
+                  <Button
+                    bg='#fadede' 
+                    variant='outline' 
+                    colorScheme='red'
+                    w='full'
+                    isDisabled={!hasSelected || hasSelectNoPrice}
+                    onClick={handleDelete}
+                  >
+                    Hapus Barang
+                  </Button>
+                </Tooltip>
+          </div>
       </div>
 
       <div className={style['content']}>
-- 
cgit v1.2.3


From 30a686e4140a503d88142e71c25bc517092b4bd5 Mon Sep 17 00:00:00 2001
From: it-fixcomart <it@fixcomart.co.id>
Date: Fri, 2 Aug 2024 13:23:30 +0700
Subject: <iman> update delete button

---
 src-migrate/pages/shop/cart/index.tsx | 24 +++++++++++++++++++-----
 1 file changed, 19 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 cb0156f1..4586c65c 100644
--- a/src-migrate/pages/shop/cart/index.tsx
+++ b/src-migrate/pages/shop/cart/index.tsx
@@ -15,7 +15,8 @@ import useDevice from '@/core/hooks/useDevice';
 import CartSummaryMobile from '~/modules/cart/components/CartSummaryMobile';
 import Image from '~/components/ui/image';
 import { CartItem } from '~/types/cart'
-import { upsertUserCart } from '~/services/cart'
+import { deleteUserCart ,upsertUserCart } from '~/services/cart'
+import { Trash2Icon } from 'lucide-react';
 
 const CartPage = () => {
   const router = useRouter();
@@ -25,7 +26,7 @@ const CartPage = () => {
   const [isButtonChek, setIsButtonChek] = useState(false);
   const [buttonSelectNow, setButtonSelectNow] = useState(true);
   const [isLoad, setIsLoad] = useState<boolean>(false)
-
+  const [isLoadDelete, setIsLoadDelete] = useState<boolean>(false)
   const { loadCart, cart, summary } = useCartStore();
   const useDivvice = useDevice();
 
@@ -87,8 +88,17 @@ const CartPage = () => {
     setIsLoad(false)
   }
 
-  const handleDelete = () => {
-    console.log("delete data");
+  const handleDelete = async () => {
+    if (typeof auth !== 'object' || !cart) return;
+
+    setIsLoadDelete(true)
+    for (const item of cart.products) {
+      if(item.selected === true){
+        await deleteUserCart(auth.id, [item.cart_id])
+        await loadCart(auth.id)
+      }
+    }
+    setIsLoadDelete(false)
   }
 
   return (
@@ -128,7 +138,11 @@ const CartPage = () => {
                     isDisabled={!hasSelected || hasSelectNoPrice}
                     onClick={handleDelete}
                   >
-                    Hapus Barang
+                    {isLoadDelete && <Spinner size='xs' />}
+                    {!isLoadDelete && <Trash2Icon size={16} />}
+                    <p className='text-sm ml-2'>
+                     Hapus Barang
+                      </p> 
                   </Button>
                 </Tooltip>
           </div>
-- 
cgit v1.2.3

