diff --git a/lib/manager-context.tsx b/lib/manager-context.tsx index 25b7bb0..85682e8 100644 --- a/lib/manager-context.tsx +++ b/lib/manager-context.tsx @@ -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( + 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) => { diff --git a/lib/types.ts b/lib/types.ts index 334e1f0..ab5ea2a 100644 --- a/lib/types.ts +++ b/lib/types.ts @@ -138,3 +138,7 @@ export interface allEateriesQuery { export interface addMenuItemMutation { addMenuItem: MenuItemEntity; } + +export interface updateMenuItemMutation { + updateMenuItem: MenuItemEntity; +}