-
Notifications
You must be signed in to change notification settings - Fork 0
/
product-info.tsx
103 lines (94 loc) · 3 KB
/
product-info.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
'use client'
import { useState } from 'react'
import Link from 'next/link'
import { ArrowRight, ArrowLeft } from 'lucide-react'
import { formatCurrencyString, useShoppingCart } from 'use-shopping-cart'
import { SanityProduct } from '../config/inventory'
import { getSizeName } from '../lib/utils'
import { Button } from '../components/ui/button'
import { useToast } from '../components/ui/use-toast'
import InformationProduct from './accordion-product-info'
interface Props {
product: SanityProduct
}
export function ProductInfo({ product }: Props) {
const [selectedSize, setSelectedSize] = useState(product.sizes[0])
const { addItem, incrementItem, cartDetails } = useShoppingCart()
const { toast } = useToast()
const isInCart = !!cartDetails?.[product._id]
function addToCart() {
const item = {
...product,
product_data: {
size: selectedSize,
},
}
isInCart ? incrementItem(item._id) : addItem(item)
toast({
title: `${item.name} (${getSizeName(selectedSize)})`,
description: 'Produto adicionado na Sacola de Compras',
action: (
<Link href="/cart">
<Button variant="link" className="gap-x-2 whitespace-nowrap">
<span>Abrir</span>
<ArrowRight className="h-5 w-5" />
</Button>
</Link>
),
})
}
return (
<div className="mt-10 px-4 sm:mt-16 sm:px-0 lg:mt-0">
<div className="flex justify-between">
<h1 className="text-3xl font-bold tracking-tight">{product.name}</h1>
<Link href={'/products/'}>
<ArrowLeft className="h-5 w-5" />
</Link>
</div>
<div className="mt-3">
<h2 className="sr-only">Product information</h2>
<p className="text-3xl tracking-tight">
{formatCurrencyString({
value: product.price,
currency: product.currency,
})}
</p>
</div>
<div className="mt-6">
<h3 className="sr-only">Description</h3>
<div className="space-y-6 text-base">{product.description}</div>
</div>
<div className="mt-4">
<p>
Tamanho: <strong>{getSizeName(selectedSize)}</strong>
</p>
{product.sizes.map((size) => (
<Button
onClick={() => setSelectedSize(size)}
key={size}
variant={selectedSize === size ? 'default' : 'outline'}
className="mr-2 mt-4"
>
{getSizeName(size)}
</Button>
))}
<div className="mt-4">
<span>
<InformationProduct product={product} />
</span>
</div>
</div>
<form className="mt-6">
<div className="mt-4 flex">
<Button
onClick={addToCart}
type="button"
className="w-full bg-violet-600 py-6 text-base font-medium text-white hover:bg-violet-700 focus:outline-none focus:ring-2 focus:ring-violet-500"
>
Adicionar na Sacola
</Button>
</div>
</form>
</div>
)
}