"use client"; import { zodResolver } from "@hookform/resolvers/zod"; import clsx from "clsx"; import { CheckCircle, Heart, HeartCrack, PartyPopper, PlusCircle, X, } from "lucide-react"; import { useCallback, useState } 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 { Alert, AlertTitle } from "@/components/ui/alert"; import { AspectRatio } from "@/components/ui/aspect-ratio"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardTitle } from "@/components/ui/card"; import { Checkbox } from "@/components/ui/checkbox"; import { Empty, EmptyContent, EmptyDescription, EmptyHeader, EmptyMedia, EmptyTitle, } from "@/components/ui/empty"; import { Field, FieldError } from "@/components/ui/field"; import { InputGroup, InputGroupAddon, InputGroupButton, InputGroupInput, } from "@/components/ui/input-group"; import { Label } from "@/components/ui/label"; import { Sheet, SheetClose, SheetContent, SheetFooter, SheetHeader, SheetTitle, } from "@/components/ui/sheet"; const formatPrice = (price: number, currency = "IRR") => new Intl.NumberFormat("fa-IR", { style: "currency", currency, }).format(price); interface ProductPrice { regular: number; sale?: number; currency: string; } type EmptyCartData = { title: string; description: string; cta: { link: string; text: string; }; }; type EmptyCartProps = EmptyCartData; type CartItem = { product_id: string; link: string; name: string; image: string; price: ProductPrice; details?: { label: string; value: string; }[]; }; interface FullCartProps { cartItems: CartItem[]; formItems: CartFormType["products"]; form: UseFormReturn; } interface CartItemProps extends CartItem { index: number; onRemoveClick: () => void; onQuantityChange: (newQty: number) => void; } const EMPTY_CART_DATA = { title: "سبد خرید شما خالی است", description: "سبد خرید شما خالی است؛ همه محصولات ما را ببینید و اولین خرید خود را ثبت کنید.", cta: { link: "#", text: "مشاهده محصولات", }, }; 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 ShoppingCart17 = ({ emptyCartData = EMPTY_CART_DATA, cartItems = CART_ITEMS, }) => { const { title, description, cta } = emptyCartData; 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 = ({ title, description, cta }: EmptyCartProps) => { return ( {title} {description} ); }; 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)()} 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, }: CartItemProps) => { const { regular, currency } = price; return (
{name}
{name} {formatPrice(regular, currency)}
{details && (
    {details?.map((item, index) => { const isLast = index === details.length - 1; return (
  • {item.label}:
    {item.value}
    {!isLast && ( / )}
  • ); })}
)}
); }; const QuantityField = ({ index, onQuantityChange, }: { index: number; onQuantityChange: (n: number) => void; }) => { const { control } = useFormContext(); return ( { return ( { field.onChange(newQty); onQuantityChange(newQty); }} className="rounded-none" /> ); }} /> ); }; const promoCodeFormSchema = z.object({ promoCode: z.string().trim().min(3), }); type promoCodeFormType = z.infer; const PromoCode = () => { const [showPromoInput, setShowPromoInput] = useState(false); const promoForm = useForm({ resolver: zodResolver(promoCodeFormSchema), defaultValues: { promoCode: "", }, }); const onSubmit = (data: promoCodeFormType) => { console.log(data); }; const handlePromoCodeForm = (checked: boolean) => { setShowPromoInput(checked); }; return (
( {fieldState.invalid && } )} />
); }; export { ShoppingCart17 };