fix: final commit (#46)
Release package / release (push) Successful in 8m30s

Co-authored-by: TakahashiNg <83152264+TakahashiNguyen@users.noreply.github.com>
Reviewed-on: #46
This commit was merged in pull request #46.
This commit is contained in:
2026-05-15 13:22:12 +00:00
parent 7b4e46c4e4
commit 2376d57cbb
5 changed files with 103 additions and 36 deletions
+24
View File
@@ -18,12 +18,14 @@ interface CartContextValue {
items: CartItemEntity[];
totalItems: number;
totalPrice: number;
cart: CartEntity;
eateryId: string | undefined;
addToCart: (product: CartItemEntity) => void;
increaseQty: (id: string) => void;
decreaseQty: (id: string) => void;
removeFromCart: (id: string) => void;
setQuantity: (id: string, quantity: number) => void;
removeCart: () => void;
}
const CART_ID = "cartId";
@@ -120,6 +122,7 @@ export function CartProvider({ children }: { children: React.ReactNode }) {
}
} catch (err) {
console.error("Lỗi khi tạo giỏ hàng:", err);
localStorage.removeItem(CART_ID);
}
}
};
@@ -151,6 +154,25 @@ export function CartProvider({ children }: { children: React.ReactNode }) {
if (result) setCart(result.addItem);
};
const DELETE_CART = gql`
mutation deleteCart($cartId: String!) {
deleteCart(cartId: $cartId)
}
`;
const [mutateDeleteCart] = useMutation(DELETE_CART, {
client: cartClient,
});
const removeCart = async () => {
localStorage.removeItem(CART_ID);
await mutateDeleteCart({ variables: { cartId } });
setCartId(null);
setCart(null!);
};
const setQuantity = async (id: string, newQuantity: number) => {
if (!cartId) return;
@@ -191,6 +213,8 @@ export function CartProvider({ children }: { children: React.ReactNode }) {
decreaseQty,
removeFromCart,
setQuantity,
removeCart,
cart,
}),
[cart?.items, cart?.totalAmount, cart?.eateryId],
);