feat(app): clients list, page, create, edit, archive and restore
Routes /app/clients, /new, /:clientId, /:clientId/edit behind a workspace gate that remounts per workspace; debounced POST search, archived toggle and « Afficher plus » pages; form on field-limits (D-R7) with the phone entry rule and ISO countries; request id per creation intent; conflict shows the saved values; archive confirmation with the sole-owner preview; animals read-only; leave guard on in-app links; Clients entry after the dashboard.
This commit is contained in:
1 parent
ff028ec071
commit
b38e62770e
25 files changed
+2744
-5
No files matched your search
@@ -0,0 +1,160 @@
|
||||
import { useEffect, useState } from 'react'
|
||||
import {
|
||||
classifyRecordError,
|
||||
fetchClientArchivePreview,
|
||||
setClientArchived,
|
||||
type ClientArchivePreview,
|
||||
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 ArchiveClientDialogProps {
|
||||
open: boolean
|
||||
clientId: string
|
||||
onClose: () => void
|
||||
/** Called once the server archived the client; the page then closes the dialog. */
|
||||
onArchived: () => void
|
||||
}
|
||||
|
||||
type Preview =
|
||||
| { status: 'loading' }
|
||||
| { status: 'ready'; preview: ClientArchivePreview }
|
||||
| { status: 'failed' }
|
||||
|
||||
/**
|
||||
* Confirmation before archiving (contract §7.1): it lists the active animals
|
||||
* for which this client is the only current owner, then archives through
|
||||
* set_client_archived. Closing it is never a confirmation, and while the
|
||||
* request runs it refuses to close, so focus is restored once, after it.
|
||||
*/
|
||||
export default function ArchiveClientDialog({
|
||||
open,
|
||||
clientId,
|
||||
onClose,
|
||||
onArchived,
|
||||
}: ArchiveClientDialogProps) {
|
||||
const t = useT()
|
||||
const [preview, setPreview] = useState<Preview>({ status: 'loading' })
|
||||
const [pending, setPending] = useState(false)
|
||||
const [failure, setFailure] = useState<RecordFailureKind | null>(null)
|
||||
|
||||
useEffect(() => {
|
||||
if (!open) return
|
||||
let live = true
|
||||
setPreview({ status: 'loading' })
|
||||
setFailure(null)
|
||||
const client = getSupabaseClient()
|
||||
if (!client) {
|
||||
setPreview({ status: 'failed' })
|
||||
return
|
||||
}
|
||||
fetchClientArchivePreview(client, clientId)
|
||||
.then((result) => {
|
||||
if (live) setPreview({ status: 'ready', preview: result })
|
||||
})
|
||||
.catch(() => {
|
||||
if (live) setPreview({ status: 'failed' })
|
||||
})
|
||||
return () => {
|
||||
live = false
|
||||
}
|
||||
}, [open, clientId])
|
||||
|
||||
const close = () => {
|
||||
if (!pending) onClose()
|
||||
}
|
||||
|
||||
async function confirm() {
|
||||
const client = getSupabaseClient()
|
||||
if (pending) return
|
||||
if (!client) return setFailure('unavailable')
|
||||
setPending(true)
|
||||
setFailure(null)
|
||||
try {
|
||||
await setClientArchived(client, clientId, true)
|
||||
onArchived()
|
||||
} catch (error) {
|
||||
setFailure(classifyRecordError(error))
|
||||
} finally {
|
||||
setPending(false)
|
||||
}
|
||||
}
|
||||
|
||||
const sole = preview.status === 'ready' ? preview.preview : null
|
||||
const others = sole ? sole.count - sole.animals.length : 0
|
||||
|
||||
return (
|
||||
<Dialog
|
||||
open={open}
|
||||
onClose={close}
|
||||
title={t('clients.archive.title')}
|
||||
description={
|
||||
<p className="text-sm text-[var(--color-ink-muted)]">
|
||||
{t('clients.archive.body')}
|
||||
</p>
|
||||
}
|
||||
size="sm"
|
||||
>
|
||||
<div className="mt-3 space-y-3 text-sm">
|
||||
{preview.status === 'loading' && (
|
||||
<p role="status" className="text-[var(--color-ink-muted)]">
|
||||
{t('clients.archive.preview_loading')}
|
||||
</p>
|
||||
)}
|
||||
{preview.status === 'failed' && (
|
||||
<Notice tone="info">{t('clients.archive.preview_failed')}</Notice>
|
||||
)}
|
||||
{sole && sole.count > 0 && (
|
||||
<div className="space-y-1">
|
||||
<p>
|
||||
{sole.count === 1
|
||||
? t('clients.archive.sole_owner_one')
|
||||
: t('clients.archive.sole_owner_other').replace(
|
||||
'{count}',
|
||||
String(sole.count),
|
||||
)}
|
||||
</p>
|
||||
<ul className="list-disc pl-5">
|
||||
{sole.animals.map((animal) => (
|
||||
<li key={animal.id}>{animal.name}</li>
|
||||
))}
|
||||
</ul>
|
||||
{others > 0 && (
|
||||
<p>
|
||||
{t('clients.archive.others').replace('{count}', String(others))}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
{failure && (
|
||||
<Notice tone="error">{t(`clients.failure.${failure}`)}</Notice>
|
||||
)}
|
||||
</div>
|
||||
<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('clients.archive.archiving')
|
||||
: t('clients.archive.confirm')}
|
||||
</Button>
|
||||
</div>
|
||||
</Dialog>
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user