From 3f992f62c54e09254c48d653c2cd138df1cbd8e2 Mon Sep 17 00:00:00 2001 From: Miqdad Date: Sat, 31 May 2025 14:33:24 +0700 Subject: fix error deployment --- src-migrate/modules/cart/components/ItemAction.tsx | 65 ++++++----- src-migrate/modules/cart/components/ItemSelect.tsx | 22 ++-- src-migrate/modules/cart/stores/useCartStore.ts | 98 +++++++++++------ src-migrate/pages/shop/cart/index.tsx | 121 ++++++++++----------- 4 files changed, 177 insertions(+), 129 deletions(-) diff --git a/src-migrate/modules/cart/components/ItemAction.tsx b/src-migrate/modules/cart/components/ItemAction.tsx index 4dcebd9e..b06e8e75 100644 --- a/src-migrate/modules/cart/components/ItemAction.tsx +++ b/src-migrate/modules/cart/components/ItemAction.tsx @@ -22,11 +22,11 @@ import { import { toast } from 'react-hot-toast'; -type Props = { +interface Props { item: CartItem; -}; +} -const CartItemAction = ({ item }: Props) => { +const CartItemAction: React.FC = ({ item }) => { const auth = getAuth(); const { setRefreshCart } = useProductCartContext(); const [isLoadDelete, setIsLoadDelete] = useState(false); @@ -36,9 +36,9 @@ const CartItemAction = ({ item }: Props) => { const { loadCart, cart, updateCartItem } = useCartStore(); - const limitQty = item.limit_qty?.transaction || 0; + const limitQty: number = item.limit_qty?.transaction || 0; - const handleDelete = async () => { + const handleDelete = async (): Promise => { if (typeof auth !== 'object') return; setIsLoadDelete(true); @@ -80,25 +80,29 @@ const CartItemAction = ({ item }: Props) => { } }; - const updateQuantityInCookie = (productId, cartId, newQuantity) => { + const updateQuantityInCookie = ( + productId: number, + cartId: string | number, + newQuantity: number + ): boolean => { try { - const cartData = getCartDataFromCookie(); + const cartData = getCartDataFromCookie() as Record; let itemFound = false; + const cartIdString = String(cartId); // Find item by cart_id key or search within objects - if (cartData[cartId]) { - cartData[cartId].quantity = newQuantity; + if (cartData[cartIdString]) { + cartData[cartIdString].quantity = newQuantity; itemFound = true; } else { // Search by product id or cart_id within objects - for (const key in cartData) { + Object.keys(cartData).forEach((key) => { const item = cartData[key]; - if (item.id === productId || item.cart_id === cartId) { + if (item.id === productId || String(item.cart_id) === cartIdString) { item.quantity = newQuantity; itemFound = true; - break; } - } + }); } if (itemFound) { @@ -113,12 +117,12 @@ const CartItemAction = ({ item }: Props) => { } }; - const decreaseQty = () => { - setQuantity((quantity) => (quantity -= 1)); + const decreaseQty = (): void => { + setQuantity((prevQuantity) => prevQuantity - 1); }; - const increaseQty = () => { - setQuantity((quantity) => (quantity += 1)); + const increaseQty = (): void => { + setQuantity((prevQuantity) => prevQuantity + 1); }; const debounceQty = useDebounce(quantity, 1000); @@ -129,7 +133,7 @@ const CartItemAction = ({ item }: Props) => { }, [debounceQty, limitQty]); useEffect(() => { - const updateCart = async () => { + const updateCart = async (): Promise => { if (typeof auth !== 'object' || isNaN(debounceQty)) return; if (debounceQty === item.quantity) return; @@ -167,20 +171,22 @@ const CartItemAction = ({ item }: Props) => { await loadCart(auth.id); // Re-update cookie if server reload overwrote it - const currentCookieData = getCartDataFromCookie(); + const currentCookieData = getCartDataFromCookie() as Record< + string, + any + >; let needsReUpdate = false; - for (const key in currentCookieData) { + Object.keys(currentCookieData).forEach((key) => { const cookieItem = currentCookieData[key]; if ( (cookieItem.id === item.id || - cookieItem.cart_id === item.cart_id) && + String(cookieItem.cart_id) === String(item.cart_id)) && cookieItem.quantity !== debounceQty ) { needsReUpdate = true; - break; } - } + }); if (needsReUpdate) { updateQuantityInCookie(item.id, item.cart_id, debounceQty); @@ -202,6 +208,13 @@ const CartItemAction = ({ item }: Props) => { //eslint-disable-next-line react-hooks/exhaustive-deps }, [debounceQty]); + const handleQuantityInputChange = ( + e: React.ChangeEvent + ): void => { + const value = parseInt(e.target.value); + setQuantity(isNaN(value) ? 1 : value); + }; + return (