summaryrefslogtreecommitdiff
path: root/src-migrate/pages
diff options
context:
space:
mode:
authorIT Fixcomart <it@fixcomart.co.id>2024-08-02 06:24:16 +0000
committerIT Fixcomart <it@fixcomart.co.id>2024-08-02 06:24:16 +0000
commit612a3e28416cce42710cda163ddc321fec3977fc (patch)
treec12909fdc46282adaf1de47a9431788a61643f86 /src-migrate/pages
parent85355d38fd1c6d4b9fd5c45c3410088bfbe47d01 (diff)
parent30a686e4140a503d88142e71c25bc517092b4bd5 (diff)
Merged in Feature/iman-unchek-cart (pull request #203)
Feature/iman unchek cart
Diffstat (limited to 'src-migrate/pages')
-rw-r--r--src-migrate/pages/shop/cart/index.tsx76
1 files changed, 61 insertions, 15 deletions
diff --git a/src-migrate/pages/shop/cart/index.tsx b/src-migrate/pages/shop/cart/index.tsx
index 0eb9c554..4586c65c 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';
@@ -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();
@@ -24,7 +25,8 @@ const CartPage = () => {
const [isSelectedAll, setIsSelectedAll] = useState(false);
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();
@@ -69,7 +71,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 +85,67 @@ const CartPage = () => {
});
}
await loadCart(auth.id);
+ setIsLoad(false)
+ }
+
+ 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 (
<>
<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}
+ >
+ {isLoadDelete && <Spinner size='xs' />}
+ {!isLoadDelete && <Trash2Icon size={16} />}
+ <p className='text-sm ml-2'>
+ Hapus Barang
+ </p>
+ </Button>
+ </Tooltip>
+ </div>
</div>
<div className={style['content']}>