Files
meezi/web/dashboard/src/lib/pos2/use-pos2.ts
T

59 lines
2.4 KiB
TypeScript
Raw Normal View History

"use client";
// ─────────────────────────────────────────────────────────────────────────────
// POS v2 data hooks — thin wrappers over the EXISTING data layer so the new POS
// UI reuses the same endpoints/query keys as the classic POS (cache-shared).
// ─────────────────────────────────────────────────────────────────────────────
import { useMemo } from "react";
import { useQuery } from "@tanstack/react-query";
import { apiGet } from "@/lib/api/client";
import { getBranchMenu, branchMenuItemToMenuItem } from "@/lib/api/branch-menu";
import { fetchCafeTableBoard } from "@/lib/api/branch-tables";
import type { MenuCategory, MenuItem, TableBoardItem } from "@/lib/api/types";
export function usePos2Categories(cafeId?: string | null) {
return useQuery({
queryKey: ["menu-categories", cafeId],
queryFn: () => apiGet<MenuCategory[]>(`/api/cafes/${cafeId}/menu/categories`),
enabled: !!cafeId,
staleTime: 60_000,
});
}
/** Branch-scoped menu (effective prices) when a branch is selected; otherwise the
* café-wide menu. Both normalize to MenuItem so the cart store can consume them. */
export function usePos2Menu(cafeId?: string | null, branchId?: string | null) {
return useQuery({
queryKey: ["pos2-menu", cafeId, branchId ?? "cafe"],
queryFn: async (): Promise<MenuItem[]> => {
if (branchId) {
const rows = await getBranchMenu(cafeId as string, branchId);
return rows.map(branchMenuItemToMenuItem);
}
return apiGet<MenuItem[]>(`/api/cafes/${cafeId}/menu/items`);
},
enabled: !!cafeId,
staleTime: 30_000,
});
}
export function usePos2Tables(cafeId?: string | null, branchId?: string | null) {
return useQuery({
queryKey: ["tables-board", cafeId, branchId, "pos"],
queryFn: () => fetchCafeTableBoard(cafeId as string, branchId ?? undefined),
enabled: !!cafeId,
refetchInterval: 15_000,
});
}
export function useMenuById(items: MenuItem[] | undefined): Map<string, MenuItem> {
return useMemo(() => {
const m = new Map<string, MenuItem>();
for (const it of items ?? []) m.set(it.id, it);
return m;
}, [items]);
}
export type { MenuCategory, MenuItem, TableBoardItem };