diff options
Diffstat (limited to 'src-migrate/pages')
| -rw-r--r-- | src-migrate/pages/shop/cart/index.tsx | 101 |
1 files changed, 21 insertions, 80 deletions
diff --git a/src-migrate/pages/shop/cart/index.tsx b/src-migrate/pages/shop/cart/index.tsx index cfb20284..8d9ea91c 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, useRef, useState } from 'react'; +import React, { useEffect, useMemo, useState } from 'react'; import Link from 'next/link'; import { Button, Checkbox, Spinner, Tooltip } from '@chakra-ui/react'; import { toast } from 'react-hot-toast'; @@ -28,12 +28,10 @@ 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 } = 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 = () => { @@ -53,35 +51,6 @@ const CartPage = () => { } }, [auth, loadCart, cart, isButtonChek]); - useEffect(() => { - if (typeof auth === 'object' && !cart) { - loadCart(auth.id); - setIsStepApproval(auth?.feature?.soApproval); - } - }, [auth, loadCart, cart, isButtonChek]); - - useEffect(() => { - const hasSelectedChanged = () => { - if (prevCartRef.current && cart) { - const prevCart = prevCartRef.current; - return cart.products.some((item, index) => - prevCart[index] && prevCart[index].selected !== item.selected - ); - } - return false; - }; - - if (hasSelectedChanged()) { - setHasChanged(true) - // Perform necessary actions here if selection has changed - }else{ - setHasChanged(false) - } - - // Update the ref to the current cart state - prevCartRef.current = cart ? [...cart.products] : null; - }, [cart]); - const hasSelectedPromo = useMemo(() => { if (!cart) return false; return cart.products.some(item => item.cart_type === 'promotion' && item.selected); @@ -102,31 +71,6 @@ 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'); } @@ -140,26 +84,23 @@ const CartPage = () => { } const handleChange = async (e: React.ChangeEvent<HTMLInputElement>) => { - - // Ensure that cart is not null before attempting to update - if (cart) { - const updatedCart = { - ...cart, - products: cart.products.map(item => ({ - ...item, - selected: !hasSelectedAll - })) - }; - - updateCartItem(updatedCart); // Pass only valid CartProps to updateCartItem - if(hasSelectedAll){ - setIsSelectedAll(false); - }else{ - setIsSelectedAll(true); - } - } - }; + if (typeof auth !== 'object' || !cart) return; + setIsLoad(true) + const newSelected = e.target.checked; + setIsSelectedAll(newSelected); + for (const item of cart.products) { + await upsertUserCart({ + userId: auth.id, + type: item.cart_type, + id: item.id, + qty: item.quantity, + selected: newSelected + }); + } + await loadCart(auth.id); + setIsLoad(false) + } const handleDelete = async () => { if (typeof auth !== 'object' || !cart) return; @@ -177,7 +118,7 @@ const CartPage = () => { return ( <> - <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={`${isTop ? 'border-b-[0px]' : 'border-b-[1px]'} sticky top-[180px] 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 `}> @@ -195,7 +136,7 @@ const CartPage = () => { /> )} <p className='p-2 text-caption-2'> - {hasSelectedAll ? "Uncheck all" : "Select all"} + {hasSelectedAll ? "Unchek all" : "Select all"} </p> </div> <div className='delate all flex items-center object-center'> @@ -209,7 +150,7 @@ const CartPage = () => { variant='outline' colorScheme='red' w='full' - isDisabled={!hasSelected} + isDisabled={!hasSelected || hasSelectNoPrice} onClick={handleDelete} > {isLoadDelete && <Spinner size='xs' />} |
