All files / src/features/legal/components LegalAcceptanceGate.tsx

72.72% Statements 8/11
77.77% Branches 7/9
50% Functions 2/4
80% Lines 8/10

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                      11x 11x 11x   11x           11x 8x     3x                   5x                                                                                          
import { useTranslation } from 'react-i18next'
import { Link } from 'react-router-dom'
import type { ReactNode } from 'react'
 
import { Button } from '@/components/ui/button'
import { BrandMark } from '@/shared/components/BrandMark'
 
import { documentsAwaitingAcceptance, useLegalAcceptances, useRecordLegalAcceptance } from '../api/legal.hooks'
 
/** Versioned document acceptance, separate from consent for specific data processing. */
export function LegalAcceptanceGate({ children }: { children: ReactNode }) {
  const { t } = useTranslation()
  const acceptances = useLegalAcceptances()
  const record = useRecordLegalAcceptance()
 
  const pending = documentsAwaitingAcceptance(acceptances.data)
 
  // While the answer is unknown, the app renders. A network hiccup on this
  // query must not lock somebody out of a health record they already have — the
  // gate exists to collect consent, not to hold data hostage. `isPending` is
  // false when the query is disabled, which is the inert case.
  if (acceptances.isPending || acceptances.isError || pending.length === 0) {
    return <>{children}</>
  }
 
  return (
    <main className="bg-surface flex min-h-dvh flex-col items-center justify-center px-5 py-10">
      <div className="bg-card border-border/70 w-full max-w-lg rounded-3xl border p-8 shadow-xl">
        <BrandMark className="size-11" />
 
        <h1 className="font-display text-ink mt-6 text-2xl font-extrabold">{t('legal.gate.title')}</h1>
        <p className="text-ink-muted mt-2 text-sm leading-relaxed">{t('legal.gate.description')}</p>
 
        <ul className="mt-6 space-y-3">
          {pending.map((document) => (
            <li key={document.id} className="border-border rounded-2xl border p-4">
              <div className="flex flex-wrap items-baseline justify-between gap-2">
                <Link
                  to={document.path}
                  className="text-primary font-semibold underline-offset-4 hover:underline"
                >
                  {t(`legal.${document.id}.title`)}
                </Link>
                <span className="text-ink-faint text-xs">
                  {t('legal.gate.versionLabel', { version: document.version })}
                </span>
              </div>
              <p className="text-ink-muted mt-1 text-xs">{t('legal.gate.readFirst')}</p>
            </li>
          ))}
        </ul>
 
        {record.isError && (
          <p role="alert" className="text-destructive mt-4 text-sm">
            {t('legal.gate.genericError')}
          </p>
        )}
 
        <Button
          type="button"
          size="cta"
          className="mt-6 w-full"
          disabled={record.isPending}
          onClick={async () => {
            // One at a time, and sequentially: the API records one document per
            // request, and a failure halfway through must leave the accepted
            // ones accepted. Each is a fact about the past on its own.
            for (const document of pending) {
              await record.mutateAsync(document.id).catch(() => null)
            }
          }}
        >
          {record.isPending ? t('common.saving') : t('legal.gate.acceptAction')}
        </Button>
 
        <p className="text-ink-faint mt-4 text-center text-xs">{t('legal.gate.noWayAround')}</p>
      </div>
    </main>
  )
}