diff --git a/src/app/(category-sidebar)/products/[category]/[subcategory]/[product]/page.tsx b/src/app/(category-sidebar)/products/[category]/[subcategory]/[product]/page.tsx index bed866b..2f1d110 100644 --- a/src/app/(category-sidebar)/products/[category]/[subcategory]/[product]/page.tsx +++ b/src/app/(category-sidebar)/products/[category]/[subcategory]/[product]/page.tsx @@ -16,7 +16,6 @@ export default async function Page(props: { const urlDecodedProduct = decodeURIComponent(product); const urlDecodedSubcategory = decodeURIComponent(subcategory); const urlDecodedCategory = decodeURIComponent(category); - console.log(urlDecodedProduct); const productData = await db.query.products.findFirst({ where: (products, { eq }) => eq(products.slug, urlDecodedProduct), }); diff --git a/src/app/layout.tsx b/src/app/layout.tsx index a9ed6c9..a86b218 100644 --- a/src/app/layout.tsx +++ b/src/app/layout.tsx @@ -30,7 +30,6 @@ export default async function RootLayout({ children: React.ReactNode; }>) { const cart = await getCart(); - console.log(cart); return ( diff --git a/src/app/order/page.tsx b/src/app/order/page.tsx index 073266a..7d7e5de 100644 --- a/src/app/order/page.tsx +++ b/src/app/order/page.tsx @@ -4,9 +4,31 @@ import { X } from "lucide-react"; import type { CartItem } from "@/lib/cart"; import Image from "next/image"; import { db } from "@/db"; +import { removeFromCart } from "@/lib/actions"; +import { products } from "@/db/schema"; +import { inArray } from "drizzle-orm"; +import Link from "next/link"; export default async function Page() { const cart = await getCart(); + const dbProducts = await db + .select() + .from(products) + .where( + inArray( + products.slug, + cart.map((item) => item.productSlug), + ), + ); + + const totalCost = cart.reduce( + (acc, item) => + acc + + item.quantity * + (Number(dbProducts.find((p) => p.slug === item.productSlug)?.price) ?? + 0), + 0, + ); return (
@@ -25,7 +47,7 @@ export default async function Page() {
)} {cart.length > 0 ? ( -
+
{cart.map((item) => ( ))} @@ -37,13 +59,7 @@ export default async function Page() {
-

- Merchandise $ - {/* {cart.reduce( - (acc, item) => acc + item.quantity * item.price, - 0, - )} */} -

+

Merchandise ${totalCost}

Applicable shipping and tax will be added.

@@ -62,30 +78,54 @@ export default async function Page() { async function CartItem({ item }: { item: CartItem }) { const product = await db.query.products.findFirst({ where: (products, { eq }) => eq(products.slug, item.productSlug), + with: { + subcategory: { + with: { + subcollection: true, + }, + }, + }, }); if (!product) { return null; } return (
-
- Product -
-
-

{product.name}

-

{product.description}

-
-
- -
+ +
+
+ Product +
+
+

{product.name}

+

{product.description}

+
+
+ +
+
+

{item.quantity}

+

${product.price} each

-
+

${Number(product.price) * item.quantity}

-
+
+ +
); diff --git a/src/lib/actions.ts b/src/lib/actions.ts index 0a2c88b..a67c99e 100644 --- a/src/lib/actions.ts +++ b/src/lib/actions.ts @@ -44,6 +44,22 @@ export async function addToCart(prevState: unknown, formData: FormData) { return "Item added to cart"; } +export async function removeFromCart(formData: FormData) { + const prevCart = await getCart(); + const productSlug = formData.get("productSlug"); + if (typeof productSlug !== "string") { + return; + } + const itemAlreadyExists = prevCart.find( + (item) => item.productSlug === productSlug, + ); + if (!itemAlreadyExists) { + return; + } + const newCart = prevCart.filter((item) => item.productSlug !== productSlug); + await updateCart(newCart); +} + export async function searchProducts(searchTerm: string) { const results = await db .select() diff --git a/todo.md b/todo.md index b2391a4..9e7d4f9 100644 --- a/todo.md +++ b/todo.md @@ -14,4 +14,5 @@ - [ ] link to v0 chats - [ ] Profile against real site? - [x] search (postgress fts) + - [ ] partial matches - [ ] Ensure responsive \ No newline at end of file