From 7273686107b9e92ea725662e7c5ff53abef44860 Mon Sep 17 00:00:00 2001
From: TakahashiNg <83152264+TakahashiNguyen@users.noreply.github.com>
Date: Wed, 13 May 2026 08:50:46 +0000
Subject: [PATCH] chore: update
---
app/(main)/payment/page.tsx | 8 +-
.../molecules/cards/PaymentSummaryCard.tsx | 4 +-
components/molecules/cards/ProductCard.tsx | 2 +-
.../organisms/product-grid/ProductGrid.tsx | 26 +-
lib/cart-context.tsx | 226 +++++++++++-------
lib/types.ts | 19 +-
6 files changed, 181 insertions(+), 104 deletions(-)
diff --git a/app/(main)/payment/page.tsx b/app/(main)/payment/page.tsx
index 0888779..82b1634 100644
--- a/app/(main)/payment/page.tsx
+++ b/app/(main)/payment/page.tsx
@@ -9,8 +9,8 @@ import { useManager } from "@/lib/manager-context";
import { MenuItemEntity } from "@/lib/types";
import { useState } from "react";
-const formatPrice = (value: number) =>
- value.toLocaleString("vi-VN", { style: "currency", currency: "VND" });
+export const formatPrice = (value?: number) =>
+ (value ?? 0).toLocaleString("vi-VN", { style: "currency", currency: "VND" });
export default function PaymentPage() {
const {
@@ -42,7 +42,7 @@ export default function PaymentPage() {
- {items.length === 0 ? (
+ {items?.length === 0 ? (
Chưa có sản phẩm nào trong giỏ hàng.
@@ -72,7 +72,7 @@ export default function PaymentPage() {
- {items.map(
+ {items?.map(
({
productId: id,
priceAtTimeOfAdding: price,
diff --git a/components/molecules/cards/PaymentSummaryCard.tsx b/components/molecules/cards/PaymentSummaryCard.tsx
index 68923ce..1ce88f6 100644
--- a/components/molecules/cards/PaymentSummaryCard.tsx
+++ b/components/molecules/cards/PaymentSummaryCard.tsx
@@ -1,3 +1,4 @@
+import { formatPrice } from "@/app/(main)/payment/page";
import Button from "@/components/atoms/buttons/Button";
import { ReviewModal } from "@/components/organisms/modals";
import Link from "next/link";
@@ -5,9 +6,6 @@ import { useState } from "react";
import type { PaymentSummaryCardProps } from "./Card.types";
-const formatPrice = (value: number) =>
- value.toLocaleString("vi-VN", { style: "currency", currency: "VND" });
-
export default function PaymentSummaryCard({
totalPrice,
isCustomer = false,
diff --git a/components/molecules/cards/ProductCard.tsx b/components/molecules/cards/ProductCard.tsx
index de0a79d..f18b27a 100644
--- a/components/molecules/cards/ProductCard.tsx
+++ b/components/molecules/cards/ProductCard.tsx
@@ -38,7 +38,7 @@ export default function ProductCard({
{/* Product image */}
0 ? (
- {filteredProducts.map((product) => (
-
addToCart(product)}
- />
- ))}
+ {filteredProducts.map(
+ ({ id, imageUrl, name, price, description }) => (
+ addToCart({ productId: id!, quantity: 1 })}
+ />
+ ),
+ )}
) : (
/* Empty state */
diff --git a/lib/cart-context.tsx b/lib/cart-context.tsx
index e75fcc4..a46fd57 100644
--- a/lib/cart-context.tsx
+++ b/lib/cart-context.tsx
@@ -1,131 +1,193 @@
"use client";
+import { gql } from "@apollo/client";
+import { useMutation, useQuery } from "@apollo/client/react";
import { createContext, useContext, useEffect, useMemo, useState } from "react";
-import { CartItemEntity, MenuItemEntity } from "./types";
+import { cartClient, eateryClient } from "./apollo-clients";
+import {
+ CartEntity,
+ CartItemEntity,
+ addMenuItemMutation,
+ allEateriesQuery,
+ createCartMutation,
+ getCartQuery,
+} from "./types";
interface CartContextValue {
items: CartItemEntity[];
totalItems: number;
totalPrice: number;
- addToCart: (product: MenuItemEntity) => void;
+ addToCart: (product: CartItemEntity) => void;
increaseQty: (id: string) => void;
decreaseQty: (id: string) => void;
removeFromCart: (id: string) => void;
setQuantity: (id: string, quantity: number) => void;
}
-const STORAGE_KEY = "coffee-shop-cart";
+const CART_ID = "cartId";
const CartContext = createContext(null);
+const GET_CART_ITEMS = gql`
+ query getCart($cartId: String!) {
+ getCart(cartId: $cartId) {
+ Id
+ userId
+ eateryId
+ items {
+ productId
+ quantity
+ priceAtTimeOfAdding
+ subTotal
+ }
+ totalAmount
+ paymentQrUrl
+ }
+ }
+`;
+
+const GET_EATERY = gql`
+ query GetEateryMenu {
+ allEateries {
+ id
+ }
+ }
+`;
+
+const CREATE_CART = gql`
+ mutation createCart($eateryId: String!) {
+ createCart(eateryId: $eateryId)
+ }
+`;
+
+const ADD_ITEM = gql`
+ mutation addItem(
+ $cartId: String!
+ $menuItemId: String!
+ $quantity: BigInteger!
+ ) {
+ addItem(cartId: $cartId, menuItemId: $menuItemId, quantity: $quantity) {
+ Id
+ userId
+ eateryId
+ items {
+ productId
+ quantity
+ priceAtTimeOfAdding
+ subTotal
+ }
+ totalAmount
+ paymentQrUrl
+ }
+ }
+`;
+
export function CartProvider({ children }: { children: React.ReactNode }) {
- const [items, setItems] = useState([]);
+ const [cart, setCart] = useState(null!);
+ const [cartId, setCartId] = useState(null);
+
+ const { data: eateryData } = useQuery(GET_EATERY, {
+ client: eateryClient,
+ });
+
+ const [createCart] = useMutation(CREATE_CART, {
+ client: cartClient,
+ });
+
+ const { data, loading, error } = useQuery(GET_CART_ITEMS, {
+ client: cartClient,
+ variables: { cartId },
+ skip: !cartId,
+ });
+
+ const [addMenuItem] = useMutation(ADD_ITEM, {
+ client: cartClient,
+ });
useEffect(() => {
- try {
- const raw = localStorage.getItem(STORAGE_KEY);
- if (!raw) return;
- const parsed = JSON.parse(raw) as CartItemEntity[];
- if (Array.isArray(parsed)) {
- setItems(parsed.filter((i) => i && i.productId && i.quantity > 0));
+ const createCartFunc = async () => {
+ if (eateryData && eateryData.allEateries?.length > 0) {
+ try {
+ const firstEateryId = eateryData.allEateries[0].id;
+ const { data: mutationResult } = await createCart({
+ variables: { eateryId: firstEateryId },
+ });
+
+ const newCartId = mutationResult!.createCart;
+ if (newCartId) {
+ localStorage.setItem(CART_ID, newCartId);
+ setCartId(newCartId);
+ }
+ } catch (err) {
+ console.error("Lỗi khi tạo giỏ hàng:", err);
+ }
}
- } catch {
- localStorage.removeItem(STORAGE_KEY);
+ };
+
+ if (error) createCartFunc();
+ else {
+ const localCartId = localStorage.getItem(CART_ID);
+ if (localCartId) setCartId(localCartId);
}
- }, []);
+ }, [eateryData, createCart, data, loading]);
useEffect(() => {
- localStorage.setItem(STORAGE_KEY, JSON.stringify(items));
- }, [items]);
+ if (data?.getCart) setCart(data.getCart);
+ }, [data]);
- const addToCart = (product: MenuItemEntity) => {
- setItems((prev) => {
- const index = prev.findIndex((i) => i.productId === product.id);
- if (index === -1) {
- return [
- ...prev,
- {
- productId: product.id!,
- name: product.name,
- description: product.description,
- priceAtTimeOfAdding: product.price,
- quantity: 1,
- },
- ];
- }
+ const addToCart = async (product: CartItemEntity) => {
+ if (!cartId) return;
- const next = [...prev];
- next[index] = { ...next[index], quantity: next[index].quantity + 1 };
- return next;
+ const { data: result } = await addMenuItem({
+ variables: {
+ cartId,
+ menuItemId: product.productId!,
+ quantity: product.quantity,
+ },
});
+
+ if (result) setCart(result.addItem);
};
- const increaseQty = (id: string) => {
- setItems((prev) =>
- prev.map((item) =>
- item.productId === id ? { ...item, quantity: item.quantity + 1 } : item,
- ),
- );
+ const setQuantity = async (id: string, quantity: number) => {
+ if (!cartId) return;
+
+ const { data: result } = await addMenuItem({
+ variables: {
+ cartId,
+ menuItemId: id,
+ quantity,
+ },
+ });
+
+ if (result) setCart(result.addItem);
};
- const decreaseQty = (id: string) => {
- setItems((prev) =>
- prev
- .map((item) =>
- item.productId === id
- ? { ...item, quantity: Math.max(0, item.quantity - 1) }
- : item,
- )
- .filter((item) => item.quantity > 0),
- );
- };
+ const removeFromCart = (id: string) => setQuantity(id, 0);
- const removeFromCart = (id: string) => {
- setItems((prev) => prev.filter((item) => item.productId !== id));
- };
+ const increaseQty = (id: string) =>
+ setQuantity(id, cart.items.find((i) => i.productId == id)!.quantity + 1);
- const setQuantity = (id: string, quantity: number) => {
- const safeQty = Number.isFinite(quantity)
- ? Math.max(0, Math.floor(quantity))
- : 0;
- if (safeQty === 0) {
- removeFromCart(id);
- return;
- }
-
- setItems((prev) =>
- prev.map((item) =>
- item.productId === id ? { ...item, quantity: safeQty } : item,
- ),
- );
- };
+ const decreaseQty = (id: string) =>
+ setQuantity(id, cart.items.find((i) => i.productId == id)!.quantity - 1);
const totalItems = useMemo(
- () => items.reduce((sum, item) => sum + item.quantity, 0),
- [items],
- );
-
- const totalPrice = useMemo(
- () =>
- items.reduce(
- (sum, item) => sum + item.priceAtTimeOfAdding * item.quantity,
- 0,
- ),
- [items],
+ () => cart?.items.reduce((sum, item) => sum + item.quantity, 0),
+ [cart],
);
const value = useMemo(
() => ({
- items,
+ items: cart?.items,
totalItems,
- totalPrice,
+ totalPrice: cart?.totalAmount,
addToCart,
increaseQty,
decreaseQty,
removeFromCart,
setQuantity,
}),
- [items, totalItems, totalPrice],
+ [cart?.items, cart?.totalAmount],
);
return {children};
diff --git a/lib/types.ts b/lib/types.ts
index a7121c7..1730227 100644
--- a/lib/types.ts
+++ b/lib/types.ts
@@ -125,6 +125,7 @@ export interface MenuItemEntity {
export interface ShiftEntity {}
export interface EateryEntity {
+ id: string;
ownerId: string;
name: string;
menuItems: MenuItemEntity[];
@@ -150,10 +151,24 @@ export interface deleteMenuItemMutation {
export interface CartItemEntity {
productId: string;
quantity: number;
- priceAtTimeOfAdding: number;
+ priceAtTimeOfAdding?: number;
}
export interface CartEntity {
Id: string;
items: CartItemEntity[];
-}
\ No newline at end of file
+ totalAmount: number;
+ paymentQrUrl: string;
+}
+
+export interface getCartQuery {
+ getCart: CartEntity;
+}
+
+export interface createCartMutation {
+ createCart: string;
+}
+
+export interface addMenuItemMutation {
+ addItem: CartEntity;
+}