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

78.72% Statements 37/47
78.26% Branches 36/46
75% Functions 18/24
78.57% Lines 33/42

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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208                                                      9x 9x 32x 9x 9x 18x 18x 9x 9x 16x 14x     14x       9x   16x 9x   16x 9x                     9x 9x   9x       9x 11x 11x 11x 11x     9x     3x 3x       9x                                                 4x 2x                                                                     14x                                                       40x                                                                      
import { useSearchDestination } from '@/shared/stores/searchDestination.store'
import { useState } from 'react'
import { useTranslation } from 'react-i18next'
import { Link, Navigate } from 'react-router-dom'
 
import { Button } from '@/components/ui/button'
import { useQueryClient } from '@tanstack/react-query'
import { useSelectedBabyStore } from '@/shared/stores/selectedBaby.store'
import { cn } from '@/lib/utils'
import { EmptyState } from '@/shared/components/EmptyState'
import { PlusIcon } from '@/shared/icons/plus-icon'
import { PrinterIcon } from '@/shared/icons/printer-icon'
import { SyringeIcon } from '@/shared/icons/syringe-icon'
 
import { useAllBabiesVaccineCalendars } from '../api/vaccines.hooks'
import type { VaccineStatus } from '../api/vaccines.schemas'
import { AdhocVaccineList } from '../components/AdhocVaccineList'
import { RegisterVaccineDialog } from '../components/RegisterVaccineDialog'
import { VaccineCalendarList } from '../components/VaccineCalendarList'
import { VaccineCalendarSkeleton } from '../components/VaccineCalendarSkeleton'
import { VaccineCatalogNotice } from '../components/VaccineCatalogNotice'
import { VaccineProgressCard } from '../components/VaccineProgressCard'
import { vaccineProgress } from '../components/vaccine-progress'
 
type Filter = 'ALL' | VaccineStatus
 
export function VaccinesRoute() {
  const { t } = useTranslation()
  const calendar = useAllBabiesVaccineCalendars()
  const selectedId = useSelectedBabyStore((state) => state.selectedBabyId)
  const client = useQueryClient()
  const { isEmpty, metadata } = calendar
  const babies = calendar.babies.filter((baby) => !selectedId || baby.id === selectedId)
  const entries = calendar.perBaby.filter((entry) => !selectedId || entry.baby.id === selectedId)
  const destination = useSearchDestination('/vaccines')
  const items = entries
    .filter((entry) => !entry.isError)
    .flatMap((entry) => entry.items)
    .filter(
      (item) =>
        !destination ||
        destination.keys.includes(`vaccine:${item.babyId}:${item.vaccineId}`) ||
        destination.keys.includes(`vaccine:${item.babyId}:${item.vaccineId}:${item.doseNumber}`),
    )
  const isPending = !calendar.babies.length
    ? calendar.isPending
    : entries.some((entry) => entry.isPending)
  const isError = !calendar.babies.length
    ? calendar.isError
    : entries.some((entry) => entry.isError)
  const retry = () => {
    if (!calendar.babies.length) void client.invalidateQueries({ queryKey: ['babies'] })
    entries
      .filter((entry) => entry.isError)
      .forEach(
        (entry) =>
          void client.invalidateQueries({
            queryKey: ['babies', entry.baby.id, 'vaccines'],
          }),
      )
  }
  const [filter, setFilter] = useState<Filter>('ALL')
  const [isRegisterOpen, setRegisterOpen] = useState(false)
 
  Iif (isEmpty) {
    return <Navigate to="/dashboard" replace />
  }
 
  const counts = {
    APPLIED: items.filter((item) => item.status === 'APPLIED').length,
    PENDING: items.filter((item) => item.status === 'PENDING').length,
    DELAYED: items.filter((item) => item.status === 'DELAYED').length,
    GUIDANCE: items.filter((item) => item.status === 'GUIDANCE').length,
  }
 
  const filteredItems = (
    filter === 'ALL' ? items : items.filter((item) => item.status === filter)
  ).sort((a, b) => {
    const rank = { DELAYED: 0, GUIDANCE: 1, PENDING: 2, APPLIED: 3 } as const
    Eif (a.status !== b.status) return rank[a.status] - rank[b.status]
    return a.recommendedAgeInMonths - b.recommendedAgeInMonths
  })
 
  return (
    <div>
      <div className="mb-6 flex flex-col justify-between gap-4 sm:flex-row sm:items-end">
        <div>
          <h1 className="font-display text-3xl font-extrabold text-ink">{t('vaccines.title')}</h1>
          <p className="mt-1 text-sm text-ink-muted">{t('vaccines.page.intro')}</p>
        </div>
        <Button
          type="button"
          size="cta"
          onClick={() => setRegisterOpen(true)}
          className="rounded-2xl shadow-lg shadow-emerald-900/20 active:scale-[0.98]"
        >
          <PlusIcon className="mr-2 h-5 w-5" />
          {t('vaccines.registerAction')}
        </Button>
      </div>
 
      <RegisterVaccineDialog open={isRegisterOpen} onOpenChange={setRegisterOpen} />
 
      {isError && (
        <div role="alert" className="mb-5 rounded-2xl border border-border bg-card p-4">
          <p className="text-sm text-ink-muted">
            {t('vaccines.genericError')}{' '}
            {entries
              .filter((entry) => entry.isError)
              .map((entry) => entry.baby.name)
              .join(', ')}
          </p>
          <button
            type="button"
            onClick={retry}
            className="mt-2 min-h-11 text-sm font-semibold text-primary"
          >
            {t('nav.shell.retry')}
          </button>
        </div>
      )}
      {destination?.source === 'notification' && !isPending && !isError && items.length === 0 && (
        <p role="status" className="rounded-2xl border border-border bg-card p-5 text-ink-muted">
          {t('notifications.recordUnavailable')}
        </p>
      )}
      {isPending && <VaccineCalendarSkeleton />}
      {!destination && !isPending && !isError && items.length === 0 && (
        <EmptyState
          icon={<SyringeIcon className="size-8" />}
          title={t('vaccines.empty.title')}
          description={t('vaccines.empty.description')}
          tone="emerald"
        />
      )}
      {items.length > 0 && (
        <>
          {!destination && !isPending && !isError && (
            <div className="mb-5">
              <VaccineProgressCard progress={vaccineProgress(items)} />
            </div>
          )}
          <div className="mb-6 flex flex-wrap gap-2">
            {babies.map((baby) => (
              <Link
                key={baby.id}
                to={`/vaccines/${baby.id}/card`}
                className="inline-flex min-h-11 items-center gap-1.5 rounded-xl bg-card px-3.5 py-1.5 text-[13px] font-semibold text-ink-muted shadow-sm transition-colors hover:bg-muted hover:text-ink"
              >
                <PrinterIcon className="h-3.5 w-3.5" />
                {t('vaccines.card.viewAction', { name: baby.name })}
              </Link>
            ))}
          </div>
 
          {/* Same treatment as the milestone category row and the shared
              BabyFilterChips: the group carries a name, each chip carries its
              pressed state. Selection was previously fill colour only. */}
          <div
            className="mb-6 flex flex-wrap gap-2"
            role="group"
            aria-label={t('vaccines.statusFilter.groupLabel')}
          >
            {(
              [
                ['ALL', t('vaccines.filterAll')],
                ['APPLIED', t('vaccines.filterApplied', { count: counts.APPLIED })],
                ['PENDING', t('vaccines.filterPending', { count: counts.PENDING })],
                ['DELAYED', t('vaccines.filterDelayed', { count: counts.DELAYED })],
                ['GUIDANCE', t('vaccines.filterGuidance', { count: counts.GUIDANCE })],
              ] as const
            ).map(([value, label]) => (
              <button
                key={value}
                type="button"
                aria-pressed={filter === value}
                onClick={() => setFilter(value)}
                className={cn(
                  'min-h-11 rounded-xl px-4 py-1.5 text-[13px] font-semibold transition-colors',
                  filter === value
                    ? 'bg-primary text-primary-foreground'
                    : 'bg-card text-ink-muted shadow-sm hover:bg-muted',
                )}
              >
                {label}
              </button>
            ))}
          </div>
 
          <VaccineCalendarList items={filteredItems} babies={babies} filterKey={filter} />
        </>
      )}
 
      {(!destination || destination.keys.some((key) => key.startsWith('adhoc:'))) && (
        <AdhocVaccineList babies={babies} />
      )}
      {metadata && (
        <details className="mt-6 rounded-2xl border border-border bg-card p-4">
          <summary className="cursor-pointer py-2 text-sm font-semibold text-ink">
            {t('vaccines.catalog.title')}
          </summary>
          <VaccineCatalogNotice metadata={metadata} className="mt-3" />
        </details>
      )}
    </div>
  )
}