Routes /app/animals, new, :animalId, edit under one workspace gate; list with species filter; form (D-R7, U3 birth, no default sex or role, optional first client); page with current and former relations, end, dates, void; client page links its animals, adds one pre-associated, links an existing one. Card now passes aria-labelledby, which it silently dropped.
99 lines
2.5 KiB
TypeScript
99 lines
2.5 KiB
TypeScript
import { useState } from 'react'
|
|
import {
|
|
classifyRecordError,
|
|
setAnimalArchived,
|
|
type RecordFailureKind,
|
|
} from '@faunosteo/supabase'
|
|
import Button from '@/components/ui/Button'
|
|
import Dialog from '@/components/ui/Dialog'
|
|
import Notice from '@/components/ui/Notice'
|
|
import { getSupabaseClient } from '@/lib/supabase'
|
|
import { useT } from '@/i18n/useT'
|
|
|
|
export interface ArchiveAnimalDialogProps {
|
|
open: boolean
|
|
animalId: string
|
|
onClose: () => void
|
|
/** Called once the server archived the animal; the page then closes the dialog. */
|
|
onArchived: () => void
|
|
}
|
|
|
|
/**
|
|
* Confirmation before archiving an animal (contract §7.1): its relations are
|
|
* kept and frozen, no client is archived. Closing it is never a
|
|
* confirmation, and while the request runs it refuses to close.
|
|
*/
|
|
export default function ArchiveAnimalDialog({
|
|
open,
|
|
animalId,
|
|
onClose,
|
|
onArchived,
|
|
}: ArchiveAnimalDialogProps) {
|
|
const t = useT()
|
|
const [pending, setPending] = useState(false)
|
|
const [failure, setFailure] = useState<RecordFailureKind | null>(null)
|
|
|
|
const close = () => {
|
|
if (pending) return
|
|
setFailure(null)
|
|
onClose()
|
|
}
|
|
|
|
async function confirm() {
|
|
const client = getSupabaseClient()
|
|
if (pending) return
|
|
if (!client) return setFailure('unavailable')
|
|
setPending(true)
|
|
setFailure(null)
|
|
try {
|
|
await setAnimalArchived(client, animalId, true)
|
|
onArchived()
|
|
} catch (error) {
|
|
setFailure(classifyRecordError(error))
|
|
} finally {
|
|
setPending(false)
|
|
}
|
|
}
|
|
|
|
return (
|
|
<Dialog
|
|
open={open}
|
|
onClose={close}
|
|
title={t('animals.archive.title')}
|
|
description={
|
|
<p className="text-sm text-[var(--color-ink-muted)]">
|
|
{t('animals.archive.body')}
|
|
</p>
|
|
}
|
|
size="sm"
|
|
>
|
|
{failure && (
|
|
<Notice tone="error" className="mt-3">
|
|
{t(`animals.failure.${failure}`)}
|
|
</Notice>
|
|
)}
|
|
<div className="mt-4 flex flex-wrap justify-end gap-2">
|
|
{/* Not disabled while pending: a button that turns disabled drops the focus out of the dialog. */}
|
|
<Button
|
|
type="button"
|
|
variant="ghost"
|
|
onClick={close}
|
|
aria-disabled={pending}
|
|
>
|
|
{t('common.cancel')}
|
|
</Button>
|
|
<Button
|
|
type="button"
|
|
variant="danger"
|
|
onClick={() => void confirm()}
|
|
aria-disabled={pending}
|
|
>
|
|
{pending
|
|
? t('animals.archive.archiving')
|
|
: t('animals.archive.confirm')}
|
|
</Button>
|
|
</div>
|
|
</Dialog>
|
|
)
|
|
}
|