-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix(client): fix bug in product filter * feat(server): add to cart functionality in progress * feat(client/server): persist cart for guest and loged in user * feat(client): blunt implement of cart item * feat(client): add loading modal to add to cart action * feat(server): add channge cartItem quantity server action: * fix: fix prettier * feat: upgrade to nextjs14 to have stable server action
- Loading branch information
1 parent
fa7db66
commit 76b1af8
Showing
34 changed files
with
3,264 additions
and
2,691 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,38 @@ | ||
generator client { | ||
provider = "prisma-client-js" | ||
} | ||
|
||
datasource db { | ||
provider = "postgresql" | ||
url = env("POSTGRES_PRISMA_URL") | ||
provider = "postgresql" | ||
url = env("POSTGRES_PRISMA_URL") | ||
directUrl = env("POSTGRES_URL_NON_POOLING") | ||
} | ||
|
||
model User { | ||
id String @id @default(uuid()) | ||
name String | ||
firstName String | ||
lastName String | ||
email String @unique | ||
password String | ||
phoneNumber String @unique | ||
prefix String | ||
createdAt DateTime @default(now()) | ||
} | ||
id String @id @default(uuid()) | ||
name String | ||
firstName String | ||
lastName String | ||
email String @unique | ||
password String | ||
phoneNumber String @unique | ||
prefix String | ||
createdAt DateTime @default(now()) | ||
cart CartItem[] | ||
} | ||
|
||
model CartItem { | ||
id String @id @default(uuid()) | ||
name String | ||
variantName String | ||
variantSlug String | ||
variantRef String | ||
sku String | ||
size String | ||
price Int | ||
media String | ||
quantity Int @default(1) | ||
createdAt DateTime @default(now()) | ||
User User? @relation(fields: [userId], references: [id]) | ||
userId String? | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
"use server"; | ||
|
||
import prisma from "@/lib/prisma"; | ||
import { CartItem } from "@prisma/client"; | ||
import { getServerSession } from "next-auth/next"; | ||
import { options } from "@/app/api/auth/[...nextauth]/options"; | ||
|
||
export async function addToUserCart( | ||
cartItemData: Omit<CartItem, "id">, | ||
): Promise<CartItem> { | ||
const session = await getServerSession(options); | ||
if (session?.user?.email) { | ||
const user = await prisma.user.findUnique({ | ||
where: { email: session.user.email }, | ||
}); | ||
if (user) { | ||
const cartItem = await prisma.cartItem.create({ | ||
data: { | ||
...cartItemData, | ||
userId: user.id, | ||
}, | ||
}); | ||
console.log(cartItem); | ||
return cartItem; | ||
} else { | ||
throw new Error("User not found."); | ||
} | ||
} | ||
throw new Error("Not authenticated"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
"use server"; | ||
|
||
import prisma from "@/lib/prisma"; | ||
import { getServerSession } from "next-auth/next"; | ||
import { options } from "@/app/api/auth/[...nextauth]/options"; | ||
|
||
export async function changeCartItemQuantity( | ||
cartItemId: string, | ||
delta: number, | ||
) { | ||
const session = await getServerSession(options); | ||
|
||
if (session?.user?.email) { | ||
const user = await prisma.user.findUnique({ | ||
where: { email: session.user.email }, | ||
}); | ||
|
||
if (user) { | ||
const cartItem = await prisma.cartItem.findFirst({ | ||
where: { id: cartItemId }, | ||
}); | ||
|
||
if (!cartItem) { | ||
// should send message to client and not throw ? | ||
throw new Error("User not found."); | ||
} | ||
|
||
const updatedCartItem = await prisma.cartItem.update({ | ||
where: { | ||
id: cartItemId, | ||
}, | ||
data: { | ||
quantity: Math.max(cartItem.quantity + delta, 0), | ||
}, | ||
}); | ||
return updatedCartItem; | ||
} else { | ||
throw new Error("User not found."); | ||
} | ||
} | ||
throw new Error("Not authenticated"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
"use server"; | ||
|
||
import { CartItem } from "@prisma/client"; | ||
import { getServerSession } from "next-auth/next"; | ||
import { options } from "@/app/api/auth/[...nextauth]/options"; | ||
import prisma from "@/lib/prisma"; | ||
|
||
export async function getCart(): Promise<CartItem[] | undefined> { | ||
const session = await getServerSession(options); | ||
|
||
if (session?.user?.email) { | ||
const userWithCart = await prisma.user.findFirst({ | ||
where: { email: session.user.email }, | ||
select: { cart: { orderBy: { createdAt: "desc" } } }, | ||
}); | ||
|
||
if (userWithCart && userWithCart.cart) { | ||
return userWithCart.cart; | ||
} | ||
} else { | ||
// handle this error | ||
console.log("User not found!"); | ||
} | ||
|
||
return []; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import React from "react"; | ||
import { CartOverview } from "@/components/CartOverview"; | ||
|
||
export default function page() { | ||
return <>CART</>; | ||
return <CartOverview />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.