From 4f2df2cfb83ef56f7e7f53bca41c95762a2bad48 Mon Sep 17 00:00:00 2001 From: david emioma Date: Tue, 23 Apr 2024 12:57:53 +0100 Subject: [PATCH] Worked on checkout page --- app/(marketing)/checkout/page.tsx | 97 +++++++++++++++++-------------- components/CheckoutSkeleton.tsx | 40 +++++++++++++ components/cart/CartItem.tsx | 2 + 3 files changed, 96 insertions(+), 43 deletions(-) create mode 100644 components/CheckoutSkeleton.tsx diff --git a/app/(marketing)/checkout/page.tsx b/app/(marketing)/checkout/page.tsx index dffdd8a..ae09aef 100644 --- a/app/(marketing)/checkout/page.tsx +++ b/app/(marketing)/checkout/page.tsx @@ -1,16 +1,21 @@ -import prismadb from "@/lib/prisma"; +"use client"; + +import axios from "axios"; +import { CartType } from "@/types"; import Empty from "@/components/Empty"; -import { currentUser } from "@/lib/auth"; import { UserRole } from "@prisma/client"; import { redirect } from "next/navigation"; import Heading from "@/components/Heading"; import Container from "@/components/Container"; +import { useQuery } from "@tanstack/react-query"; import CartItem from "@/components/cart/CartItem"; import { Separator } from "@/components/ui/separator"; import OrderSummary from "./_components/OrderSummary"; +import useCurrentUser from "@/hooks/use-current-user"; +import CheckoutSkeleton from "@/components/CheckoutSkeleton"; -export default async function CheckoutPage() { - const { user } = await currentUser(); +export default function CheckoutPage() { + const { user } = useCurrentUser(); if (!user) { return redirect("/auth/sign-in"); @@ -20,29 +25,16 @@ export default async function CheckoutPage() { return redirect("/"); } - const cart = await prismadb.cart.findUnique({ - where: { - userId: user.id, - }, - include: { - cartItems: { - include: { - product: { - include: { - category: true, - }, - }, - productItem: true, - availableItem: { - include: { - size: true, - }, - }, - }, - orderBy: { - createdAt: "desc", - }, - }, + const { + data: cart, + isLoading, + isError, + } = useQuery({ + queryKey: ["get-cart-item"], + queryFn: async () => { + const res = await axios.get("/api/cart"); + + return res.data as CartType; }, }); @@ -52,24 +44,43 @@ export default async function CheckoutPage() { -
-
- {cart?.cartItems && cart?.cartItems?.length > 0 ? ( -
- {cart.cartItems.map((item, i) => ( - - ))} -
- ) : ( - - )} + {isError && ( + + )} + + {isLoading && ( +
+ {new Array(5).fill("").map((_, i) => ( + + ))}
+ )} + + {!isError && !isLoading && cart && ( +
+
+ {cart?.cartItems && cart?.cartItems?.length > 0 ? ( +
+ {cart.cartItems.map((item, i) => ( + + ))} +
+ ) : ( + + )} +
- -
+ +
+ )} ); } diff --git a/components/CheckoutSkeleton.tsx b/components/CheckoutSkeleton.tsx new file mode 100644 index 0000000..825cafd --- /dev/null +++ b/components/CheckoutSkeleton.tsx @@ -0,0 +1,40 @@ +"use client"; + +import React from "react"; +import { Skeleton } from "./ui/skeleton"; + +const CheckoutSkeleton = () => { + return ( +
+
+
+ + +
+ + + + + +
+
+ + +
+ +
+
+ + + + + +
+ + +
+
+ ); +}; + +export default CheckoutSkeleton; diff --git a/components/cart/CartItem.tsx b/components/cart/CartItem.tsx index 91189ae..1e62ee3 100644 --- a/components/cart/CartItem.tsx +++ b/components/cart/CartItem.tsx @@ -32,6 +32,8 @@ const CartItem = ({ cartItem, isCheckout, index }: Props) => { queryClient.invalidateQueries({ queryKey: ["get-cart-item"], }); + + isCheckout && router.refresh(); }, onError: (err) => { if (err instanceof AxiosError) {