Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/SantaClaas/enterprise-web-development.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869import { infiniteQueryOptions } from "@tanstack/solid-query";
import type { UserId } from "./user";
export type TimeId = string & { __brand: "TimeId" };
export type Time = { id: TimeId; start: Temporal.Instant; end: Temporal.Instant;};
const PAGE_SIZE = 30;
export const query = (userId: string | undefined) => infiniteQueryOptions({ queryKey: ["users", userId, "times"], async queryFn({ pageParam, signal }): Promise<Time[]> { const response = await fetch( `/api/users/${userId}/times?page=${pageParam}&size=${PAGE_SIZE}`, { method: "GET", signal }, );
if (!response.ok) throw new Error(`Error fetching times: ${response.status} ${await response.text()}`);
const times: Time[] = await response.json(); for (const time of times) { time.start = Temporal.Instant.from(time.start); time.end = Temporal.Instant.from(time.end); }
return times; }, initialPageParam: 0, getNextPageParam: (lastPage, allPages) => lastPage.length === PAGE_SIZE ? allPages.length : undefined, enabled: Boolean(userId), });
export type UpdateParameters = { userId: UserId; times: Time[];};
export async function updateTimes({ userId, times }: UpdateParameters) { const response = await fetch(`/api/users/${userId}/times`, { method: "PUT", headers: { "Content-Type": "application/json", }, body: JSON.stringify(times), });
if (!response.ok) throw new Error("Error updating times");}
export async function deleteTimes({ userId, timeIds }: { userId: UserId; timeIds: TimeId[] }) { const response = await fetch(`/api/users/${userId}/times`, { method: "DELETE", headers: { "Content-Type": "application/json", }, body: JSON.stringify(timeIds), });
if (!response.ok) throw new Error("Error deleting times");}