feat(plan): react query options and page state hook

This commit is contained in:
BOHA
2026-06-05 12:54:50 +02:00
parent 9c10ff9f22
commit ca911bf96c
2 changed files with 237 additions and 0 deletions

View File

@@ -0,0 +1,63 @@
import { queryOptions } from "@tanstack/react-query";
import apiFetch from "../../utils/api";
import { jsonQuery } from "../apiAdapter";
export interface PlanUser {
id: number;
full_name: string;
username: string;
role_name: string | null;
is_active: boolean;
has_attendance_record: boolean;
}
export interface ResolvedCell {
source: "entry" | "override";
entryId: number | null;
overrideId: number | null;
user_id: number;
shift_date: string;
project_id: number | null;
category: string;
note: string;
rangeFrom: string | null;
rangeTo: string | null;
}
export interface GridData {
view: "week" | "month";
date_from: string;
date_to: string;
users: PlanUser[];
cells: Record<number, Record<string, ResolvedCell | null>>;
}
export const planKeys = {
all: ["plan"] as const,
grid: (dateFrom: string, dateTo: string, view: "week" | "month") =>
["plan", "grid", { dateFrom, dateTo, view }] as const,
entries: (userId: number | null, dateFrom: string, dateTo: string) =>
["plan", "entries", { userId, dateFrom, dateTo }] as const,
overrides: (userId: number | null, dateFrom: string, dateTo: string) =>
["plan", "overrides", { userId, dateFrom, dateTo }] as const,
users: () => ["plan", "users"] as const,
};
export const gridQuery = (
dateFrom: string,
dateTo: string,
view: "week" | "month",
) =>
queryOptions({
queryKey: planKeys.grid(dateFrom, dateTo, view),
queryFn: () =>
jsonQuery<GridData>(
`/api/admin/plan/grid?date_from=${dateFrom}&date_to=${dateTo}&view=${view}`,
),
});
export const usersQuery = () =>
queryOptions({
queryKey: planKeys.users(),
queryFn: () => jsonQuery<PlanUser[]>("/api/admin/plan/users"),
});