Skip to content

Commit 5b1667d

Browse files
author
david emioma
committed
Cart system update
1 parent 301c671 commit 5b1667d

File tree

4 files changed

+34
-35
lines changed

4 files changed

+34
-35
lines changed

actions/cart.ts

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,32 +25,15 @@ export const addToCartHandler = async (values: CartItemValidator) => {
2525

2626
const { productId, productItemId, availableItemId } = validatedBody;
2727

28-
//Check if product exists
29-
const productExists = await prismadb.product.findUnique({
30-
where: {
31-
id: productId,
32-
},
33-
});
34-
35-
if (!productExists) {
36-
throw new Error("Product with provided ID does not exist.");
37-
}
38-
39-
//Check if product item exists
40-
const productItemExists = await prismadb.productItem.findUnique({
41-
where: {
42-
id: productItemId,
43-
},
44-
});
45-
46-
if (!productItemExists) {
47-
throw new Error("Product item with provided ID does not exist.");
48-
}
49-
5028
//Check if available Item exists
5129
const availableItemExists = await prismadb.available.findUnique({
5230
where: {
5331
id: availableItemId,
32+
productId,
33+
productItemId,
34+
},
35+
select: {
36+
id: true,
5437
},
5538
});
5639

components/cart/CartItem.tsx

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use client";
22

3-
import React from "react";
3+
import React, { useState } from "react";
44
import Image from "next/image";
55
import { toast } from "sonner";
66
import { Button } from "../ui/button";
@@ -21,12 +21,18 @@ const CartItem = ({ cartItem, isCheckout, index }: Props) => {
2121

2222
const queryClient = useQueryClient();
2323

24+
const [quantity, setQuantity] = useState(cartItem.quantity || 0);
25+
26+
const [isChangeLoading, setIsChangeLoading] = useState(false);
27+
2428
const { mutate: removeItem, isPending } = useMutation({
2529
mutationKey: ["remove-cart-item"],
2630
mutationFn: deleteCartItem,
2731
onSuccess: () => {
2832
toast.success("Item removed from cart.");
2933

34+
setQuantity(0);
35+
3036
queryClient.invalidateQueries({
3137
queryKey: ["get-cart-item"],
3238
});
@@ -41,6 +47,8 @@ const CartItem = ({ cartItem, isCheckout, index }: Props) => {
4147
const { mutate: onQuantityChange, isPending: isLoading } = useMutation({
4248
mutationKey: ["update-quantity", cartItem.id],
4349
mutationFn: async ({ task }: { task: "add" | "minus" }) => {
50+
setIsChangeLoading(true);
51+
4452
if (
4553
cartItem.availableItem?.numInStocks &&
4654
task === "add" &&
@@ -53,6 +61,10 @@ const CartItem = ({ cartItem, isCheckout, index }: Props) => {
5361
return;
5462
}
5563

64+
task === "add"
65+
? setQuantity((prev) => prev + 1)
66+
: setQuantity((prev) => prev - 1);
67+
5668
await updateCartItem({ cartItemId: cartItem.id, task });
5769
},
5870
onSuccess: () => {
@@ -61,9 +73,13 @@ const CartItem = ({ cartItem, isCheckout, index }: Props) => {
6173
});
6274

6375
isCheckout && router.refresh();
76+
77+
setIsChangeLoading(false);
6478
},
6579
onError: (err) => {
6680
toast.error(err.message || "Something went wrong");
81+
82+
setIsChangeLoading(false);
6783
},
6884
});
6985

@@ -111,19 +127,19 @@ const CartItem = ({ cartItem, isCheckout, index }: Props) => {
111127
<Button
112128
className="text-lg font-semibold"
113129
variant="outline"
114-
disabled={isPending || isLoading}
130+
disabled={isPending || isLoading || isChangeLoading}
115131
onClick={() => onQuantityChange({ task: "minus" })}
116132
data-cy={`cart-item-${index}-minus`}
117133
>
118134
-
119135
</Button>
120136

121-
<div className="px-3 font-semibold">{cartItem.quantity}</div>
137+
<div className="px-3 font-semibold">{quantity}</div>
122138

123139
<Button
124140
className="text-lg font-semibold"
125141
variant="outline"
126-
disabled={isPending || isLoading}
142+
disabled={isPending || isLoading || isChangeLoading}
127143
onClick={() => onQuantityChange({ task: "add" })}
128144
data-cy={`cart-item-${index}-add`}
129145
>

package-lock.json

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"@radix-ui/react-tooltip": "^1.0.7",
3939
"@react-email/components": "^0.0.14",
4040
"@react-email/render": "^0.0.12",
41-
"@tanstack/react-query": "^5.28.6",
41+
"@tanstack/react-query": "^5.40.1",
4242
"@tanstack/react-table": "^8.11.8",
4343
"@upstash/ratelimit": "^1.0.3",
4444
"@upstash/redis": "^1.29.0",

0 commit comments

Comments
 (0)