-
Notifications
You must be signed in to change notification settings - Fork 14
/
page.tsx
239 lines (216 loc) · 8.09 KB
/
page.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
"use client";
import Image from "next/image";
import { useEffect, useState } from "react";
import { CartItem } from "@/lib/cart";
import { Input } from "@/components/ui/input";
import { getUser, purchaseItem } from "@/db/utils";
import { Button } from "@/components/ui/button";
import { User } from "@/db/schema";
import { getSession } from "next-auth/react";
import { Card, CardContent } from "@/components/ui/card";
import { ShoppingCartIcon } from "lucide-react";
/*
This is the cart page. It is responsible for displaying the items in the cart, and allowing the
user to checkout and purchase the items in the cart.
*/
export default function Cart() {
const [user, setUser] = useState<User | null>(null); // The current user
const [cartItems, setCartItems] = useState<CartItem[]>([]); // The items in the card
const [cartCheckoutLoading, setCartCheckoutLoading] =
useState<boolean>(false); // Whether the cart is currently checking out
/*
On page load, get the cart items from local storage.
*/
useEffect(() => {
/*
TODO: Get the cart items from local storage
*/
/*
TODO: Parse the cart items as a CartItem array and update the cart items state
*/
}, []);
/*
On page load, get the current user.
*/
useEffect(() => {
/*
TODO: Get the current user from the session and update the user state
*/
}, []);
/*
Dispatches an event to the window whenever the cart items change.
*/
useEffect(() => {
/*
TODO: Dispatch an event to the window whenever the cart items change.
HINT: Dispatch an event with the name "cartUpdated"
*/
}, [cartItems]);
/*
This function is called when the user clicks the checkout button. It is responsible for
purchasing the items in the cart.
*/
const onCheckout = async () => {
// TODO: If there is no user logged in, return
// TODO: Get the cart items from local storage
// TODO: Parse the cart items as a CartItem array
// TODO: Set the cart checkout loading state to true
/*
TODO: For each item in the cart, purchase the item using the purchaseItem function. Once all
items have been purchased, set the cart checkout loading state to false, empty the cart
in local storage, and redirect the user to their purchases page.
*/
};
/*
This is the cart card component. It is responsible for displaying a single item in the cart.
*/
const CartCard = ({ title, quantity, price, image, id }: CartItem) => {
return (
<div className="bg-white p-6 flex flex-row gap-6">
<Image
src={image}
alt="Background Image"
className="shrink-0"
width={160}
height={160}
/>
<div className="flex flex-col gap-3">
<p className="text-xl font-medium">{title}</p>
<div className="flex flex-row gap-6 items-center">
<div className="flex">
<div className="flex flex-row items-center border border-black/25 rounded py-2 px-3 space-x-2">
<p className="">Qty:</p>
<Input
className="w-20 h-8 bg-white border-0"
type="number"
value={quantity}
onChange={(e) => {
const index = cartItems.findIndex((item) => item.id === id);
const updatedCartItems = [
...cartItems.slice(0, index),
{
...cartItems[index],
quantity: Number(e.target.value),
},
...cartItems.slice(index + 1, cartItems.length),
];
setCartItems(updatedCartItems);
localStorage.setItem(
"cart",
JSON.stringify(updatedCartItems)
);
}}
/>
</div>
</div>
<p
className="text-blue-500 hover:underline hover:cursor-pointer"
onClick={() => {
const itemIndex = cartItems.findIndex((item) => item.id === id);
if (itemIndex !== -1) {
const deletedItemFromCart = [
...cartItems.slice(0, itemIndex),
...cartItems.slice(itemIndex + 1, cartItems.length),
];
setCartItems(deletedItemFromCart);
localStorage.setItem(
"cart",
JSON.stringify(deletedItemFromCart)
);
}
}}
>
Delete
</p>
</div>
</div>
<p className="text-xl font-medium">${(quantity * price).toLocaleString()}</p>
</div>
);
};
return (
<>
<main className="bg-[#f4f7f7] p-6 text-black h-full">
<div className="flex flex-row gap-6 items-start">
<Card className="bg-white p-6 w-9/12">
<CardContent>
<div className="flex flex-col gap-6">
<div className="text-3xl font-medium flex items-center ">
<ShoppingCartIcon className="inline mr-4 w-8 h-8" />
Shopping Cart
</div>
<div className="bg-black/25 rounded w-full h-px" />
<div className="grid grid-cols-1">
{!cartItems ||
(cartItems.length === 0 && <p>No items in cart</p>)}
{cartItems.map((item) => (
<CartCard key={item.id} {...item}></CartCard>
))}
</div>
{cartItems && cartItems.length >= 1 && (
<div className="border-t border-black/25 pt-6 flex items-end gap-2 justify-end">
<p className="text-2xl font-medium">
Subtotal ({cartItems.length} item
{cartItems && cartItems.length >= 2 ? "s" : ""}
):
</p>
<p className="text-2xl font-bold">
{" "}
$
{cartItems
.reduce<number>(
(acc, curr) => acc + curr.quantity * curr.price,
0
)
.toLocaleString()}{" "}
</p>
</div>
)}
</div>
</CardContent>
</Card>
<Card className="bg-white p-6 w-3/12 sticky top-6">
<CardContent>
<div className="flex flex-col gap-6 ">
{cartItems && cartItems.length >= 1 && (
<>
<p className="text-2xl font-medium">
Subtotal ({cartItems.length} item
{cartItems && cartItems.length >= 2 ? "s" : ""})
<span className="font-bold block">
{" "}
$
{cartItems
.reduce<number>(
(acc, curr) => acc + curr.quantity * curr.price,
0
)
.toLocaleString()}{" "}
</span>
</p>
<Button
className="w-full font-medium bg-[#FFD813] hover:bg-[#FFE813] text-black"
onClick={onCheckout}
disabled={cartCheckoutLoading || !user}
>
Checkout
</Button>
</>
)}
{!cartItems ||
(cartItems.length === 0 && (
<a
href="/"
className="flex items-center justify-center w-full font-medium bg-[#FFD813] py-3 rounded-full hover:bg-[#FFD813]"
>
Visit Shop
</a>
))}
</div>
</CardContent>
</Card>
</div>
</main>
</>
);
}