"use client"; import { zodResolver } from "@hookform/resolvers/zod"; import { ArrowUpRight, PackageOpen, X } from "lucide-react"; import { useCallback } from "react"; import { useFieldArray, useForm, type UseFormReturn } from "react-hook-form"; import { Controller } from "react-hook-form"; import z from "zod"; import QuantityInput from "@/components/shadcnblocks/quantity-input"; import { AspectRatio } from "@/components/ui/aspect-ratio"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardTitle } from "@/components/ui/card"; import { Empty, EmptyContent, EmptyMedia, EmptyTitle, } from "@/components/ui/empty"; import { Field } from "@/components/ui/field"; import { Sheet, SheetContent, SheetFooter, SheetHeader, SheetTitle, } from "@/components/ui/sheet"; interface ProductPrice { regular: number; sale?: number; currency: string; } type CartItem = { product_id: string; link: string; name: string; image: string; price: ProductPrice; details: { label: string; value: string; }[]; }; interface ShoppingCart14Props { cartItems: CartItem[]; } interface FullCartProps { cartItems: CartItem[]; } interface CartItemProps extends CartItem { index: number; form: UseFormReturn; quantity: number; onRemoveClick: () => void; onQuantityChange: (newQty: number) => void; } const formatPrice = (price: number, currency = "IRR") => new Intl.NumberFormat("fa-IR", { style: "currency", currency, }).format(price); const CART_ITEMS: CartItem[] = [ { product_id: "product-1", link: "#", name: "کفش اسپرت زرشکی", image: "/block-media/shadcnblocks/block/ecommerce/clothes/stylish-maroon-sneaker.png", price: { regular: 354.0, currency: "IRR", }, details: [ { label: "رنگ", value: "قرمز", }, { label: "سایز", value: "36", }, ], }, { product_id: "product-2", link: "#", name: "سوئیشرت دو رنگ با لوگوی گلدوزی", image: "/block-media/shadcnblocks/block/ecommerce/clothes/bicolor-crewneck-sweatshirt-with-embroidered-logo.png", price: { regular: 499.0, currency: "IRR", }, details: [ { label: "رنگ", value: "آبی و سفید", }, { label: "سایز", value: "L", }, ], }, { product_id: "product-3", link: "#", name: "هودی مشکی", image: "/block-media/shadcnblocks/block/ecommerce/clothes/black-hoodie-against-light-background.png", price: { regular: 84.0, currency: "IRR", }, details: [ { label: "رنگ", value: "مشکی", }, { label: "سایز", value: "XL", }, ], }, { product_id: "product-4", link: "#", name: "کیف چرمی زرشکی", image: "/block-media/shadcnblocks/block/ecommerce/clothes/maroon-leather-handbag.png", price: { regular: 245.0, currency: "IRR", }, details: [ { label: "رنگ", value: "زرشکی", }, ], }, { product_id: "product-5", link: "#", name: "کلاه فدورا کلاسیک", image: "/block-media/shadcnblocks/block/ecommerce/accessories/Classic-Fedora-Hat-1.png", price: { regular: 499.0, currency: "IRR", }, details: [ { label: "رنگ", value: "بژ", }, ], }, ]; const cartFormSchema = z.object({ products: z .object({ product_id: z.string(), quantity: z.number(), price: z.number(), }) .array(), }); type CartFormType = z.infer; const ShoppingCart14 = ({ cartItems = CART_ITEMS }: ShoppingCart14Props) => { const isEmpty = cartItems.length === 0; return ( {isEmpty ? : } ); }; const EmptyCart = () => { return ( سبد خرید شما خالی است ); }; const FullCart = ({ cartItems }: FullCartProps) => { const defaultProducts = cartItems.map((item) => ({ product_id: item.product_id, quantity: 1, price: item.price.sale ?? item.price.regular, })); const form = useForm({ resolver: zodResolver(cartFormSchema), defaultValues: { products: defaultProducts, }, }); const { fields, remove, update } = useFieldArray({ control: form.control, name: "products", }); const formItems = form.watch("products"); const itemsCount = formItems && formItems.length ? formItems.length : 0; const totalPrice = formItems?.reduce( (sum, p) => sum + p.price * p.quantity, 0, ); const onSubmit = (data: CartFormType) => { console.log(data); }; const handleQuantityChange = useCallback( (index: number) => (newQty: number) => update(index, { ...fields[index], quantity: newQty }), [update, fields], ); const handleRemove = useCallback( (index: number) => () => { remove(index); }, [remove], ); return (
سبد خرید ({itemsCount.toLocaleString("fa-IR")})
{fields.map((field, index) => ( handleRemove(index)()} quantity={field.quantity} onQuantityChange={(newQty: number) => handleQuantityChange(index)(newQty) } {...(cartItems.find( (p) => p.product_id === field.product_id, ) as CartItem)} /> ))}

مبلغ قابل پرداخت

{formatPrice(totalPrice ?? 0, cartItems[0]?.price.currency)}
); }; const CartItem = ({ image, name, details, index, form, onQuantityChange, onRemoveClick, price, quantity, }: CartItemProps) => { const { regular, currency } = price; return (
{name}
{name}
    {details.map((item, index) => { const isLast = index === details.length - 1; return (
  • {item.label}:
    {item.value}
    {!isLast && ( / )}
  • ); })}
{quantity.toLocaleString("fa-IR")} × {formatPrice(regular, currency)}
); }; const QuantityField = ({ form, index, onQuantityChange, }: { form: ReturnType>; index: number; onQuantityChange: (n: number) => void; }) => { return ( (
{ field.onChange(newQty); onQuantityChange(newQty); }} className="rounded-none border-none !shadow-none" />
)} /> ); }; export { ShoppingCart14 };