diff options
| author | Rafi Zadanly <zadanlyr@gmail.com> | 2023-12-22 17:33:46 +0700 |
|---|---|---|
| committer | Rafi Zadanly <zadanlyr@gmail.com> | 2023-12-22 17:33:46 +0700 |
| commit | 89f32128f37d99b490de7590e2116a9cfd853f89 (patch) | |
| tree | feb74cc6bd0030b291fbf3dbba9b89a7afd6ea31 /src-migrate/modules/cart/components/ItemSelect.tsx | |
| parent | c9366090153e8aba3a673b2b77cbc8acc24e59a5 (diff) | |
Update promotion program feature
Diffstat (limited to 'src-migrate/modules/cart/components/ItemSelect.tsx')
| -rw-r--r-- | src-migrate/modules/cart/components/ItemSelect.tsx | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/src-migrate/modules/cart/components/ItemSelect.tsx b/src-migrate/modules/cart/components/ItemSelect.tsx new file mode 100644 index 00000000..96e7c713 --- /dev/null +++ b/src-migrate/modules/cart/components/ItemSelect.tsx @@ -0,0 +1,47 @@ +import { Checkbox, Spinner } from '@chakra-ui/react' +import React, { useState } from 'react' + +import { getAuth } from '~/common/libs/auth' +import { CartItem } from '~/common/types/cart' +import { upsertUserCart } from '~/services/cart' + +import { useCartStore } from '../stores/useCartStore' + +type Props = { + item: CartItem +} + +const CartItemSelect = ({ item }: Props) => { + const auth = getAuth() + const { loadCart } = useCartStore() + + const [isLoad, setIsLoad] = useState<boolean>(false) + + const handleChange = async (e: React.ChangeEvent<HTMLInputElement>) => { + if (typeof auth !== 'object') return + + setIsLoad(true) + await upsertUserCart(auth.id, item.cart_type, item.id, item.quantity, e.target.checked) + await loadCart(auth.id) + setIsLoad(false) + } + + return ( + <div className='w-5 my-auto'> + {isLoad && ( + <Spinner className='my-auto' size='sm' /> + )} + {!isLoad && ( + <Checkbox + borderColor='gray.600' + colorScheme='red' + size='lg' + isChecked={item.selected} + onChange={handleChange} + /> + )} + </div> + ) +} + +export default CartItemSelect
\ No newline at end of file |
