import { useState } from "react"; import { Plus, Minus, Trash2 } from "lucide-react"; import { Dialog, DialogContent, DialogOverlay } from "../ui/dialog"; import { Button } from "../ui/button"; interface QuantityControlProps { quantity: number; maxQuantity: number; onIncrease: () => void; onDecrease: () => void; onRemove?: () => void; isPending: boolean; allowManualEdit?: boolean; onQuantityChange?: (quantity: number) => void; } export const QuantityControl = ({ quantity, maxQuantity, onIncrease, onDecrease, onRemove, isPending, allowManualEdit = false, onQuantityChange, }: QuantityControlProps) => { const [openDeleteModal, setOpenDeleteModal] = useState(false); const [isEditing, setIsEditing] = useState(false); const [editValue, setEditValue] = useState(quantity.toString()); const handleEditStart = () => { if (allowManualEdit) { setIsEditing(true); setEditValue(quantity.toString()); } }; const handleEditConfirm = () => { const newQuantity = parseInt(editValue); if (!isNaN(newQuantity) && newQuantity > 0 && newQuantity <= maxQuantity) { onQuantityChange?.(newQuantity); } setIsEditing(false); }; const handleEditCancel = () => { setIsEditing(false); setEditValue(quantity.toString()); }; const handleKeyDown = (e: React.KeyboardEvent) => { if (e.key === "Enter") { handleEditConfirm(); } else if (e.key === "Escape") { handleEditCancel(); } }; return (
{ if (quantity < maxQuantity) { onIncrease(); } }} className={`${quantity >= maxQuantity ? "text-GRAY" : "text-BLACK"} cursor-pointer`} /> {isEditing ? ( setEditValue(e.target.value)} onBlur={handleEditConfirm} onKeyDown={handleKeyDown} className="px-2 text-B14 font-bold w-12 text-center bg-transparent border-none outline-none" min="1" max={maxQuantity} /> ) : ( { if (e.key === "Enter" || e.key === " ") { handleEditStart(); } }} role={allowManualEdit ? "button" : undefined} tabIndex={allowManualEdit ? 0 : undefined} > {quantity} )} {quantity > 1 || !onRemove ? ( { if (quantity > 1 || !onRemove) { onDecrease(); } }} className={`${(quantity <= 1 && onRemove) || (quantity <= 0 && !onRemove) ? "text-GRAY" : "text-BLACK"} cursor-pointer`} /> ) : ( <> { setOpenDeleteModal(true); }} /> {})} onOpenChange={setOpenDeleteModal} open={openDeleteModal} /> )}
); }; interface RemoveCartModalProps { isPending: boolean; onRemove: () => void; open: boolean; onOpenChange: (open: boolean) => void; } const RemoveCartModal = ({ isPending, onRemove, open, onOpenChange, }: RemoveCartModalProps) => { return (

حذف محصول از سبد خرید

این محصول از سبد شما حذف شود؟

); };