chore: update
Release package / release (pull_request) Successful in 2m10s

This commit is contained in:
TakahashiNg
2026-05-13 08:50:46 +00:00
parent f3a7609c62
commit 7273686107
6 changed files with 181 additions and 104 deletions
+4 -4
View File
@@ -9,8 +9,8 @@ import { useManager } from "@/lib/manager-context";
import { MenuItemEntity } from "@/lib/types"; import { MenuItemEntity } from "@/lib/types";
import { useState } from "react"; import { useState } from "react";
const formatPrice = (value: number) => export const formatPrice = (value?: number) =>
value.toLocaleString("vi-VN", { style: "currency", currency: "VND" }); (value ?? 0).toLocaleString("vi-VN", { style: "currency", currency: "VND" });
export default function PaymentPage() { export default function PaymentPage() {
const { const {
@@ -42,7 +42,7 @@ export default function PaymentPage() {
</h1> </h1>
</div> </div>
{items.length === 0 ? ( {items?.length === 0 ? (
<div className="px-4 py-10 text-center text-(--color-text-muted)"> <div className="px-4 py-10 text-center text-(--color-text-muted)">
Chưa sản phẩm nào trong giỏ hàng. Chưa sản phẩm nào trong giỏ hàng.
</div> </div>
@@ -72,7 +72,7 @@ export default function PaymentPage() {
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{items.map( {items?.map(
({ ({
productId: id, productId: id,
priceAtTimeOfAdding: price, priceAtTimeOfAdding: price,
@@ -1,3 +1,4 @@
import { formatPrice } from "@/app/(main)/payment/page";
import Button from "@/components/atoms/buttons/Button"; import Button from "@/components/atoms/buttons/Button";
import { ReviewModal } from "@/components/organisms/modals"; import { ReviewModal } from "@/components/organisms/modals";
import Link from "next/link"; import Link from "next/link";
@@ -5,9 +6,6 @@ import { useState } from "react";
import type { PaymentSummaryCardProps } from "./Card.types"; import type { PaymentSummaryCardProps } from "./Card.types";
const formatPrice = (value: number) =>
value.toLocaleString("vi-VN", { style: "currency", currency: "VND" });
export default function PaymentSummaryCard({ export default function PaymentSummaryCard({
totalPrice, totalPrice,
isCustomer = false, isCustomer = false,
+1 -1
View File
@@ -38,7 +38,7 @@ export default function ProductCard({
</div> </div>
{/* Product image */} {/* Product image */}
<Image <Image
src={image} src={image || "/"}
alt={imageAlt} alt={imageAlt}
fill fill
className="z-1 object-cover" className="z-1 object-cover"
@@ -2,9 +2,9 @@
import { ProductCard } from "@/components/molecules/cards"; import { ProductCard } from "@/components/molecules/cards";
import { useCart } from "@/lib/cart-context"; import { useCart } from "@/lib/cart-context";
import { useManager } from "@/lib/manager-context";
import type { ProductGridProps } from "./ProductGrid.types"; import type { ProductGridProps } from "./ProductGrid.types";
import { useManager } from "@/lib/manager-context";
export default function ProductGrid({ export default function ProductGrid({
searchQuery = "", searchQuery = "",
@@ -31,17 +31,19 @@ export default function ProductGrid({
{/* ── Product grid ── */} {/* ── Product grid ── */}
{filteredProducts.length > 0 ? ( {filteredProducts.length > 0 ? (
<div className={`grid gap-4 ${gridCols}`}> <div className={`grid gap-4 ${gridCols}`}>
{filteredProducts.map((product) => ( {filteredProducts.map(
({ id, imageUrl, name, price, description }) => (
<ProductCard <ProductCard
key={product.id} key={id}
image={product.imageUrl} image={imageUrl}
imageAlt={product.name} imageAlt={name}
productName={product.name} productName={name}
price={product.price} price={price}
description={product.description} description={description}
onBuy={() => addToCart(product)} onBuy={() => addToCart({ productId: id!, quantity: 1 })}
/> />
))} ),
)}
</div> </div>
) : ( ) : (
/* Empty state */ /* Empty state */
+151 -89
View File
@@ -1,131 +1,193 @@
"use client"; "use client";
import { gql } from "@apollo/client";
import { useMutation, useQuery } from "@apollo/client/react";
import { createContext, useContext, useEffect, useMemo, useState } from "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 { interface CartContextValue {
items: CartItemEntity[]; items: CartItemEntity[];
totalItems: number; totalItems: number;
totalPrice: number; totalPrice: number;
addToCart: (product: MenuItemEntity) => void; addToCart: (product: CartItemEntity) => void;
increaseQty: (id: string) => void; increaseQty: (id: string) => void;
decreaseQty: (id: string) => void; decreaseQty: (id: string) => void;
removeFromCart: (id: string) => void; removeFromCart: (id: string) => void;
setQuantity: (id: string, quantity: number) => void; setQuantity: (id: string, quantity: number) => void;
} }
const STORAGE_KEY = "coffee-shop-cart"; const CART_ID = "cartId";
const CartContext = createContext<CartContextValue | null>(null); const CartContext = createContext<CartContextValue | null>(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 }) { export function CartProvider({ children }: { children: React.ReactNode }) {
const [items, setItems] = useState<CartItemEntity[]>([]); const [cart, setCart] = useState<CartEntity>(null!);
const [cartId, setCartId] = useState<string | null>(null);
useEffect(() => { const { data: eateryData } = useQuery<allEateriesQuery>(GET_EATERY, {
try { client: eateryClient,
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));
}
} catch {
localStorage.removeItem(STORAGE_KEY);
}
}, []);
useEffect(() => {
localStorage.setItem(STORAGE_KEY, JSON.stringify(items));
}, [items]);
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 next = [...prev];
next[index] = { ...next[index], quantity: next[index].quantity + 1 };
return next;
}); });
};
const increaseQty = (id: string) => { const [createCart] = useMutation<createCartMutation>(CREATE_CART, {
setItems((prev) => client: cartClient,
prev.map((item) => });
item.productId === id ? { ...item, quantity: item.quantity + 1 } : item,
),
);
};
const decreaseQty = (id: string) => { const { data, loading, error } = useQuery<getCartQuery>(GET_CART_ITEMS, {
setItems((prev) => client: cartClient,
prev variables: { cartId },
.map((item) => skip: !cartId,
item.productId === id });
? { ...item, quantity: Math.max(0, item.quantity - 1) }
: item,
)
.filter((item) => item.quantity > 0),
);
};
const removeFromCart = (id: string) => { const [addMenuItem] = useMutation<addMenuItemMutation>(ADD_ITEM, {
setItems((prev) => prev.filter((item) => item.productId !== id)); client: cartClient,
}; });
const setQuantity = (id: string, quantity: number) => { useEffect(() => {
const safeQty = Number.isFinite(quantity) const createCartFunc = async () => {
? Math.max(0, Math.floor(quantity)) if (eateryData && eateryData.allEateries?.length > 0) {
: 0; try {
if (safeQty === 0) { const firstEateryId = eateryData.allEateries[0].id;
removeFromCart(id); const { data: mutationResult } = await createCart({
return; 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);
}
} }
setItems((prev) =>
prev.map((item) =>
item.productId === id ? { ...item, quantity: safeQty } : item,
),
);
}; };
if (error) createCartFunc();
else {
const localCartId = localStorage.getItem(CART_ID);
if (localCartId) setCartId(localCartId);
}
}, [eateryData, createCart, data, loading]);
useEffect(() => {
if (data?.getCart) setCart(data.getCart);
}, [data]);
const addToCart = async (product: CartItemEntity) => {
if (!cartId) return;
const { data: result } = await addMenuItem({
variables: {
cartId,
menuItemId: product.productId!,
quantity: product.quantity,
},
});
if (result) setCart(result.addItem);
};
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 removeFromCart = (id: string) => setQuantity(id, 0);
const increaseQty = (id: string) =>
setQuantity(id, cart.items.find((i) => i.productId == id)!.quantity + 1);
const decreaseQty = (id: string) =>
setQuantity(id, cart.items.find((i) => i.productId == id)!.quantity - 1);
const totalItems = useMemo( const totalItems = useMemo(
() => items.reduce((sum, item) => sum + item.quantity, 0), () => cart?.items.reduce((sum, item) => sum + item.quantity, 0),
[items], [cart],
);
const totalPrice = useMemo(
() =>
items.reduce(
(sum, item) => sum + item.priceAtTimeOfAdding * item.quantity,
0,
),
[items],
); );
const value = useMemo( const value = useMemo(
() => ({ () => ({
items, items: cart?.items,
totalItems, totalItems,
totalPrice, totalPrice: cart?.totalAmount,
addToCart, addToCart,
increaseQty, increaseQty,
decreaseQty, decreaseQty,
removeFromCart, removeFromCart,
setQuantity, setQuantity,
}), }),
[items, totalItems, totalPrice], [cart?.items, cart?.totalAmount],
); );
return <CartContext.Provider value={value}>{children}</CartContext.Provider>; return <CartContext.Provider value={value}>{children}</CartContext.Provider>;
+16 -1
View File
@@ -125,6 +125,7 @@ export interface MenuItemEntity {
export interface ShiftEntity {} export interface ShiftEntity {}
export interface EateryEntity { export interface EateryEntity {
id: string;
ownerId: string; ownerId: string;
name: string; name: string;
menuItems: MenuItemEntity[]; menuItems: MenuItemEntity[];
@@ -150,10 +151,24 @@ export interface deleteMenuItemMutation {
export interface CartItemEntity { export interface CartItemEntity {
productId: string; productId: string;
quantity: number; quantity: number;
priceAtTimeOfAdding: number; priceAtTimeOfAdding?: number;
} }
export interface CartEntity { export interface CartEntity {
Id: string; Id: string;
items: CartItemEntity[]; items: CartItemEntity[];
totalAmount: number;
paymentQrUrl: string;
}
export interface getCartQuery {
getCart: CartEntity;
}
export interface createCartMutation {
createCart: string;
}
export interface addMenuItemMutation {
addItem: CartEntity;
} }