"use client"; import { zodResolver } from "@hookform/resolvers/zod"; import clsx from "clsx"; import { ChevronUp, ShoppingBag, TicketPercent, X } from "lucide-react"; import { Fragment, memo, useCallback, useState } from "react"; import { useFieldArray, useForm } 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 } from "@/components/ui/card"; import { Carousel, CarouselContent, CarouselItem, } from "@/components/ui/carousel"; import { Collapsible, CollapsibleContent, CollapsibleTrigger, } from "@/components/ui/collapsible"; import { Field } from "@/components/ui/field"; 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 CartItem = { product_id: string; link: string; name: string; image: string; price: ProductPrice; details: { label: string; value: string; }[]; }; type SuggestedProduct = { name: string; image: string; price: ProductPrice; link: string; }; interface ShoppingCart16Props { cartItems: CartItem[]; suggestedProducts: SuggestedProduct[]; } interface CartItemProps extends CartItem { index: number; form: ReturnType>; onRemoveClick: () => void; onQuantityChange: (newQty: number) => void; } interface suggestedProductsProps { products: SuggestedProduct[]; } type ProductCardProp = SuggestedProduct; interface CartItemActionsProps { index: number; form: ReturnType>; onRemoveClick: () => void; onQuantityChange: (newQty: number) => void; } 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 SUGGESTED_PRODUCTS: SuggestedProduct[] = [ { name: "تی‌شرت سفید", link: "#", image: "/block-media/shadcnblocks/block/ecommerce/clothes/Tattooed-Streetwear-Look-2.png", price: { regular: 88.0, currency: "IRR", }, }, { name: "هودی کلاسیک شهری", link: "#", image: "/block-media/shadcnblocks/block/ecommerce/clothes/Man-in-White-Hoodie-2.png", price: { regular: 499.0, currency: "IRR", }, }, { name: "هودی استریت‌لاین", link: "#", image: "/block-media/shadcnblocks/block/ecommerce/clothes/Young-Man-in-Blue-Hoodie-2.png", price: { regular: 499.0, currency: "IRR", }, }, { name: "هودی خیابانی هریتیج", link: "#", image: "/block-media/shadcnblocks/block/ecommerce/clothes/Young-Man-in-Maroon-Hoodie-2.png", price: { regular: 499.0, currency: "IRR", }, }, ]; const cartFormSchema = z.object({ products: z .object({ product_id: z.string(), quantity: z.number(), price: z.number(), }) .array(), }); type CartFormType = z.infer; const ShoppingCart16 = ({ cartItems = CART_ITEMS, suggestedProducts = SUGGESTED_PRODUCTS, }: ShoppingCart16Props) => { 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 totalPrice = formItems?.reduce( (sum, p) => sum + p.price * p.quantity, 0, ); const itemsCount = formItems && formItems.length ? formItems.length : 0; const onSubmit = (data: CartFormType) => { console.log(data); }; const handleRemove = useCallback( (index: number) => () => { remove(index); }, [remove], ); const handleQuantityChange = useCallback( (index: number) => (newQty: number) => update(index, { ...fields[index], quantity: newQty }), [update, fields], ); return ( {itemsCount === 0 && (
)} {itemsCount > 0 && ( سبد خرید ({itemsCount.toLocaleString("fa-IR")})
    {fields.map((field, index) => { return (
  • p.product_id === field.product_id, ) as CartItem)} form={form} onRemoveClick={() => handleRemove(index)()} onQuantityChange={(newQty: number) => handleQuantityChange(index)(newQty) } index={index} />
  • ); })}

FIRST10 ({formatPrice(20)} تخفیف)

هزینه ارسال
خرید بالای{" "} {formatPrice(150, cartItems[0]?.price.currency)}

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

{formatPrice( totalPrice ?? 0, cartItems[0]?.price.currency, )}
مالیات، تخفیف و هزینه ارسال در صفحه پرداخت محاسبه می‌شود
)}
); }; const CartItem = ({ image, name, link, details, price, index, form, onRemoveClick, onQuantityChange, }: CartItemProps) => { const { regular, sale, currency } = price; console.log(index); return (

{name}

    {details.map((item, index) => (
  • {item.label}:
    {item.value}
  • ))}
{sale != null && ( {formatPrice(sale, currency)} )} {formatPrice(regular, currency)}
); }; const CartItemActions = memo( ({ onRemoveClick, index, form, onQuantityChange }: CartItemActionsProps) => { return (
); }, (prev, next) => prev.onRemoveClick !== next.onRemoveClick && prev.onQuantityChange !== next.onQuantityChange, ); const QuantityField = memo( ({ form, index, onQuantityChange, }: { form: ReturnType>; index: number; onQuantityChange: (n: number) => void; }) => { return ( (
{ field.onChange(newQty); onQuantityChange(newQty); }} />
)} /> ); }, ); const ProductCard = ({ image, name, price, link }: ProductCardProp) => { const { regular, sale, currency } = price; return (
{name}
{sale != null && ( {formatPrice(sale, currency)} )} {formatPrice(regular, currency)}
); }; const SuggestedProducts = ({ products }: suggestedProductsProps) => { const [open, setOpen] = useState(true); if (!products) return; return (

پیشنهاد برای شما

{products.map((product, index) => ( ))}
); }; export { ShoppingCart16 };