"use client"; import { zodResolver } from "@hookform/resolvers/zod"; import { ArrowUpRight, PackageOpen, X } from "lucide-react"; import { useCallback } from "react"; import type { UseFormReturn } from "react-hook-form"; import { Controller, FormProvider, useFieldArray, useForm, useFormContext, } 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 { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { Empty, EmptyContent, EmptyMedia, EmptyTitle, } from "@/components/ui/empty"; import { Field } from "@/components/ui/field"; 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 ShoppingCart7Props { cartItems: CartItem[]; } interface FullCartProps { cartItems: CartItem[]; formItems: CartFormType["products"]; form: UseFormReturn; } interface CartItemProps extends CartItem { index: number; 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/skin-care-product/point-normal-Xp0bTCLD07k-unsplash-3.jpg", price: { regular: 49.0, currency: "IRR", }, details: [ { label: "سایز", value: "100ml", }, ], }, { product_id: "product-2", link: "#", name: "ست مراقبت درخشان", image: "/block-media/shadcnblocks/block/ecommerce/skin-care-product/pexels-amelie-chen-243775000-12352170-3.jpg", price: { regular: 140.0, currency: "IRR", }, }, { product_id: "product-3", link: "#", name: "کرم جوان‌سازی تایم‌لس", image: "/block-media/shadcnblocks/block/ecommerce/skin-care-product/pexels-carlos-diaz-216017-18350286-3.jpg", price: { regular: 84.0, currency: "IRR", }, details: [ { label: "سایز", value: "240ml", }, ], }, { product_id: "product-4", link: "#", name: "اسپری قفل‌کننده رطوبت", image: "/block-media/shadcnblocks/block/ecommerce/skin-care-product/pexels-rdne-8903695-3.jpg", price: { regular: 245.0, currency: "IRR", }, details: [ { label: "سایز", value: "250ml", }, ], }, { product_id: "product-5", link: "#", name: "کرم شب هیدرابلوم", image: "/block-media/shadcnblocks/block/ecommerce/skin-care-product/pexels-volkerthimm-19049367-3.jpg", price: { regular: 499.0, currency: "IRR", }, details: [ { label: "سایز", value: "200ml", }, ], }, ]; const cartFormSchema = z.object({ products: z .object({ product_id: z.string(), quantity: z.number(), price: z.number(), }) .array(), }); type CartFormType = z.infer; const ShoppingCart7 = ({ cartItems = CART_ITEMS }: ShoppingCart7Props) => { 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 formItems = form.watch("products"); const isEmpty = formItems.length === 0; return (
{isEmpty ? ( ) : ( )}
); }; const EmptyCart = () => { return ( سبد خرید شما خالی است ); }; const FullCart = ({ cartItems, form, formItems }: FullCartProps) => { 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 { fields, remove, update } = useFieldArray({ control: form.control, name: "products", }); 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, onQuantityChange, onRemoveClick, price, quantity, }: CartItemProps) => { const { regular, currency } = price; return (
{name}
{name} {details && (
    {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 = ({ index, onQuantityChange, }: { index: number; onQuantityChange: (n: number) => void; }) => { const { control } = useFormContext(); return ( { return ( { field.onChange(newQty); onQuantityChange(newQty); }} className="rounded-none border-none !shadow-none" /> ); }} /> ); }; export { ShoppingCart7 };