"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() // Sync local quantity with server quantity useEffect(() => { setLocalQuantity(item.quantity) setPendingQuantity(item.quantity) }, [item.quantity]) // Debounced update effect useEffect(() => { if (pendingQuantity === item.quantity) { return } // Clear previous timeout if (updateTimeoutRef.current) { clearTimeout(updateTimeoutRef.current) } // Set new timeout for update updateTimeoutRef.current = setTimeout(() => { setIsLoading(true) if (pendingQuantity <= 0) { removeItem(item.id, { onSuccess: () => { onUpdate?.() }, onError: (error) => { console.error("Failed to remove item:", error) // Revert on error setLocalQuantity(item.quantity) setPendingQuantity(item.quantity) }, onSettled: () => { setIsLoading(false) }, }) } else { updateQuantity( { itemId: item.id, quantity: pendingQuantity }, { onSuccess: () => { onUpdate?.() }, onError: (error) => { console.error("Failed to update quantity:", error) // Revert on error setLocalQuantity(item.quantity) setPendingQuantity(item.quantity) }, onSettled: () => { setIsLoading(false) }, } ) } }, 300) return () => { if (updateTimeoutRef.current) { clearTimeout(updateTimeoutRef.current) } } }, [pendingQuantity, item.quantity, item.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.id, { onSuccess: () => { onUpdate?.() }, onError: (error) => { console.error("Failed to remove item:", error) }, onSettled: () => { setIsLoading(false) }, }) } const getImageSrc = () => { if (item.product.image) return item.product.image if (item.product.images && item.product.images.length > 0) { return item.product.images[0] } return "/placeholder.svg" } return (
{/* Product Image & Info */}
{item.product.name}

{item.product.name}

{item.seller.name}

{/* Price & Quantity */}

{t.pricePerUnit}{" "} {item.price_formatted || `${item.price} TMT`}

{t.additionalPrice}{" "} {item.sub_total_formatted || `${item.total} TMT`}

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

{t.discount} {item.discount_formatted}

)}
{t.totalPrice} {item.total_formatted || `${item.total} TMT`}
{/* Quantity Controls */}
{localQuantity}
) }