-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathcart-table.tsx
166 lines (158 loc) · 4.9 KB
/
cart-table.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
'use client';
import { useRouter } from 'next/navigation';
import { useToast } from '@/hooks/use-toast';
import { useTransition } from 'react';
import { addItemToCart, removeItemFromCart } from '@/lib/actions/cart.actions';
import { ArrowRight, Loader, Minus, Plus } from 'lucide-react';
import { Cart, CartItem } from '@/types';
import Link from 'next/link';
import Image from 'next/image';
import {
Table,
TableBody,
TableHead,
TableHeader,
TableRow,
TableCell,
} from '@/components/ui/table';
import { Button } from '@/components/ui/button';
import { Card, CardContent } from '@/components/ui/card';
import { formatCurrency } from '@/lib/utils';
// NOTE: The code here has changed from the original course code so that the
// Buttons no longer share the same state and show the loader independently from
// other items in the cart
function AddButton({ item }: { item: CartItem }) {
const { toast } = useToast();
const [isPending, startTransition] = useTransition();
return (
<Button
disabled={isPending}
variant='outline'
type='button'
onClick={() =>
startTransition(async () => {
const res = await addItemToCart(item);
if (!res.success) {
toast({
variant: 'destructive',
description: res.message,
});
}
})
}
>
{isPending ? (
<Loader className='w-4 h-4 animate-spin' />
) : (
<Plus className='w-4 h-4' />
)}
</Button>
);
}
function RemoveButton({ item }: { item: CartItem }) {
const { toast } = useToast();
const [isPending, startTransition] = useTransition();
return (
<Button
disabled={isPending}
variant='outline'
type='button'
onClick={() =>
startTransition(async () => {
const res = await removeItemFromCart(item.productId);
if (!res.success) {
toast({
variant: 'destructive',
description: res.message,
});
}
})
}
>
{isPending ? (
<Loader className='w-4 h-4 animate-spin' />
) : (
<Minus className='w-4 h-4' />
)}
</Button>
);
}
const CartTable = ({ cart }: { cart?: Cart }) => {
const router = useRouter();
const [isPending, startTransition] = useTransition();
return (
<>
<h1 className='py-4 h2-bold'>Shopping Cart</h1>
{!cart || cart.items.length === 0 ? (
<div>
Cart is empty. <Link href='/'>Go Shopping</Link>
</div>
) : (
<div className='grid md:grid-cols-4 md:gap-5'>
<div className='overflow-x-auto md:col-span-3'>
<Table>
<TableHeader>
<TableRow>
<TableHead>Item</TableHead>
<TableHead className='text-center'>Quantity</TableHead>
<TableHead className='text-right'>Price</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{cart.items.map((item) => (
<TableRow key={item.slug}>
<TableCell>
<Link
href={`/product/${item.slug}`}
className='flex items-center'
>
<Image
src={item.image}
alt={item.name}
width={50}
height={50}
/>
<span className='px-2'>{item.name}</span>
</Link>
</TableCell>
<TableCell className='flex-center gap-2'>
<RemoveButton item={item} />
<span>{item.qty}</span>
<AddButton item={item} />
</TableCell>
<TableCell className='text-right'>${item.price}</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</div>
<Card>
<CardContent className='p-4 gap-4'>
<div className='pb-3 text-xl'>
Subtotal ({cart.items.reduce((a, c) => a + c.qty, 0)}):
<span className='font-bold'>
{formatCurrency(cart.itemsPrice)}
</span>
</div>
<Button
className='w-full'
disabled={isPending}
onClick={() =>
startTransition(() => router.push('/shipping-address'))
}
>
{isPending ? (
<Loader className='w-4 h-4 animate-spin' />
) : (
<ArrowRight className='w-4 h-4' />
)}{' '}
Proceed to Checkout
</Button>
</CardContent>
</Card>
</div>
)}
</>
);
};
export default CartTable;