Skip to content
This repository was archived by the owner on Jun 2, 2025. It is now read-only.

Commit a73c5f4

Browse files
authored
Add cart functionality (#8)
* Add cart actions * Add imports for actions.ts * add constant * update constants locations * update import * cart functionality * pass in the correct id * revert breaking change * update badge * up to date w main * add remove from cart button * update to match main * remove duplicate layout * remove duplicate pages * remove commented out code
1 parent 60e8f3e commit a73c5f4

File tree

15 files changed

+395
-360
lines changed

15 files changed

+395
-360
lines changed

frontend/app/actions.ts

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
"use server";
2+
import { revalidateTag } from "next/cache";
3+
import { cookies } from "next/headers";
4+
import { TAGS } from "@/lib/constants";
25

36
type FetchQueryProps = {
47
query: string;
@@ -119,3 +122,107 @@ export async function generateSearchObjectFromLLM(text: string) {
119122
return { data };
120123
}
121124
}
125+
126+
export async function addToCart(productId: string) {
127+
const cookieStore = cookies();
128+
let cartId = cookieStore.get("cartId")?.value;
129+
if (!cartId) {
130+
cartId = Math.random().toString(36).substring(2, 15);
131+
cookies().set("cartId", cartId);
132+
}
133+
const graphqlQuery = `
134+
query addToCart($cartId: String!, $productId: String!) {
135+
addToCart(cartId: $cartId, productId: $productId)
136+
}
137+
`;
138+
139+
const { error, data } = await fetchQuery({
140+
query: graphqlQuery,
141+
variables: { cartId, productId },
142+
});
143+
144+
if (error) {
145+
return { error: Array.isArray(error) ? error[0] : error };
146+
} else {
147+
revalidateTag(TAGS.cart);
148+
return { data };
149+
}
150+
}
151+
152+
export async function getCart(cartId: string) {
153+
const graphqlQuery = `
154+
query getCart($cartId: String!) {
155+
getCart(cartId: $cartId) {
156+
cartId
157+
totalCartQuantity
158+
items {
159+
Product {
160+
name
161+
description
162+
stars
163+
price
164+
image
165+
}
166+
quantity
167+
cartItemID
168+
}
169+
}
170+
}
171+
`;
172+
173+
const { error, data } = await fetchQuery({
174+
query: graphqlQuery,
175+
variables: { cartId },
176+
});
177+
178+
if (error) {
179+
return { error: Array.isArray(error) ? error[0] : error };
180+
} else {
181+
return { data };
182+
}
183+
}
184+
185+
export async function removeFromCart(productId: string) {
186+
const cookieStore = cookies();
187+
let cartId = cookieStore.get("cartId")?.value;
188+
189+
const graphqlQuery = `
190+
query removeFromCart($cartId: String!, $productId: String!) {
191+
removeFromCart(cartId: $cartId, productId: $productId)
192+
}
193+
`;
194+
195+
const { error, data } = await fetchQuery({
196+
query: graphqlQuery,
197+
variables: { cartId, productId },
198+
});
199+
200+
if (error) {
201+
return { error: Array.isArray(error) ? error[0] : error };
202+
} else {
203+
revalidateTag(TAGS.cart);
204+
return { data };
205+
}
206+
}
207+
208+
export async function decreaseItemQuantity(productId: string) {
209+
const cookieStore = cookies();
210+
let cartId = cookieStore.get("cartId")?.value;
211+
const graphqlQuery = `
212+
query decreaseQuantity($cartId: String!, $productId: String!) {
213+
decreaseQuantity(cartId: $cartId, productId: $productId)
214+
}
215+
`;
216+
217+
const { error, data } = await fetchQuery({
218+
query: graphqlQuery,
219+
variables: { cartId, productId },
220+
});
221+
222+
if (error) {
223+
return { error: Array.isArray(error) ? error[0] : error };
224+
} else {
225+
revalidateTag(TAGS.cart);
226+
return { data };
227+
}
228+
}

frontend/app/components/cart.tsx

Lines changed: 0 additions & 252 deletions
This file was deleted.

0 commit comments

Comments
 (0)