'use client' import { useState, useEffect } from 'react' import { addToCart, showAlert } from 'store' import { existItem } from 'utils' import { ArrowLink, ProductPrice, CartButtons } from 'components' import { useAppDispatch, useAppSelector } from 'hooks' const AddToCartOperation = props => { //? Props const { product } = props //? Assets const dispatch = useAppDispatch() //? Store const { cartItems, tempColor, tempSize } = useAppSelector(state => state.cart) //? State const [currentItemInCart, setCurrentItemInCart] = useState(undefined) //? Re-Renders useEffect(() => { const item = existItem(cartItems, product._id, tempColor, tempSize) setCurrentItemInCart(item) }, [tempColor, tempSize, cartItems]) //? handlers const handleAddItem = () => { if (product.inStock === 0) return dispatch( showAlert({ status: 'error', title: '此商品缺货', }) ) dispatch( addToCart({ productID: product._id, name: product.title, price: product.price, discount: product.discount, inStock: product.inStock, sold: product.sold, color: tempColor, size: tempSize, img: product.images[0], quantity: 1, }) ) } //? Render(s) return (