This commit is contained in:
+35
-6
@@ -11,10 +11,11 @@ import {
|
||||
} from "react";
|
||||
|
||||
import { eateryClient } from "./apollo-clients";
|
||||
import type {
|
||||
MenuItemEntity,
|
||||
addMenuItemMutation,
|
||||
allEateriesQuery,
|
||||
import {
|
||||
type MenuItemEntity,
|
||||
type addMenuItemMutation,
|
||||
type allEateriesQuery,
|
||||
updateMenuItemMutation,
|
||||
} from "./types";
|
||||
|
||||
// ─── Types ────────────────────────────────────────────────────────────────────
|
||||
@@ -69,6 +70,18 @@ const ADD_MENU_ITEM = gql`
|
||||
}
|
||||
`;
|
||||
|
||||
const UPDATE_MENU_ITEM = gql`
|
||||
mutation updateMenuItem($menuItem: UpdateMenuItemInput!) {
|
||||
updateMenuItem(menuItem: $menuItem) {
|
||||
id
|
||||
name
|
||||
available
|
||||
description
|
||||
price
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
// ─── Provider ─────────────────────────────────────────────────────────────────
|
||||
|
||||
export function ManagerProvider({ children }: { children: ReactNode }) {
|
||||
@@ -84,6 +97,13 @@ export function ManagerProvider({ children }: { children: ReactNode }) {
|
||||
client: eateryClient,
|
||||
});
|
||||
|
||||
const [mutateUpdateMenuItem] = useMutation<updateMenuItemMutation>(
|
||||
UPDATE_MENU_ITEM,
|
||||
{
|
||||
client: eateryClient,
|
||||
},
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
if (data?.allEateries?.[0]) {
|
||||
setProducts(data.allEateries[0].menuItems);
|
||||
@@ -102,8 +122,17 @@ export function ManagerProvider({ children }: { children: ReactNode }) {
|
||||
if (data) setProducts((prev) => [...prev, data.addMenuItem]);
|
||||
};
|
||||
|
||||
const updateProduct = (product: MenuItemEntity) => {
|
||||
setProducts((prev) => prev.map((p) => (p.id === product.id ? product : p)));
|
||||
const updateProduct = async (product: MenuItemEntity) => {
|
||||
const { data } = await mutateUpdateMenuItem({
|
||||
variables: {
|
||||
menuItem: product,
|
||||
},
|
||||
});
|
||||
|
||||
if (data)
|
||||
setProducts((prev) =>
|
||||
prev.map((p) => (p.id === product.id ? data.updateMenuItem : p)),
|
||||
);
|
||||
};
|
||||
|
||||
const deleteProduct = (id: string) => {
|
||||
|
||||
@@ -138,3 +138,7 @@ export interface allEateriesQuery {
|
||||
export interface addMenuItemMutation {
|
||||
addMenuItem: MenuItemEntity;
|
||||
}
|
||||
|
||||
export interface updateMenuItemMutation {
|
||||
updateMenuItem: MenuItemEntity;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user