Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | 337x 198x 13x 131x 131x 131x 134x 18x 131x 134x 134x 134x 131x 117x 134x 134x 286x 286x 7x 5x 5x 1x 1x 128x 136x 144x 136x 124x 19x 136x 124x 124x 198x 198x 4x 3x 3x 7x 7x 2x 1x 1x | import { useMutation, useQueries, useQuery, useQueryClient } from '@tanstack/react-query'
import { useBabies } from '@/features/babies/api/babies.hooks'
import type { Baby } from '@/features/babies/api/babies.schemas'
import {
applyVaccine,
deleteAdhocVaccine,
fetchAdhocVaccines,
fetchVaccineCalendar,
registerAdhocVaccine,
} from './vaccines.api'
import type {
AdhocVaccineRecord,
ApplyVaccineInput,
CreateAdhocVaccineInput,
VaccineAgeGroup,
VaccineCalendar,
VaccineCatalogMetadata,
VaccineItem,
} from './vaccines.schemas'
export function vaccinesQueryKey(babyId: string) {
return ['babies', babyId, 'vaccines'] as const
}
export function useVaccineCalendar(babyId: string | null) {
return useQuery({
queryKey: vaccinesQueryKey(babyId ?? ''),
queryFn: () => fetchVaccineCalendar(babyId!),
enabled: !!babyId,
})
}
export interface VaccineItemWithBaby extends VaccineItem {
babyId: string
}
export interface BabyVaccineCalendarEntry {
baby: Baby
groups: VaccineAgeGroup[]
items: VaccineItemWithBaby[]
isPending: boolean
isError: boolean
}
export interface AllBabiesVaccineCalendars {
babies: Baby[]
metadata: VaccineCatalogMetadata | null
isPending: boolean
isError: boolean
isEmpty: boolean
perBaby: BabyVaccineCalendarEntry[]
items: VaccineItemWithBaby[]
}
// Vaccine schedule items don't carry a babyId of their own (only the fetch call is
// baby-scoped) — this stamps one on client-side so a merged household view can tell
// whose dose is whose.
export function useAllBabiesVaccineCalendars(): AllBabiesVaccineCalendars {
const babies = useBabies()
const babyList = babies.data ?? []
const results = useQueries({
queries: babyList.map((baby) => ({
queryKey: vaccinesQueryKey(baby.id),
queryFn: () => fetchVaccineCalendar(baby.id),
enabled: babies.isSuccess,
})),
})
const perBaby: BabyVaccineCalendarEntry[] = babyList.map((baby, index) => {
const result = results[index]
const groups = result?.data?.groups ?? []
return {
baby,
groups,
items: groups.flatMap((group) => group.items.map((item) => ({ ...item, babyId: baby.id }))),
isPending: result?.isPending ?? true,
isError: result?.isError ?? false,
}
})
return {
babies: babyList,
metadata: results.find((result) => result.data)?.data?.metadata ?? null,
isPending: babies.isPending || (babies.isSuccess && results.some((result) => result.isPending)),
isError: babies.isError || results.some((result) => result.isError),
isEmpty: babies.isSuccess && babyList.length === 0,
perBaby,
items: perBaby.flatMap((entry) => entry.items),
}
}
interface ApplyVaccineVariables {
vaccineId: string
input: ApplyVaccineInput
}
export function useApplyVaccine(babyId: string | null) {
const queryClient = useQueryClient()
return useMutation({
mutationFn: ({ vaccineId, input }: ApplyVaccineVariables) =>
applyVaccine(babyId!, vaccineId, input),
onSuccess: (updatedItem) => {
queryClient.setQueryData<VaccineCalendar>(vaccinesQueryKey(babyId ?? ''), (prev) =>
prev
? {
...prev,
groups: prev.groups.map((group) => ({
...group,
items: group.items.map((item) =>
item.vaccineId === updatedItem.vaccineId ? updatedItem : item,
),
})),
}
: prev,
)
},
})
}
export function adhocVaccinesQueryKey(babyId: string) {
return ['babies', babyId, 'vaccines', 'adhoc'] as const
}
export function useAdhocVaccines(babyId: string | null) {
return useQuery({
queryKey: adhocVaccinesQueryKey(babyId ?? ''),
queryFn: () => fetchAdhocVaccines(babyId!),
enabled: !!babyId,
})
}
export interface AllBabiesAdhocVaccines {
isPending: boolean
isError: boolean
items: AdhocVaccineRecord[]
}
export function useAllBabiesAdhocVaccines(visibleIds?: readonly string[]): AllBabiesAdhocVaccines {
const babies = useBabies()
const babyList = (babies.data ?? []).filter((baby) => !visibleIds || visibleIds.includes(baby.id))
const results = useQueries({
queries: babyList.map((baby) => ({
queryKey: adhocVaccinesQueryKey(baby.id),
queryFn: () => fetchAdhocVaccines(baby.id),
enabled: babies.isSuccess,
})),
})
return {
isPending: babies.isPending || (babies.isSuccess && results.some((result) => result.isPending)),
isError: babies.isError || results.some((result) => result.isError),
items: results.flatMap((result) => result.data ?? []),
}
}
export function useRegisterAdhocVaccine(babyId: string | null) {
const queryClient = useQueryClient()
return useMutation({
mutationFn: (input: CreateAdhocVaccineInput) => registerAdhocVaccine(babyId!, input),
onSuccess: (created) => {
queryClient.setQueryData<AdhocVaccineRecord[]>(
adhocVaccinesQueryKey(babyId ?? ''),
(prev) => [created, ...(prev ?? [])],
)
},
})
}
export function useDeleteAdhocVaccine(babyId: string) {
const queryClient = useQueryClient()
return useMutation({
mutationFn: (recordId: string) => deleteAdhocVaccine(babyId, recordId),
onSuccess: (_data, deletedRecordId) => {
queryClient.setQueryData<AdhocVaccineRecord[]>(adhocVaccinesQueryKey(babyId), (prev) =>
prev?.filter((record) => record.id !== deletedRecordId),
)
},
})
}
|