diff options
| author | it-fixcomart <it@fixcomart.co.id> | 2024-08-05 09:38:32 +0700 |
|---|---|---|
| committer | it-fixcomart <it@fixcomart.co.id> | 2024-08-05 09:38:32 +0700 |
| commit | 8aa9307006910e5b512fc6551f0046f0c5a6a2d5 (patch) | |
| tree | 4380367b52d4881e37d26b4f5477b6eec462a91c | |
| parent | 574754df6366cd693fbe2cd04a7a50fdf9597d1e (diff) | |
| parent | 7eb2d613c6b16c89dd06491a54b229bcbcb8dc13 (diff) | |
Merge branch 'development' of https://bitbucket.org/altafixco/next-indoteknik into development
# Please enter a commit message to explain why this merge is necessary,
# especially if it merges an updated upstream into a topic branch.
#
# Lines starting with '#' will be ignored, and an empty message aborts
# the commit.
| -rw-r--r-- | src-migrate/modules/cart/components/ItemAction.tsx | 4 | ||||
| -rw-r--r-- | src-migrate/modules/cart/components/ItemSelect.tsx | 28 | ||||
| -rw-r--r-- | src-migrate/modules/cart/stores/useCartStore.ts | 12 | ||||
| -rw-r--r-- | src-migrate/modules/product-promo/components/Section.tsx | 1 | ||||
| -rw-r--r-- | src-migrate/pages/shop/cart/index.tsx | 101 | ||||
| -rw-r--r-- | src/lib/cart/components/Cartheader.jsx | 3 |
6 files changed, 36 insertions, 113 deletions
diff --git a/src-migrate/modules/cart/components/ItemAction.tsx b/src-migrate/modules/cart/components/ItemAction.tsx index e5e7f314..e73d507b 100644 --- a/src-migrate/modules/cart/components/ItemAction.tsx +++ b/src-migrate/modules/cart/components/ItemAction.tsx @@ -11,7 +11,6 @@ import { deleteUserCart, upsertUserCart } from '~/services/cart' import { useDebounce } from 'usehooks-ts' import { useCartStore } from '../stores/useCartStore' -import { useProductCartContext } from '@/contexts/ProductCartContext' type Props = { @@ -20,7 +19,7 @@ type Props = { const CartItemAction = ({ item }: Props) => { const auth = getAuth() - const { setRefreshCart } = useProductCartContext() + const [isLoadDelete, setIsLoadDelete] = useState<boolean>(false) const [isLoadQuantity, setIsLoadQuantity] = useState<boolean>(false) @@ -37,7 +36,6 @@ const CartItemAction = ({ item }: Props) => { await deleteUserCart(auth.id, [item.cart_id]) await loadCart(auth.id) setIsLoadDelete(false) - setRefreshCart(true) } const decreaseQty = () => { setQuantity((quantity) => quantity -= 1) } diff --git a/src-migrate/modules/cart/components/ItemSelect.tsx b/src-migrate/modules/cart/components/ItemSelect.tsx index d4a1b537..b904a1de 100644 --- a/src-migrate/modules/cart/components/ItemSelect.tsx +++ b/src-migrate/modules/cart/components/ItemSelect.tsx @@ -13,25 +13,23 @@ type Props = { const CartItemSelect = ({ item }: Props) => { const auth = getAuth() - const { updateCartItem, cart } = useCartStore() + const { loadCart } = useCartStore() const [isLoad, setIsLoad] = useState<boolean>(false) const handleChange = async (e: React.ChangeEvent<HTMLInputElement>) => { - if (typeof auth !== 'object' || !cart) return - - setIsLoad(true); - const updatedCartItems = cart.products.map(cartItem => - cartItem.id === item.id - ? { ...cartItem, selected: e.target.checked } - : cartItem - ); - - // Update the entire cart - const updatedCart = { ...cart, products: updatedCartItems }; - updateCartItem(updatedCart); - - setIsLoad(false); + if (typeof auth !== 'object') return + + setIsLoad(true) + await upsertUserCart({ + userId: auth.id, + type: item.cart_type, + id: item.id, + qty: item.quantity, + selected: e.target.checked + }) + await loadCart(auth.id) + setIsLoad(false) } return ( diff --git a/src-migrate/modules/cart/stores/useCartStore.ts b/src-migrate/modules/cart/stores/useCartStore.ts index 3b50ec32..3d9a0aed 100644 --- a/src-migrate/modules/cart/stores/useCartStore.ts +++ b/src-migrate/modules/cart/stores/useCartStore.ts @@ -1,5 +1,5 @@ import { create } from 'zustand'; -import { CartItem, CartProps } from '~/types/cart'; +import { CartProps } from '~/types/cart'; import { getUserCart } from '~/services/cart'; type State = { @@ -16,7 +16,6 @@ type State = { type Action = { loadCart: (userId: number) => Promise<void>; - updateCartItem: (updateCart: CartProps) => void; }; export const useCartStore = create<State & Action>((set, get) => ({ @@ -40,15 +39,6 @@ export const useCartStore = create<State & Action>((set, get) => ({ const summary = computeSummary(cart); set({ summary }); }, - updateCartItem: (updatedCart) => { - const cart = get().cart; - if (!cart) return; - - set({ cart: updatedCart }); - const summary = computeSummary(updatedCart); - set({ summary }); - }, - })); const computeSummary = (cart: CartProps) => { diff --git a/src-migrate/modules/product-promo/components/Section.tsx b/src-migrate/modules/product-promo/components/Section.tsx index 1228a6f0..e1719998 100644 --- a/src-migrate/modules/product-promo/components/Section.tsx +++ b/src-migrate/modules/product-promo/components/Section.tsx @@ -25,7 +25,6 @@ const ProductPromoSection = ({ product, productId }: Props) => { const promotions = promotionsQuery.data const { openModal } = useModalStore() - console.log("productId",productId) return ( <SmoothRender isLoaded={(promotions?.data && promotions?.data.length > 0) || false} 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' />} diff --git a/src/lib/cart/components/Cartheader.jsx b/src/lib/cart/components/Cartheader.jsx index f76634ce..c7a8f0b8 100644 --- a/src/lib/cart/components/Cartheader.jsx +++ b/src/lib/cart/components/Cartheader.jsx @@ -1,15 +1,12 @@ import { useCallback, useEffect, useMemo, useState } from 'react' import { getCartApi } from '../api/CartApi' import currencyFormat from '@/core/utils/currencyFormat' -import Image from '@/core/components/elements/Image/Image' import { createSlug } from '@/core/utils/slug' import useAuth from '@/core/hooks/useAuth' import { useRouter } from 'next/router' import odooApi from '@/core/api/odooApi' import { useProductCartContext } from '@/contexts/ProductCartContext' -import currencyFormat from '@/core/utils/currencyFormat' import Image from '@/core/components/elements/Image/Image' -import { createSlug } from '@/core/utils/slug' import whatsappUrl from '@/core/utils/whatsappUrl' import { AnimatePresence, motion } from 'framer-motion' import style from '../../../../src-migrate/modules/cart/styles/item-promo.module.css' |
