All files / src/features/babies/components EditBabyDialog.tsx

76.92% Statements 10/13
100% Branches 10/10
75% Functions 3/4
75% Lines 9/12

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                                                          17x 17x 17x 17x 17x 10x                                                                                         2x   1x 1x                                
import { useTranslation } from 'react-i18next'
import { toast } from 'sonner'
 
import {
  AlertDialog,
  AlertDialogAction,
  AlertDialogCancel,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogHeader,
  AlertDialogTitle,
  AlertDialogTrigger,
} from '@/components/ui/alert-dialog'
import { Button } from '@/components/ui/button'
import { useAppointments } from '@/features/appointments/api/appointments.hooks'
import { growthSeries } from '@/features/growth/api/growth.selectors'
import { useDeleteBaby, useUpdateBaby } from '../api/babies.hooks'
import type { Baby } from '../api/babies.schemas'
import { BabyEditorDialog } from './BabyEditorDialog'
import { GuardiansSection } from './GuardiansSection'
 
export function EditBabyDialog({
  baby,
  onOpenChange,
}: {
  baby: Baby | null
  onOpenChange: (open: boolean) => void
}) {
  const { t } = useTranslation()
  const updateBaby = useUpdateBaby(baby?.id ?? '')
  const deleteBaby = useDeleteBaby()
  const appointments = useAppointments(baby?.id ?? null)
  if (!baby) return null
  return (
    <BabyEditorDialog
      key={baby.id}
      open
      baby={baby}
      onOpenChange={onOpenChange}
      busy={deleteBaby.isPending}
      latestMeasurement={growthSeries(appointments.data ?? [], baby.birthDate, baby.measurements).at(-1)}
      onSave={async (values) => {
        await updateBaby.mutateAsync(values)
        toast.success(t('babies.edit.successToast'))
        onOpenChange(false)
      }}
      management={
        <>
          <GuardiansSection babyId={baby.id} babyName={baby.name} />
          <details className="rounded-2xl border border-border p-4">
            <summary className="cursor-pointer text-sm font-bold text-destructive">
              {t('babies.delete.sectionTitle')}
            </summary>
            <p className="my-4 text-sm text-ink-muted">{t('babies.delete.sectionDescription')}</p>
            {deleteBaby.isError && (
              <p role="alert" className="mb-3 text-sm text-destructive">
                {t('babies.delete.genericError')}
              </p>
            )}
            <AlertDialog>
              <AlertDialogTrigger asChild>
                <Button type="button" variant="destructive" size="sm">
                  {t('babies.delete.action', { name: baby.name })}
                </Button>
              </AlertDialogTrigger>
              <AlertDialogContent>
                <AlertDialogHeader>
                  <AlertDialogTitle>{t('babies.delete.confirmTitle')}</AlertDialogTitle>
                  <AlertDialogDescription>
                    {t('babies.delete.confirmDescription', { name: baby.name })}
                  </AlertDialogDescription>
                </AlertDialogHeader>
                <AlertDialogFooter>
                  <AlertDialogCancel>{t('babies.delete.confirmDismiss')}</AlertDialogCancel>
                  <AlertDialogAction
                    variant="destructive"
                    disabled={deleteBaby.isPending}
                    onClick={() =>
                      deleteBaby.mutate(baby.id, {
                        onSuccess: () => {
                          toast.success(t('babies.delete.successToast'))
                          onOpenChange(false)
                        },
                      })
                    }
                  >
                    {t('babies.delete.confirmAction')}
                  </AlertDialogAction>
                </AlertDialogFooter>
              </AlertDialogContent>
            </AlertDialog>
          </details>
        </>
      }
    />
  )
}