Skip to content
This repository was archived by the owner on Jun 2, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions frontend/app/actions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
"use server";
import { revalidateTag } from "next/cache";
import { cookies } from "next/headers";
import { TAGS } from "@/lib/constants";

type FetchQueryProps = {
query: string;
Expand Down Expand Up @@ -119,3 +122,107 @@ export async function generateSearchObjectFromLLM(text: string) {
return { data };
}
}

export async function addToCart(productId: string) {
const cookieStore = cookies();
let cartId = cookieStore.get("cartId")?.value;
if (!cartId) {
cartId = Math.random().toString(36).substring(2, 15);
cookies().set("cartId", cartId);
}
const graphqlQuery = `
query addToCart($cartId: String!, $productId: String!) {
addToCart(cartId: $cartId, productId: $productId)
}
`;

const { error, data } = await fetchQuery({
query: graphqlQuery,
variables: { cartId, productId },
});

if (error) {
return { error: Array.isArray(error) ? error[0] : error };
} else {
revalidateTag(TAGS.cart);
return { data };
}
}

export async function getCart(cartId: string) {
const graphqlQuery = `
query getCart($cartId: String!) {
getCart(cartId: $cartId) {
cartId
totalCartQuantity
items {
Product {
name
description
stars
price
image
}
quantity
cartItemID
}
}
}
`;

const { error, data } = await fetchQuery({
query: graphqlQuery,
variables: { cartId },
});

if (error) {
return { error: Array.isArray(error) ? error[0] : error };
} else {
return { data };
}
}

export async function removeFromCart(productId: string) {
const cookieStore = cookies();
let cartId = cookieStore.get("cartId")?.value;

const graphqlQuery = `
query removeFromCart($cartId: String!, $productId: String!) {
removeFromCart(cartId: $cartId, productId: $productId)
}
`;

const { error, data } = await fetchQuery({
query: graphqlQuery,
variables: { cartId, productId },
});

if (error) {
return { error: Array.isArray(error) ? error[0] : error };
} else {
revalidateTag(TAGS.cart);
return { data };
}
}

export async function decreaseItemQuantity(productId: string) {
const cookieStore = cookies();
let cartId = cookieStore.get("cartId")?.value;
const graphqlQuery = `
query decreaseQuantity($cartId: String!, $productId: String!) {
decreaseQuantity(cartId: $cartId, productId: $productId)
}
`;

const { error, data } = await fetchQuery({
query: graphqlQuery,
variables: { cartId, productId },
});

if (error) {
return { error: Array.isArray(error) ? error[0] : error };
} else {
revalidateTag(TAGS.cart);
return { data };
}
}
252 changes: 0 additions & 252 deletions frontend/app/components/cart.tsx

This file was deleted.

Loading