df1f32c23d
- Added `credentials: "include"` to fetch requests in LoginOtpPage and RegisterPage for cookie handling. - Introduced `eateryId` in PaymentPage to manage cart context. - Updated ProductCardProps and ShopCardProps to include `eateryId`. - Enhanced PaymentSummaryCard to accept `eateryId` prop. - Modified ShopCard to set `eateryId` in cart context on click. - Implemented menu item management in MenuItemsTab with add, edit, and delete functionalities. - Created new API functions for handling reviews and cart operations. - Updated GraphQL queries and mutations for menu items and cart management. - Added loading states and error handling in ProductGrid and ReviewModal. - Enhanced local storage management for cart and eatery ID.
87 lines
2.3 KiB
TypeScript
87 lines
2.3 KiB
TypeScript
import type { Eatery, MenuItem } from "./types";
|
|
|
|
export async function gqlFetch<T = Record<string, unknown>>(
|
|
query: string,
|
|
variables?: Record<string, unknown>,
|
|
): Promise<T> {
|
|
const res = await fetch("/api/eatery/graphql", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
credentials: "include",
|
|
body: JSON.stringify({ query, variables }),
|
|
});
|
|
|
|
if (!res.ok) {
|
|
const body = await res.json().catch(() => null);
|
|
const message =
|
|
Array.isArray(body) && body[0]?.message
|
|
? body[0].message
|
|
: `GraphQL request failed: ${res.status}`;
|
|
throw new Error(message);
|
|
}
|
|
return res.json() as Promise<T>;
|
|
}
|
|
|
|
export async function getMenuItemsByEatery(
|
|
eateryId: string,
|
|
): Promise<MenuItem[]> {
|
|
const data = await gqlFetch<{ menuItemsByEatery: MenuItem[] }>(
|
|
`query menuItemsByEatery($eateryId: String!) {
|
|
menuItemsByEatery(eateryId: $eateryId) { id name price }
|
|
}`,
|
|
{ eateryId },
|
|
);
|
|
return data.menuItemsByEatery ?? [];
|
|
}
|
|
|
|
export async function addMenuItem(input: {
|
|
name: string;
|
|
price: number;
|
|
}): Promise<MenuItem | null> {
|
|
const data = await gqlFetch<{ addMenuItem: MenuItem | null }>(
|
|
`mutation addMenuItem($menuItem: AddMenuItemInput!) {
|
|
addMenuItem(menuItem: $menuItem) { id name price }
|
|
}`,
|
|
{ menuItem: input },
|
|
);
|
|
return data.addMenuItem;
|
|
}
|
|
|
|
export async function updateMenuItem(input: {
|
|
id: string;
|
|
name?: string;
|
|
price?: number;
|
|
}): Promise<MenuItem | null> {
|
|
const data = await gqlFetch<{ updateMenuItem: MenuItem | null }>(
|
|
`mutation updateMenuItem($menuItem: UpdateMenuItemInput!) {
|
|
updateMenuItem(menuItem: $menuItem) { id name price }
|
|
}`,
|
|
{ menuItem: input },
|
|
);
|
|
return data.updateMenuItem;
|
|
}
|
|
|
|
export async function deleteMenuItem(menuItemId: string): Promise<boolean> {
|
|
const data = await gqlFetch<{ deleteMenuItem: boolean }>(
|
|
`mutation deleteMenuItem($menuItemId: String!) {
|
|
deleteMenuItem(menuItemId: $menuItemId)
|
|
}`,
|
|
{ menuItemId },
|
|
);
|
|
return data.deleteMenuItem ?? false;
|
|
}
|
|
|
|
export async function fetchManagerEatery(): Promise<Eatery | null> {
|
|
const data = await gqlFetch<{ eateriesByOwner: Eatery | null }>(`
|
|
query {
|
|
eateriesByOwner {
|
|
id
|
|
ownerId
|
|
name
|
|
isVerified
|
|
}
|
|
}
|
|
`);
|
|
return data.eateriesByOwner;
|
|
}
|