All files / src/features/vaccines/routes VaccinationCardRoute.tsx

78.78% Statements 26/33
63.46% Branches 33/52
76.92% Functions 10/13
79.31% Lines 23/29

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                          4x 4x 4x 3x   4x 4x 4x 4x 4x 4x 4x 4x   8x                                 4x 8x                   8x 4x 4x 4x             4x                         1x                           4x               1x                                                                                      
import { useEffect, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { Link, useParams } from 'react-router-dom'
import { ArrowLeft, Printer } from 'lucide-react'
import { useBabies } from '@/features/babies/api/babies.hooks'
import { useAdhocVaccines, useVaccineCalendar } from '../api/vaccines.hooks'
import {
  VaccinationCardDocument,
  type VaccinationCardRow,
} from '../components/VaccinationCardDocument'
import './vaccination-card-print.css'
 
export function VaccinationCardRoute() {
  const { t } = useTranslation()
  const { babyId = '' } = useParams<{ babyId: string }>()
  useEffect(() => {
    Iif (window.scrollY) window.scrollTo({ top: 0, behavior: 'instant' })
  }, [babyId])
  const babies = useBabies()
  const baby = babies.data?.find((item) => item.id === babyId)
  const calendar = useVaccineCalendar(baby?.id ?? null)
  const adhoc = useAdhocVaccines(baby?.id ?? null)
  const [includeCalendar, setIncludeCalendar] = useState(false)
  const isPending = babies.isPending || (!!baby && (calendar.isPending || adhoc.isPending))
  const isError = babies.isError || (!!baby && (calendar.isError || adhoc.isError))
  const rows: VaccinationCardRow[] = [
    ...(calendar.data?.groups ?? []).flatMap((group) =>
      group.items.map((item) => ({
        ...item,
        key: `catalog-${item.vaccineId}`,
        date: item.applicationDate,
        dose: t('vaccines.doseLabel', { count: item.doseNumber }),
        source: 'CATALOG' as const,
      })),
    ),
    ...(adhoc.data ?? []).map((item) => ({
      ...item,
      key: `adhoc-${item.id}`,
      date: item.applicationDate,
      name: item.customName,
      dose: item.customDose || '—',
      source: item.source,
    })),
  ]
  const applied = rows
    .filter((row) => row.status === 'APPLIED')
    .sort((a, b) =>
      a.date && b.date
        ? a.date.localeCompare(b.date)
        : a.date
          ? -1
          : b.date
            ? 1
            : a.name.localeCompare(b.name),
    )
  const upcoming = rows.filter((row) => row.status !== 'APPLIED')
  const ready = !!baby && !isPending && !isError
  const canPrint = ready && (applied.length > 0 || (includeCalendar && upcoming.length > 0))
  const retry = () => {
    void babies.refetch()
    if (baby) {
      void calendar.refetch()
      void adhoc.refetch()
    }
  }
  return (
    <div className={`vaccination-card-page mx-auto max-w-5xl ${ready ? '' : 'print:hidden'}`}>
      <div className="mb-5 flex flex-wrap items-center justify-between gap-3 print:hidden">
        <Link
          to="/vaccines"
          className="inline-flex min-h-11 items-center gap-2 text-sm font-semibold text-ink-muted"
        >
          <ArrowLeft aria-hidden className="size-4" />
          {t('vaccines.document.back')}
        </Link>
        <button
          type="button"
          disabled={!canPrint}
          onClick={() => window.print()}
          className="inline-flex min-h-11 items-center gap-2 rounded-xl bg-primary px-5 py-3 text-sm font-semibold text-primary-foreground disabled:opacity-50"
        >
          <Printer aria-hidden className="size-4" />
          {t('vaccines.document.print')}
        </button>
      </div>
      {ready && (
        <fieldset className="mb-5 rounded-2xl border border-border bg-card p-4 print:hidden">
          <legend className="px-1 text-sm font-semibold text-ink">
            {t('vaccines.document.printScope')}
          </legend>
          <div className="flex flex-wrap gap-x-6">
            {[false, true].map((value) => (
              <label
                key={String(value)}
                className="flex min-h-11 cursor-pointer items-center gap-2 text-sm text-ink-muted"
              >
                <input
                  type="radio"
                  name="print-scope"
                  checked={includeCalendar === value}
                  onChange={() => setIncludeCalendar(value)}
                  className="size-4 accent-primary"
                />
                {t(value ? 'vaccines.document.include' : 'vaccines.document.onlyApplied')}
              </label>
            ))}
          </div>
          {!canPrint && (
            <p className="mt-1 text-sm text-ink-muted">{t('vaccines.document.nothingToPrint')}</p>
          )}
        </fieldset>
      )}
      {isPending ? (
        <div role="status" className="rounded-3xl bg-card p-10 text-center text-ink-muted">
          {t('common.loading')}
        </div>
      ) : isError ? (
        <div role="alert" className="rounded-3xl bg-card p-8 text-center">
          <p>{t('vaccines.card.loadError')}</p>
          <button
            type="button"
            onClick={retry}
            className="mt-3 min-h-11 px-4 font-semibold text-primary"
          >
            {t('nav.shell.retry')}
          </button>
        </div>
      ) : !baby ? (
        <div role="status" className="rounded-3xl bg-card p-8 text-center text-ink-muted">
          {t('vaccines.document.notFound')}
        </div>
      ) : (
        <VaccinationCardDocument
          baby={baby}
          applied={applied}
          upcoming={upcoming}
          includeCalendar={includeCalendar}
          metadata={calendar.data?.metadata}
        />
      )}
    </div>
  )
}