All files / src/features/appointments/components AppointmentsList.tsx

74.19% Statements 23/31
57.69% Branches 15/26
52.94% Functions 9/17
73.33% Lines 22/30

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                                                  3x 3x 3x     3x 3x 3x 3x 3x 5x   3x 4x     3x 3x           3x 3x               3x   5x     3x               15x                   16x                                                           4x               4x                                              
import { useState } from 'react'
import { appointmentGroup, sortAppointments, type AppointmentView } from './appointment-view'
import { useTranslation } from 'react-i18next'
 
import type { Baby } from '@/features/babies/api/babies.schemas'
import { useDebouncedValue } from '@/hooks/useDebouncedValue'
import { usePagedList } from '@/hooks/usePagedList'
import { LoadMoreButton } from '@/shared/components/LoadMoreButton'
import { NoSearchResults } from '@/shared/components/NoSearchResults'
import { SearchInput } from '@/shared/components/SearchInput'
 
import type { Appointment } from '../api/appointments.schemas'
import { AppointmentCard } from './AppointmentCard'
import { AppointmentDetailDialog } from './AppointmentDetailDialog'
import { RescheduleDialog } from './RescheduleDialog'
 
interface AppointmentsListProps {
  items: Appointment[]
  babies: Baby[]
}
 
// Renders a single, merged, household-wide grid — each card tagged with which
// baby it belongs to, so a family with several children sees one chronological
// list instead of one full section repeated per child.
export function AppointmentsList({ items, babies }: AppointmentsListProps) {
  const { t } = useTranslation()
  const [rescheduleTarget, setRescheduleTarget] = useState<Appointment | null>(null)
  const [detailTarget, setDetailTarget] = useState<Appointment | null>(null)
  // The child filter is the menu's, not this page's: the choice outlives the page
  // it was made on. See selectedBaby.store.ts.
  const [filter, setFilter] = useState<AppointmentView>('ALL')
  const now = Date.now()
  const groups = ['ALL', 'UPCOMING', 'REVIEW', 'COMPLETED', 'CANCELLED'] as const
  const [search, setSearch] = useState('')
  const debouncedSearch = useDebouncedValue(search)
  const babyById = new Map(babies.map((baby) => [baby.id, baby]))
 
  const babyFiltered = sortAppointments(
    items.filter((item) => filter === 'ALL' || appointmentGroup(item, now) === filter),
    now,
  )
  const normalize = (value: string) =>
    value
      .normalize('NFD')
      .replace(/[\u0300-\u036f]/g, '')
      .trim()
      .replace(/\s+/g, ' ')
      .toLowerCase()
  const normalizedSearch = normalize(debouncedSearch)
  const filteredItems = normalizedSearch
    ? babyFiltered.filter((item) =>
        [item.doctorName, item.specialty, item.location, item.reason]
          .filter((value): value is string => !!value)
          .some((value) => normalize(value).includes(normalizedSearch)),
      )
    : babyFiltered
 
  const { visibleItems, hasMore, loadMore } = usePagedList(
    filteredItems,
    `${normalizedSearch}|${filter}|${babies.map((baby) => baby.id).join()}`,
  )
 
  return (
    <div>
      <div
        role="group"
        aria-label={t('appointments.page.filters')}
        className="mb-4 flex flex-wrap gap-2"
      >
        {groups.map((group) => (
          <button
            key={group}
            type="button"
            aria-pressed={filter === group}
            onClick={() => setFilter(group)}
            className={`min-h-11 rounded-xl px-4 py-2 text-sm font-semibold ${filter === group ? 'bg-violet-700 text-white' : 'bg-card text-ink-muted hover:bg-muted'}`}
          >
            {t(`appointments.page.${group}`)} (
            {group === 'ALL'
              ? items.length
              : items.filter((item) => appointmentGroup(item, now) === group).length}
            )
          </button>
        ))}
      </div>
      <div className="mb-6 flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
        <SearchInput
          id="appointment-search"
          label={t('appointments.search.label')}
          value={search}
          onChange={setSearch}
          placeholder={t('appointments.page.search')}
          clearLabel={t('vaccines.searchUi.clear')}
          inputClassName="h-12 rounded-xl"
          className="w-full sm:max-w-lg"
        />
      </div>
 
      {filteredItems.length === 0 ? (
        normalizedSearch ? (
          <NoSearchResults />
        ) : (
          <p role="status" className="rounded-2xl bg-card p-8 text-center text-sm text-ink-muted">
            {t('appointments.page.noFilter')}
          </p>
        )
      ) : (
        <>
          <div className="grid grid-cols-1 gap-4 lg:grid-cols-2">
            {visibleItems.map((appointment) => (
              <AppointmentCard
                key={appointment.id}
                appointment={appointment}
                baby={babies.length > 1 ? babyById.get(appointment.babyId) : undefined}
                featured={
                  !normalizedSearch &&
                  (filter === 'ALL' || filter === 'UPCOMING') &&
                  appointment.id ===
                    babyFiltered.find((item) => appointmentGroup(item, now) === 'UPCOMING')?.id
                }
                onReschedule={() => setRescheduleTarget(appointment)}
                onViewDetails={() => setDetailTarget(appointment)}
              />
            ))}
          </div>
          {hasMore && <LoadMoreButton onClick={loadMore} label={t('common.loadMore')} />}
        </>
      )}
 
      <RescheduleDialog
        appointment={rescheduleTarget}
        onOpenChange={(open) => !open && setRescheduleTarget(null)}
      />
      <AppointmentDetailDialog
        baby={babyById.get(detailTarget?.babyId ?? '')}
        appointment={detailTarget}
        onOpenChange={(open) => !open && setDetailTarget(null)}
      />
    </div>
  )
}