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 | 133x 131x 6x 6x 1x 1x 1x 1x 1x 2x 2x 2x 1x 3x 2x 12x 8x 2x 2x 1x 3x | import { httpClient } from '@/lib/http-client'
import { kilogramsInputToGrams, centimetersInputToMillimeters } from '@/shared/utils/measurements'
import {
babyFormSchema,
babyListSchema,
babySchema,
createInviteResponseSchema,
guardianListSchema,
type Baby,
type BabyFormInput,
type CreateInviteResponse,
type Guardian,
} from './babies.schemas'
export async function fetchBabies(): Promise<Baby[]> {
const response = await httpClient.get<unknown>('/babies')
return babyListSchema.parse(response)
}
// A blank text input yields `''`, and a parent who typed only spaces meant the same thing — the
// API takes `min(1)`, so both have to become "absent" rather than reach it as a value.
function optionalText(value: string | undefined): string | undefined {
const trimmed = value?.trim()
return trimmed ? trimmed : undefined
}
export async function createBaby(input: BabyFormInput): Promise<Baby> {
const parsed = babyFormSchema.parse(input)
const { weightKg, heightCm, measuredOn, ...profile } = parsed
const body = {
...profile,
measurement: measurementBody({ weightKg, heightCm, measuredOn }),
healthPlanName: optionalText(parsed.healthPlanName),
sexAtBirth: parsed.sexAtBirth,
healthPlanNumber: optionalText(parsed.healthPlanNumber),
allergies: parsed.allergies && parsed.allergies.length > 0 ? parsed.allergies : undefined,
avatarUrl: parsed.avatarUrl ? parsed.avatarUrl : undefined,
avatarColor: parsed.avatarColor ? parsed.avatarColor : undefined,
}
const response = await httpClient.post<unknown>('/babies', body)
return babySchema.parse(response)
}
/**
* Unlike create, edit always sends the full form state (nulls for cleared
* optional fields) rather than omitting untouched-looking fields — the form
* is pre-filled with the current profile, so every field the user sees is
* an explicit value, not a partial patch.
*/
export async function updateBaby(babyId: string, input: BabyFormInput): Promise<Baby> {
const parsed = babyFormSchema.parse(input)
const body = {
measurement: measurementBody(parsed),
name: parsed.name,
birthDate: parsed.birthDate,
// `null` e não omissão: o formulário sempre mostra a escolha atual, então "prefiro não
// informar" é uma resposta explícita, e omitir a chave significaria "não mexi".
sexAtBirth: parsed.sexAtBirth ?? null,
bloodType: parsed.bloodType ?? null,
allergies: parsed.allergies ?? [],
healthPlanName: optionalText(parsed.healthPlanName) ?? null,
healthPlanNumber: optionalText(parsed.healthPlanNumber) ?? null,
avatarUrl: parsed.avatarUrl ? parsed.avatarUrl : null,
avatarColor: parsed.avatarColor ? parsed.avatarColor : null,
}
const response = await httpClient.patch<unknown>(`/babies/${babyId}`, body)
return babySchema.parse(response)
}
function measurementBody(values: Pick<BabyFormInput, 'weightKg' | 'heightCm' | 'measuredOn'>) {
Eif (!values.weightKg?.trim() && !values.heightCm?.trim()) return undefined
return {
measuredOn: values.measuredOn,
weightGrams: kilogramsInputToGrams(values.weightKg),
heightMillimeters: centimetersInputToMillimeters(values.heightCm),
}
}
export async function deleteBaby(babyId: string): Promise<void> {
await httpClient.delete<void>(`/babies/${babyId}`)
}
export async function fetchBabyGuardians(babyId: string): Promise<Guardian[]> {
const response = await httpClient.get<unknown>(`/babies/${babyId}/guardians`)
return guardianListSchema.parse(response)
}
export async function createBabyInvite(babyId: string, inviteeEmail?: string): Promise<CreateInviteResponse> {
const body = inviteeEmail ? { inviteeEmail } : {}
const response = await httpClient.post<unknown>(`/babies/${babyId}/invites`, body)
return createInviteResponseSchema.parse(response)
}
export async function removeBabyGuardian(babyId: string, userId: string): Promise<void> {
await httpClient.delete<void>(`/babies/${babyId}/guardians/${userId}`)
}
|