From 62e6fd470caf794de65be4bf58d53cb41ae93e6b Mon Sep 17 00:00:00 2001 From: Aneeq Asghar <90673129+aneeqasghar@users.noreply.github.com> Date: Sun, 3 Mar 2024 02:21:49 +0500 Subject: [PATCH] trying to make the cart functional --- src/app/(lobby)/build-a-board/page.tsx | 54 +++++++++++++++++++++++++- src/app/(lobby)/page.tsx | 12 ++++-- src/app/_actions/cart.ts | 14 +++---- src/components/board-builder.tsx | 19 +++++++-- 4 files changed, 84 insertions(+), 15 deletions(-) diff --git a/src/app/(lobby)/build-a-board/page.tsx b/src/app/(lobby)/build-a-board/page.tsx index fac5fcc6f..91eb8880f 100644 --- a/src/app/(lobby)/build-a-board/page.tsx +++ b/src/app/(lobby)/build-a-board/page.tsx @@ -2,7 +2,7 @@ import { type Metadata } from "next" import { cookies } from "next/headers" import Link from "next/link" import { db } from "@/db" -import { carts } from "@/db/schema" +import { carts, products, stores } from "@/db/schema" import { eq } from "drizzle-orm" import { productCategories } from "@/config/products" @@ -13,6 +13,7 @@ import { Icons } from "@/components/icons" import { Shell } from "@/components/shell" import { getCartItemsAction } from "@/app/_actions/cart" import { getProductsAction } from "@/app/_actions/product" +import { desc } from "drizzle-orm" export const metadata: Metadata = { title: "Build a Board", @@ -24,6 +25,14 @@ interface BuildABoadPageProps { [key: string]: string | string[] | undefined } } +import { + Card, + CardContent, + CardDescription, + CardHeader, + CardTitle, +} from "@/components/ui/card" +import { ProductCard } from "@/components/product-card" export default async function BuildABoardPage({ searchParams, @@ -49,6 +58,17 @@ export default async function BuildABoardPage({ // Get cart items const cartItems = await getCartItemsAction() + const allProducts = await db + .select() + .from(products) + .limit(8) + .orderBy(desc(products.createdAt)) + + const allStores = await db + .select() + .from(stores) + .limit(4) + .orderBy(desc(stores.createdAt)) return (
{cartItems @@ -86,11 +106,41 @@ export default async function BuildABoardPage({ + {allProducts.map((product) => ( + + ))} + {allStores.map((store) => ( + + + {store.name} + + {store.description} + + + + + +
+ View products + {`${store.name} store products`} +
+ +
+
+ ))} ) diff --git a/src/app/(lobby)/page.tsx b/src/app/(lobby)/page.tsx index c6ccb6c9b..dd9343857 100644 --- a/src/app/(lobby)/page.tsx +++ b/src/app/(lobby)/page.tsx @@ -20,6 +20,7 @@ import { Header } from "@/components/header" import { Hero } from "@/components/hero" import { ProductCard } from "@/components/product-card" import { Shell } from "@/components/shell" +import { BoardBuilder } from "@/components/board-builder" export const runtime = "edge" @@ -173,9 +174,8 @@ export default async function IndexPage() { ]?.subcategories.map((subcategory) => ( {subcategory.title} @@ -184,6 +184,12 @@ export default async function IndexPage() { ))} + {/* */} ) diff --git a/src/app/_actions/cart.ts b/src/app/_actions/cart.ts index 0026b57d5..110f56c48 100644 --- a/src/app/_actions/cart.ts +++ b/src/app/_actions/cart.ts @@ -66,14 +66,14 @@ export async function getCartItemsAction() { // throw new Error("Invalid cartId, please try again.") // } - if (!cartId) { - const cart = await db.insert(carts).values({ - items: [], - }) + // if (!cartId) { + // const cart = await db.insert(carts).values({ + // items: [], + // }) - // Convert to string because cookieStore.set() only accepts string values - cookieStore.set("cartId", String(cart.insertId)) - } + // // Convert to string because cookieStore.set() only accepts string values + // cookieStore.set("cartId", String(cart.insertId)) + // } const cart = await db.query.carts.findFirst({ where: eq(carts.id, cartId), diff --git a/src/components/board-builder.tsx b/src/components/board-builder.tsx index 1ae1bd473..efd39b3fc 100644 --- a/src/components/board-builder.tsx +++ b/src/components/board-builder.tsx @@ -2,9 +2,11 @@ import * as React from "react" import { usePathname, useRouter, useSearchParams } from "next/navigation" -import { type Product } from "@/db/schema" +import { productsRelations, type Product,products as PR } from "@/db/schema" import type { CartItem } from "@/types" import { toast } from "sonner" +// import { carts, products, stores } from "@/db/schema" +import { db } from "@/db" import { sortOptions } from "@/config/products" import { cn } from "@/lib/utils" @@ -33,25 +35,35 @@ import { Icons } from "@/components/icons" import { PaginationButton } from "@/components/pagination-button" import { ProductCard } from "@/components/product-card" import { addToCartAction, deleteCartItemAction } from "@/app/_actions/cart" +import { desc } from "drizzle-orm" interface BoardBuilderProps { products: Product[] pageCount: number subcategory: string | null cartItems: CartItem[] + app?: any } -export function BoardBuilder({ +export function BoardBuilder({ products, pageCount, subcategory, cartItems, + app }: BoardBuilderProps) { const router = useRouter() const pathname = usePathname() const searchParams = useSearchParams() const [isPending, startTransition] = React.useTransition() + + // const allProducts = await db + // .select() + // .from(app) + // .limit(8) + // .orderBy(desc(app.createdAt)) + // Search params const page = searchParams?.get("page") ?? "1" const per_page = searchParams?.get("per_page") ?? "8" @@ -79,6 +91,7 @@ export function BoardBuilder({ const [priceRange, setPriceRange] = React.useState<[number, number]>([0, 100]) const debouncedPrice = useDebounce(priceRange, 500) + React.useEffect(() => { const [min, max] = debouncedPrice startTransition(() => { @@ -254,7 +267,7 @@ export function BoardBuilder({ ) : null}
- {products.map((product) => ( + {app?.map((product) => (