Co-authored-by: Thanh Quy - wolf <524H0124@student.tdtu.edu.vn> Co-authored-by: Thanh Quy- wolf <524H0124@student.tdtu.edu.vn> Reviewed-on: #42 Co-authored-by: TaNguyenThanhQuy <tanguyenthanhquy@noreply.localhost> Co-committed-by: TaNguyenThanhQuy <tanguyenthanhquy@noreply.localhost>
This commit was merged in pull request #42.
This commit is contained in:
@@ -123,10 +123,12 @@ export function CartProvider({ children }: { children: React.ReactNode }) {
|
||||
}
|
||||
};
|
||||
|
||||
if (error) createCartFunc();
|
||||
else {
|
||||
if (error) {
|
||||
createCartFunc();
|
||||
} else if (!cartId) {
|
||||
const localCartId = localStorage.getItem(CART_ID);
|
||||
if (localCartId) setCartId(localCartId);
|
||||
else createCartFunc();
|
||||
}
|
||||
}, [eateryData, createCart, data, loading, error]);
|
||||
|
||||
@@ -148,14 +150,16 @@ export function CartProvider({ children }: { children: React.ReactNode }) {
|
||||
if (result) setCart(result.addItem);
|
||||
};
|
||||
|
||||
const setQuantity = async (id: string, quantity: number) => {
|
||||
const setQuantity = async (id: string, newQuantity: number) => {
|
||||
if (!cartId) return;
|
||||
|
||||
const currentItem = cart.items.find((i) => i.productId == id);
|
||||
|
||||
const { data: result } = await addMenuItem({
|
||||
variables: {
|
||||
cartId,
|
||||
menuItemId: id,
|
||||
quantity,
|
||||
quantity: newQuantity - currentItem!.quantity,
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
+21
-1
@@ -51,6 +51,7 @@ const GET_EATERY_MENU = gql`
|
||||
menuItems {
|
||||
id
|
||||
name
|
||||
imageUrl
|
||||
available
|
||||
description
|
||||
price
|
||||
@@ -64,6 +65,7 @@ const ADD_MENU_ITEM = gql`
|
||||
addMenuItem(menuItem: $menuItem) {
|
||||
id
|
||||
name
|
||||
imageUrl
|
||||
available
|
||||
description
|
||||
price
|
||||
@@ -76,6 +78,7 @@ const UPDATE_MENU_ITEM = gql`
|
||||
updateMenuItem(menuItem: $menuItem) {
|
||||
id
|
||||
name
|
||||
imageUrl
|
||||
available
|
||||
description
|
||||
price
|
||||
@@ -129,7 +132,24 @@ export function ManagerProvider({ children }: { children: ReactNode }) {
|
||||
},
|
||||
});
|
||||
|
||||
if (data) setProducts((prev) => [...prev, data.addMenuItem]);
|
||||
if (!data) return;
|
||||
|
||||
// addMenuItem backend does not persist imageUrl (constructor only maps name+price).
|
||||
// If the caller provided an imageUrl, patch it immediately via updateMenuItem
|
||||
// which uses MapStruct and correctly saves all fields.
|
||||
if (product.imageUrl && data.addMenuItem?.id) {
|
||||
const { data: updated } = await mutateUpdateMenuItem({
|
||||
variables: {
|
||||
menuItem: { id: data.addMenuItem.id, imageUrl: product.imageUrl },
|
||||
},
|
||||
});
|
||||
if (updated) {
|
||||
setProducts((prev) => [...prev, updated.updateMenuItem]);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
setProducts((prev) => [...prev, data.addMenuItem]);
|
||||
};
|
||||
|
||||
const updateProduct = async (product: MenuItemEntity) => {
|
||||
|
||||
Reference in New Issue
Block a user