"use client"; import { zodResolver } from "@hookform/resolvers/zod"; import { CheckCircle, Info } from "lucide-react"; import { useCallback, useState } from "react"; import type { UseFormReturn } from "react-hook-form"; import { Controller, useFieldArray, useForm } from "react-hook-form"; import z from "zod"; import QuantityInput from "@/components/shadcnblocks/quantity-input"; import { Alert, AlertDescription } from "@/components/ui/alert"; import { AspectRatio } from "@/components/ui/aspect-ratio"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardFooter, CardTitle } from "@/components/ui/card"; import { Collapsible, CollapsibleContent, CollapsibleTrigger, } from "@/components/ui/collapsible"; import { Empty, EmptyContent, EmptyDescription, EmptyHeader, EmptyTitle, } from "@/components/ui/empty"; import { Field, FieldError } from "@/components/ui/field"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; const formatPrice = (price: number, currency = "IRR") => new Intl.NumberFormat("fa-IR", { style: "currency", currency, }).format(price); type UsefulLink = { text: string; link: string; }; interface ProductPrice { regular: number; sale?: number; currency: string; } type EmptyCartData = { title: string; description: string; cta: { link: string; text: string; }; }; type Option = { id: string; label: string; value: string; color?: string; }; interface OptionsProps extends Partial { currentValue?: string; } interface Variant { label: string; id: string; name: string; options?: Option[]; type: "select" | "radio"; } type EmptyCartProps = EmptyCartData; type CartItem = { product_id: string; link: string; name: string; image: string; price: ProductPrice; details?: { label: string; value: string; }[]; variants?: Partial>; sizeValue?: string; colorValue?: string; }; interface FullCartProps { cartItems: CartItem[]; formItems: CartFormType["products"]; form: UseFormReturn; usefulLinks: UsefulLink[]; } interface CartItemProps extends CartItem { index: number; form: ReturnType>; onRemoveClick: () => void; onQuantityChange: (newQty: number) => void; } interface UsefulLinksListProps { links: UsefulLink[]; } interface EditProductProps { index: number; variants?: Partial>; onCancel: () => void; currentVariant: { color?: string; size?: string; }; } 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/clothes/Minimalist-Beige-Sneakers-2.png", price: { regular: 149.0, currency: "IRR", sale: 120.0, }, details: [ { label: "سایز", value: "EU 36", }, { label: "رنگ", value: "بژ", }, ], sizeValue: "EU_36", colorValue: "product-1-beige", variants: { size: { label: "سایز", name: "size", id: "size", type: "select", options: [ { label: "EU 36", id: "product-1-EU_36", value: "EU_36", }, { label: "EU 36.5", id: "product-1-EU_36.5", value: "EU_36.5", }, { label: "EU 37", id: "product-1-EU_37", value: "EU_37", }, { label: "EU 37.5", id: "product-1-EU_37.5", value: "EU_37.5", }, { label: "EU 39", id: "product-1-EU_39", value: "EU_39", }, { label: "EU 39.5", id: "product-1-EU_39.5", value: "EU_39.5", }, { label: "EU 41", id: "product-1-EU_41", value: "EU_41", }, ], }, color: { label: "رنگ", name: "color", id: "color", type: "radio", options: [ { label: "مشکی", id: "product-1-black", value: "product-1-black", color: "#000000", }, { label: "آبی", id: "product-1-blue", value: "product-1-blue", color: "#1581BF", }, { label: "هلویی", id: "product-1-peach", value: "product-1-peach", color: "#FF5555", }, { label: "بژ", id: "product-1-beige", value: "product-1-beige", color: "#D7C097", }, ], }, }, }, { product_id: "product-2", link: "#", name: "بلوز آبی گلدوزی", image: "/block-media/shadcnblocks/block/ecommerce/clothes/Woman-in-Embroidered-Blue-Top-2.png", price: { regular: 140.0, currency: "IRR", }, details: [ { label: "سایز", value: "S", }, { label: "رنگ", value: "آبی", }, ], sizeValue: "S", colorValue: "product-2-blue", variants: { size: { label: "سایز", name: "size", id: "size", type: "select", options: [ { label: "S", id: "product-2-S", value: "S", }, { label: "M", id: "product-2-M", value: "M", }, { label: "XL", id: "product-XL", value: "XL", }, ], }, color: { label: "رنگ", name: "color", id: "color", type: "radio", options: [ { label: "مشکی", id: "product-2-black", value: "product-2-black", color: "#000000", }, { label: "آبی", id: "product-2-blue", value: "product-2-blue", color: "#1581BF", }, { label: "هلویی", id: "product-2-peach", value: "product-2-peach", color: "#FF5555", }, ], }, }, }, { product_id: "product-3", link: "#", name: "کلاه فدورا کلاسیک", image: "/block-media/shadcnblocks/block/ecommerce/accessories/Classic-Fedora-Hat-2.png", price: { regular: 84.0, currency: "IRR", }, details: [ { label: "رنگ", value: "بژ", }, ], colorValue: "product-1-beige", variants: { color: { label: "رنگ", name: "color", id: "color", type: "radio", options: [ { label: "مشکی", id: "product-3-black", value: "product-3-black", color: "#000000", }, { label: "آبی", id: "product-3-blue", value: "product-3-blue", color: "#1581BF", }, { label: "بژ", id: "product-1-beige", value: "product-1-beige", color: "#D7C097", }, ], }, }, }, ]; const USEFUL_LINKS: UsefulLink[] = [ { text: "بازگشت و تعویض", link: "#", }, { text: "ارسال به منزل", link: "#", }, { text: "گارانتی", link: "#", }, ]; const cartFormSchema = z.object({ products: z .object({ product_id: z.string(), quantity: z.number(), price: z.number(), color: z.string(), size: z.string(), }) .array(), }); type CartFormType = z.infer; const ShoppingCart15 = ({ emptyCartData = EMPTY_CART_DATA, cartItems = CART_ITEMS, usefulLinks = USEFUL_LINKS, }) => { 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, usefulLinks, }: 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)}
هزینه ارسال
{formatPrice(30.0, cartItems[0].price.currency)}
مبلغ قابل پرداخت
{formatPrice(totalPrice ?? 0, cartItems[0].price.currency)}

جمع جزء {itemsCount.toLocaleString("fa-IR")} کالا

{formatPrice(totalPrice ?? 0, cartItems[0].price.currency)}
); }; const CartItem = ({ image, name, details, index, form, onQuantityChange, onRemoveClick, price, link, variants, sizeValue, colorValue, }: CartItemProps) => { const [openEdit, setOpenEdit] = useState(false); const { regular, currency, sale } = price; return (
{name}
{sale != null && ( {formatPrice(sale, currency)} )} {formatPrice(regular, currency)}
setOpenEdit(false)} variants={variants} index={index} currentVariant={{ size: sizeValue, color: colorValue, }} />
); }; const promoCodeFormSchema = z.object({ promoCode: z.string().trim().min(3), }); type promoCodeFormType = z.infer; const PromoCode = () => { const [showCodeForm, setShowCodeForm] = useState(false); const promoForm = useForm({ resolver: zodResolver(promoCodeFormSchema), defaultValues: { promoCode: "", }, }); const onSubmit = (data: promoCodeFormType) => { console.log(data); }; const onCancel = () => { setShowCodeForm(false); }; return (
( {fieldState.invalid && ( )} )} />
); }; const UsefulLinksList = ({ links }: UsefulLinksListProps) => { if (!links) return; return (
    {links.map(({ link, text }) => (
  • ))}
); }; const ProductDetails = ({ details, }: { details?: { label: string; value: string; }[]; }) => { if (!details) return; return (
    {details?.map((item, index) => (
  • {item.label}:
    {item.value}
  • ))}
); }; const QuantityField = ({ form, index, onQuantityChange, }: { form: ReturnType>; index: number; onQuantityChange: (n: number) => void; }) => { return ( (
{ field.onChange(newQty); onQuantityChange(newQty); }} />
)} /> ); }; const EditProduct = ({ variants, onCancel, currentVariant, }: EditProductProps) => { const [variant, setVariant] = useState({ size: currentVariant.size, color: currentVariant.color, }); if (!variants) return; return (
); }; const ColorOptions = ({ options, label, currentValue, name }: OptionsProps) => { if (!options) return; return (
{options.map((option) => ( ))}
); }; const SizeOptions = ({ options, label, currentValue, name }: OptionsProps) => { if (!options) return; const currentSizeLabel = options?.filter( (item) => item.value === currentValue, )[0]?.label; return (
); }; export { ShoppingCart15 };