-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcart-items.tsx
118 lines (109 loc) · 4.18 KB
/
cart-items.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
'use client'
import Image from 'next/image'
import Link from 'next/link'
import { urlForImage } from '../sanity/lib/image'
import { Clock, X } from 'lucide-react'
import { formatCurrencyString, useShoppingCart } from 'use-shopping-cart'
import { Product } from 'use-shopping-cart/core'
import { shimmer, toBase64 } from '../lib/image'
import { getSizeName } from '../lib/utils'
import { Button } from '../components/ui/button'
import { Input } from '../components/ui/input'
import { useToast } from '../components/ui/use-toast'
import { CartItemsEmpty } from '../components/cart-items-empty'
export function CartItems() {
const { cartDetails, removeItem, setItemQuantity } = useShoppingCart()
const cartItems = Object.entries(cartDetails!).map(([_, product]) => product)
const { toast } = useToast()
function removeCartItem(product: Product) {
removeItem(product._id)
toast({
title: `${product.name} - Removido`,
description: 'Produto retirado da sacola de compras',
variant: 'destructive',
})
}
if (cartItems.length === 0) return <CartItemsEmpty />
return (
<ul
role="list"
className="divide-y divide-gray-200 border-y border-gray-200 dark:divide-gray-500 dark:border-gray-500"
>
{cartItems.map((product, productIdx) => (
<li key={product._id} className="flex py-6 sm:py-10">
<div className="shrink-0">
<Image
src={urlForImage(product.images[0]).url()}
width={200}
height={200}
alt={product.name}
placeholder="blur"
blurDataURL={`data:image/svg+xml;base64,${toBase64(
shimmer(200, 200),
)}`}
className="h-24 w-24 rounded-md border-2 border-gray-200 object-cover object-center dark:border-gray-800 sm:h-48 sm:w-48"
/>
</div>
<div className="ml-4 flex flex-1 flex-col justify-between sm:ml-6">
<div className="relative justify-between pr-9 sm:flex sm:gap-x-6 sm:pr-0">
<div>
<div className="flex justify-between">
<h3 className="text-sm">
<Link
href={`/products/${product.slug}`}
className="font-medium"
>
{product.name}
</Link>
</h3>
</div>
<p className="mt-1 text-sm font-medium">
{formatCurrencyString({
value: product.price,
currency: product.currency,
})}
</p>
<p className="mt-1 text-sm font-medium">
Tamanho: {/* @ts-ignore */}
<strong>{getSizeName(product.product_data?.size)}</strong>
</p>
</div>
<div className="mt-4 sm:mt-0 sm:pr-9">
<label htmlFor={`quantity-${productIdx}`} className="sr-only">
Quantidade, {product.name}
</label>
<Input
id={`quantity-${productIdx}`}
name={`quantity-${productIdx}`}
type="number"
className="w-16"
min={1}
max={10}
value={product.quantity}
onChange={(event) =>
setItemQuantity(product._id, Number(event.target.value))
}
/>
<div className="absolute right-0 top-0">
<Button
variant="ghost"
type="button"
className="-mr-2 inline-flex p-2"
onClick={() => removeCartItem(product)}
>
<span className="sr-only">Remove</span>
<X className="h-5 w-5" aria-hidden="true" />
</Button>
</div>
</div>
</div>
<p className="mt-4 flex space-x-2 text-sm">
<Clock className="h-5 w-5 shrink-0" aria-hidden="true" />
<span>Envios em até 7 dias</span>
</p>
</div>
</li>
))}
</ul>
)
}