Skip to content

Commit

Permalink
Cart system update
Browse files Browse the repository at this point in the history
  • Loading branch information
david emioma committed Jun 11, 2024
1 parent 301c671 commit 5b1667d
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 35 deletions.
27 changes: 5 additions & 22 deletions actions/cart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,15 @@ export const addToCartHandler = async (values: CartItemValidator) => {

const { productId, productItemId, availableItemId } = validatedBody;

//Check if product exists
const productExists = await prismadb.product.findUnique({
where: {
id: productId,
},
});

if (!productExists) {
throw new Error("Product with provided ID does not exist.");
}

//Check if product item exists
const productItemExists = await prismadb.productItem.findUnique({
where: {
id: productItemId,
},
});

if (!productItemExists) {
throw new Error("Product item with provided ID does not exist.");
}

//Check if available Item exists
const availableItemExists = await prismadb.available.findUnique({
where: {
id: availableItemId,
productId,
productItemId,
},
select: {
id: true,
},
});

Expand Down
24 changes: 20 additions & 4 deletions components/cart/CartItem.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use client";

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

const queryClient = useQueryClient();

const [quantity, setQuantity] = useState(cartItem.quantity || 0);

const [isChangeLoading, setIsChangeLoading] = useState(false);

const { mutate: removeItem, isPending } = useMutation({
mutationKey: ["remove-cart-item"],
mutationFn: deleteCartItem,
onSuccess: () => {
toast.success("Item removed from cart.");

setQuantity(0);

queryClient.invalidateQueries({
queryKey: ["get-cart-item"],
});
Expand All @@ -41,6 +47,8 @@ const CartItem = ({ cartItem, isCheckout, index }: Props) => {
const { mutate: onQuantityChange, isPending: isLoading } = useMutation({
mutationKey: ["update-quantity", cartItem.id],
mutationFn: async ({ task }: { task: "add" | "minus" }) => {
setIsChangeLoading(true);

if (
cartItem.availableItem?.numInStocks &&
task === "add" &&
Expand All @@ -53,6 +61,10 @@ const CartItem = ({ cartItem, isCheckout, index }: Props) => {
return;
}

task === "add"
? setQuantity((prev) => prev + 1)
: setQuantity((prev) => prev - 1);

await updateCartItem({ cartItemId: cartItem.id, task });
},
onSuccess: () => {
Expand All @@ -61,9 +73,13 @@ const CartItem = ({ cartItem, isCheckout, index }: Props) => {
});

isCheckout && router.refresh();

setIsChangeLoading(false);
},
onError: (err) => {
toast.error(err.message || "Something went wrong");

setIsChangeLoading(false);
},
});

Expand Down Expand Up @@ -111,19 +127,19 @@ const CartItem = ({ cartItem, isCheckout, index }: Props) => {
<Button
className="text-lg font-semibold"
variant="outline"
disabled={isPending || isLoading}
disabled={isPending || isLoading || isChangeLoading}
onClick={() => onQuantityChange({ task: "minus" })}
data-cy={`cart-item-${index}-minus`}
>
-
</Button>

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

<Button
className="text-lg font-semibold"
variant="outline"
disabled={isPending || isLoading}
disabled={isPending || isLoading || isChangeLoading}
onClick={() => onQuantityChange({ task: "add" })}
data-cy={`cart-item-${index}-add`}
>
Expand Down
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"@radix-ui/react-tooltip": "^1.0.7",
"@react-email/components": "^0.0.14",
"@react-email/render": "^0.0.12",
"@tanstack/react-query": "^5.28.6",
"@tanstack/react-query": "^5.40.1",
"@tanstack/react-table": "^8.11.8",
"@upstash/ratelimit": "^1.0.3",
"@upstash/redis": "^1.29.0",
Expand Down

0 comments on commit 5b1667d

Please sign in to comment.