"use client" import { useState, useEffect, useRef } from "react" import Image from "next/image" import { Minus, Plus, Trash2 } from "lucide-react" import { Button } from "@/components/ui/button" import { Card } from "@/components/ui/card" import { useUpdateCartItemQuantity, useRemoveFromCart } from "@/lib/hooks" import type { CartItem, CartTranslations } from "./types" interface CartItemCardProps { item: CartItem translations: CartTranslations onUpdate?: () => void } export default function CartItemCard({ item, translations: t, onUpdate }: CartItemCardProps) { const [localQuantity, setLocalQuantity] = useState(item.quantity) const [pendingQuantity, setPendingQuantity] = useState(item.quantity) const [isLoading, setIsLoading] = useState(false) const updateTimeoutRef = useRef() const { mutate: updateQuantity } = useUpdateCartItemQuantity() const { mutate: removeItem, isPending: isRemoving } = useRemoveFromCart() useEffect(() => { setLocalQuantity(item.quantity) setPendingQuantity(item.quantity) }, [item.quantity]) useEffect(() => { if (pendingQuantity === item.quantity) return if (updateTimeoutRef.current) { clearTimeout(updateTimeoutRef.current) } updateTimeoutRef.current = setTimeout(() => { setIsLoading(true) if (pendingQuantity <= 0) { removeItem(item.product_id, { onSuccess: () => onUpdate?.(), onError: () => { setLocalQuantity(item.quantity) setPendingQuantity(item.quantity) }, onSettled: () => setIsLoading(false), }) } else { updateQuantity( { productId: item.product_id, quantity: pendingQuantity }, { onSuccess: () => onUpdate?.(), onError: () => { setLocalQuantity(item.quantity) setPendingQuantity(item.quantity) }, onSettled: () => setIsLoading(false), } ) } }, 300) return () => { if (updateTimeoutRef.current) { clearTimeout(updateTimeoutRef.current) } } }, [pendingQuantity, item.quantity, item.product_id, updateQuantity, removeItem, onUpdate]) const handleQuantityIncrease = (e: React.MouseEvent) => { e.preventDefault() e.stopPropagation() if (isLoading) return const newQuantity = localQuantity + 1 setLocalQuantity(newQuantity) setPendingQuantity(newQuantity) } const handleQuantityDecrease = (e: React.MouseEvent) => { e.preventDefault() e.stopPropagation() if (isLoading) return const newQuantity = localQuantity - 1 if (newQuantity < 1) { handleDelete() return } setLocalQuantity(newQuantity) setPendingQuantity(newQuantity) } const handleDelete = () => { setIsLoading(true) removeItem(item.product_id, { onSuccess: () => onUpdate?.(), onSettled: () => setIsLoading(false), }) } const getImageSrc = () => { if (item.product.image) return item.product.image if (item.product.images?.length > 0) return item.product.images[0] return "/placeholder.svg" } return (
{item.product.name}

{item.product.name}

{item.seller?.name || "Store"}

{t.pricePerUnit} {item.price_formatted}

{t.additionalPrice} {item.sub_total_formatted}

{item.discount_formatted && item.discount_formatted !== "0 TMT" && (

{t.discount} {item.discount_formatted}

)}
{t.totalPrice} {item.total_formatted}
{localQuantity}
) }