mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-09 04:50:59 +00:00
listPage (src/lib/http/list-query.ts) reads one row past the limit and cuts it back, on top of the existing list-cap signal rather than beside it. Every capped list now gets its page there, and every read passed to it orders by something ending in the row id, so rows that share a sort value cannot repeat or vanish at a page boundary. - practitioners, vaccinations and encounters were cut at their take with no signal; they now answer meta.truncated and accept offset, and their web readers walk the pages instead of stopping at the first - symptoms/events reported hasMore on a page that was exactly full; it is now true only when a row past the page exists - the document list's hand-rolled limit + 1 goes through the helper - security-activity answers an out-of-range limit with the 422 its published schema already promised instead of clamping it list-page-guard binds every apiListSuccess route to listPage, forbids hand-cut lists and length-derived hasMore, and requires the id tiebreaker in every listPage read.
95 lines
2.9 KiB
TypeScript
95 lines
2.9 KiB
TypeScript
"use client";
|
|
|
|
/**
|
|
* Data hooks for the account's own address book of doctors and practices.
|
|
*
|
|
* The search runs in SQL over the two plaintext columns rather than in the
|
|
* browser over a downloaded list: the columns are plaintext precisely so the
|
|
* picker can search and sort them there, and a client-side filter would need
|
|
* the whole book in memory to do it.
|
|
*/
|
|
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
|
|
|
import { apiDelete, apiGetList, apiPatch, apiPost } from "@/lib/api/api-fetch";
|
|
import {
|
|
encounterDependentKeys,
|
|
invalidateKeys,
|
|
queryKeys,
|
|
refetchInactiveDailyReads,
|
|
} from "@/lib/query-keys";
|
|
import type { PractitionerDTO } from "@/lib/practitioners/dto";
|
|
|
|
export type Practitioner = PractitionerDTO;
|
|
|
|
export interface PractitionerWriteBody {
|
|
name?: string;
|
|
specialty?: string | null;
|
|
practice?: string | null;
|
|
location?: string | null;
|
|
phone?: string | null;
|
|
note?: string | null;
|
|
}
|
|
|
|
const BASE = "/api/practitioners";
|
|
const PRACTITIONER_PAGE_SIZE = 200;
|
|
|
|
export function usePractitioners(q?: string, enabled = true) {
|
|
const term = q?.trim() || undefined;
|
|
return useQuery({
|
|
queryKey: queryKeys.practitionerList(term),
|
|
// The route pages and says whether another page exists; the pickers and
|
|
// the address book read this as the whole list, so it walks to the end.
|
|
queryFn: async () => {
|
|
const all: Practitioner[] = [];
|
|
for (let offset = 0; ; offset += PRACTITIONER_PAGE_SIZE) {
|
|
const sp = new URLSearchParams({
|
|
limit: String(PRACTITIONER_PAGE_SIZE),
|
|
offset: String(offset),
|
|
});
|
|
if (term) sp.set("q", term);
|
|
const { data: page, cap } = await apiGetList<Practitioner[]>(
|
|
`/api/practitioners?${sp.toString()}`,
|
|
);
|
|
all.push(...page);
|
|
if (!cap?.truncated) return all;
|
|
}
|
|
},
|
|
enabled,
|
|
});
|
|
}
|
|
|
|
export function usePractitionerMutations() {
|
|
const qc = useQueryClient();
|
|
// Renaming a practice changes the label a rail appointment renders under,
|
|
// and the daily reads are unmounted from the address book — pair the
|
|
// bundle invalidation with the forced inactive refetch.
|
|
const invalidate = () =>
|
|
Promise.all([
|
|
invalidateKeys(qc, encounterDependentKeys),
|
|
refetchInactiveDailyReads(qc),
|
|
]);
|
|
|
|
const create = useMutation({
|
|
mutationKey: queryKeys.practitionerCreate(),
|
|
mutationFn: (body: PractitionerWriteBody) =>
|
|
apiPost<Practitioner>(BASE, body),
|
|
onSuccess: invalidate,
|
|
});
|
|
|
|
const update = useMutation({
|
|
mutationKey: queryKeys.practitionerUpdate(),
|
|
mutationFn: ({ id, body }: { id: string; body: PractitionerWriteBody }) =>
|
|
apiPatch<Practitioner>(`${BASE}/${id}`, body),
|
|
onSuccess: invalidate,
|
|
});
|
|
|
|
const remove = useMutation({
|
|
mutationKey: queryKeys.practitionerDelete(),
|
|
mutationFn: (id: string) =>
|
|
apiDelete<{ deleted: boolean }>(`${BASE}/${id}`),
|
|
onSuccess: invalidate,
|
|
});
|
|
|
|
return { create, update, remove };
|
|
}
|