Files
baya-monorepo/client/src/services/patients/hooks/useCreatePatient.ts
T
hamid 4b4243c451 frontend phase 2: onboarding & profiles — customer/patient, nurse profile & bank
Turns a logged-in user into a usable account, consuming the b3 identity-profiles
contract behind the services/{domain} seam.

Services (mock default true; real HTTP clients wired for a one-line flip):
- services/patients: rewritten to b3 PatientDto + client-augmented relation/conditions;
  full CRUD, optimistic soft-archive, cache-splice on create, age<->birthDate helper.
- services/profiles: customer + nurse profile get/upsert + avatar (404->null mapping).
- services/nurse: payout bank accounts + IBAN(Sheba) util + pending-only polling.

Screens: A3->A4 onboarding wizard, E1 patients list/CRUD, A5 home (first-login gate +
nudge), customer profile (no national-ID), nurse profile bootstrap (unverified
placeholder), nurse bank settings (pending/verified/mismatch + make-primary).

Shared composites (each tested): GenderToggle, ConditionChips, RelationSelect,
PatientForm, PatientCard, BankStatusPanel; reuses f0 StepperHeader/StatusChip/PhoneField.
Adds onboarding/home/profile/nurseProfile/bank i18n namespaces (both locales, in sync),
the --bal-primary-soft token, and nurse sidebar Profile + Bank entries.

Contract gaps filed: REQ-005 (patient relation/conditions), REQ-006 (avatar route),
REQ-007 (customer name/language). Gate: check + 112 tests + build all green.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-02 22:04:38 +03:30

27 lines
1.1 KiB
TypeScript

import { useMutation, useQueryClient } from '@tanstack/react-query';
import type { Paginated } from '@/lib/api/types';
import { patientsApi } from '../apis';
import { patientKeys } from '../keys';
import type { CreatePatientInput, Patient } from '../types';
/**
* Creates a patient. Splices the new row into every cached list immediately (so the E1 list
* and the Home onboarding-gate reflect it without waiting for a refetch — no transient
* "0 patients" window that would bounce the user back to onboarding), then invalidates to
* reconcile. Domain 400s (missing/invalid gender, future birth date) surface via
* `mutation.error`; the fetch layer owns 401/403/5xx toasts.
*/
export function useCreatePatient() {
const queryClient = useQueryClient();
return useMutation({
mutationFn: (input: CreatePatientInput) => patientsApi.create(input),
onSuccess: (patient) => {
queryClient.setQueriesData<Paginated<Patient>>({ queryKey: patientKeys.lists() }, (old) =>
old ? { ...old, items: [patient, ...old.items], total: old.total + 1 } : old,
);
queryClient.invalidateQueries({ queryKey: patientKeys.lists() });
},
});
}