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>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,201 @@
|
||||
import { useEffect, useState } from 'react'
|
||||
import {
|
||||
classifyRecordError,
|
||||
fetchSpeciesLabels,
|
||||
listClientAnimals,
|
||||
type ClientAnimalItem,
|
||||
type RecordFailureKind,
|
||||
type RelationCursor,
|
||||
type SpeciesLabel,
|
||||
} from '@faunosteo/supabase'
|
||||
import Badge from '@/components/ui/Badge'
|
||||
import Button from '@/components/ui/Button'
|
||||
import Card from '@/components/ui/Card'
|
||||
import Notice from '@/components/ui/Notice'
|
||||
import { getSupabaseClient } from '@/lib/supabase'
|
||||
import { formatDate } from '@/lib/dates'
|
||||
import { pickLocaleLabel } from '@/i18n/pickLocaleLabel'
|
||||
import { useLocaleStore } from '@/state/localeStore'
|
||||
import { useT } from '@/i18n/useT'
|
||||
|
||||
type State =
|
||||
| { status: 'loading' }
|
||||
| {
|
||||
status: 'ready'
|
||||
rows: ClientAnimalItem[]
|
||||
next: RelationCursor | null
|
||||
species: SpeciesLabel[]
|
||||
}
|
||||
| { status: 'error'; failure: RecordFailureKind }
|
||||
|
||||
async function loadPage(
|
||||
clientId: string,
|
||||
after: RelationCursor | null,
|
||||
species: SpeciesLabel[] | null,
|
||||
) {
|
||||
const client = getSupabaseClient()
|
||||
if (!client) throw new TypeError('no backend')
|
||||
const page = await listClientAnimals(client, clientId, { after })
|
||||
// Species names are read only when there is an animal to name.
|
||||
const labels =
|
||||
species ?? (page.rows.length ? await fetchSpeciesLabels(client) : [])
|
||||
return { ...page, species: labels }
|
||||
}
|
||||
|
||||
/**
|
||||
* The client's animals, read-only in V1-1.5 (owner choice 2026-10-26): the
|
||||
* real relations from list_client_animals, current then former, each with
|
||||
* its role. No link and no « ajouter » until the animal screens exist.
|
||||
*/
|
||||
export default function ClientAnimals({ clientId }: { clientId: string }) {
|
||||
const t = useT()
|
||||
const locale = useLocaleStore((s) => s.locale)
|
||||
const [state, setState] = useState<State>({ status: 'loading' })
|
||||
const [loadingMore, setLoadingMore] = useState(false)
|
||||
|
||||
useEffect(() => {
|
||||
let live = true
|
||||
loadPage(clientId, null, null)
|
||||
.then((page) => live && setState({ status: 'ready', ...page }))
|
||||
.catch(
|
||||
(error: unknown) =>
|
||||
live &&
|
||||
setState({ status: 'error', failure: classifyRecordError(error) }),
|
||||
)
|
||||
return () => {
|
||||
live = false
|
||||
}
|
||||
}, [clientId])
|
||||
|
||||
async function more() {
|
||||
if (state.status !== 'ready' || !state.next) return
|
||||
setLoadingMore(true)
|
||||
try {
|
||||
const page = await loadPage(clientId, state.next, state.species)
|
||||
setState({
|
||||
...state,
|
||||
rows: [...state.rows, ...page.rows],
|
||||
next: page.next,
|
||||
})
|
||||
} catch (error) {
|
||||
setState({ status: 'error', failure: classifyRecordError(error) })
|
||||
} finally {
|
||||
setLoadingMore(false)
|
||||
}
|
||||
}
|
||||
|
||||
const speciesName = (row: ClientAnimalItem, species: SpeciesLabel[]) => {
|
||||
const label = species.find((s) => s.id === row.species_id)
|
||||
const name = label
|
||||
? pickLocaleLabel(label.name_fr, label.name_en, locale, row.species_id)
|
||||
: row.species_id
|
||||
return row.species_detail ? `${name} (${row.species_detail})` : name
|
||||
}
|
||||
const period = (row: ClientAnimalItem) =>
|
||||
[
|
||||
row.started_on &&
|
||||
t('clients.animals.since').replace(
|
||||
'{date}',
|
||||
formatDate(row.started_on, locale),
|
||||
),
|
||||
row.ended_on &&
|
||||
t('clients.animals.until').replace(
|
||||
'{date}',
|
||||
formatDate(row.ended_on, locale),
|
||||
),
|
||||
]
|
||||
.filter(Boolean)
|
||||
.join(' · ')
|
||||
|
||||
function group(
|
||||
rows: ClientAnimalItem[],
|
||||
species: SpeciesLabel[],
|
||||
current: boolean,
|
||||
) {
|
||||
const shown = rows.filter((r) => r.is_current === current)
|
||||
if (!shown.length) return null
|
||||
const id = current ? 'client-animals-current' : 'client-animals-former'
|
||||
return (
|
||||
<section aria-labelledby={id} className="space-y-2">
|
||||
<h3
|
||||
id={id}
|
||||
className="text-sm font-medium text-[var(--color-ink-muted)]"
|
||||
>
|
||||
{t(current ? 'clients.animals.current' : 'clients.animals.former')}
|
||||
</h3>
|
||||
<ul className="divide-y divide-[var(--color-border)] rounded-xl border border-[var(--color-border)] bg-white">
|
||||
{shown.map((row) => (
|
||||
<li
|
||||
key={row.relation_id}
|
||||
className="flex flex-wrap items-baseline justify-between gap-x-3 gap-y-1 px-4 py-3"
|
||||
>
|
||||
<span className="min-w-0">
|
||||
<span className="font-medium text-[var(--color-ink)]">
|
||||
{row.animal_name}
|
||||
</span>{' '}
|
||||
<span className="text-sm text-[var(--color-ink-muted)]">
|
||||
{speciesName(row, species)}
|
||||
</span>
|
||||
{row.animal_archived_at && (
|
||||
<Badge className="ml-2">{t('clients.archived_badge')}</Badge>
|
||||
)}
|
||||
</span>
|
||||
<span className="text-sm text-[var(--color-ink-muted)]">
|
||||
{t(
|
||||
`clients.role.${current ? '' : 'former_'}${row.relation_role}`,
|
||||
)}
|
||||
{period(row) && ` · ${period(row)}`}
|
||||
</span>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
</section>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<Card
|
||||
as="section"
|
||||
aria-labelledby="client-animals-title"
|
||||
className="space-y-4 p-5 sm:p-6"
|
||||
>
|
||||
<h2
|
||||
id="client-animals-title"
|
||||
className="font-serif text-xl text-[var(--color-ink)]"
|
||||
>
|
||||
{t('clients.animals.title')}
|
||||
</h2>
|
||||
{state.status === 'loading' && (
|
||||
<p role="status" className="text-sm text-[var(--color-ink-muted)]">
|
||||
{t('clients.animals.loading')}
|
||||
</p>
|
||||
)}
|
||||
{state.status === 'error' && (
|
||||
<Notice tone="error">{t(`clients.failure.${state.failure}`)}</Notice>
|
||||
)}
|
||||
{state.status === 'ready' && state.rows.length === 0 && (
|
||||
<p className="text-sm text-[var(--color-ink-muted)]">
|
||||
{t('clients.animals.none')}
|
||||
</p>
|
||||
)}
|
||||
{state.status === 'ready' && (
|
||||
<>
|
||||
{group(state.rows, state.species, true)}
|
||||
{group(state.rows, state.species, false)}
|
||||
{state.next && (
|
||||
<Button
|
||||
type="button"
|
||||
variant="secondary"
|
||||
onClick={() => void more()}
|
||||
disabled={loadingMore}
|
||||
>
|
||||
{loadingMore
|
||||
? t('clients.list.loading_more')
|
||||
: t('clients.list.more')}
|
||||
</Button>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
</Card>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,305 @@
|
||||
import { useEffect, useMemo } from 'react'
|
||||
import { Link } from 'react-router-dom'
|
||||
import { CLIENT_FIELD_LIMITS } from '@faunosteo/supabase'
|
||||
import Button, { buttonClassName } from '@/components/ui/Button'
|
||||
import Card from '@/components/ui/Card'
|
||||
import FormField, { fieldDescribedBy } from '@/components/ui/FormField'
|
||||
import Input from '@/components/ui/Input'
|
||||
import Notice from '@/components/ui/Notice'
|
||||
import Select from '@/components/ui/Select'
|
||||
import { countryName, countryOptions } from '@/clients/countries'
|
||||
import {
|
||||
EMPTY_CLIENT_VALUES,
|
||||
type ClientFormValues,
|
||||
} from '@/clients/validation'
|
||||
import type { ClientForm as ClientFormState } from '@/clients/useClientForm'
|
||||
import { translateFieldError } from '@/account/validation'
|
||||
import { useLocaleStore } from '@/state/localeStore'
|
||||
import { useT } from '@/i18n/useT'
|
||||
|
||||
const FIELD_ORDER = Object.keys(
|
||||
EMPTY_CLIENT_VALUES,
|
||||
) as (keyof ClientFormValues)[]
|
||||
const fieldId = (key: keyof ClientFormValues) =>
|
||||
`client-${key.replace('_', '-')}`
|
||||
|
||||
export interface ClientFormProps {
|
||||
form: ClientFormState
|
||||
submitLabel: string
|
||||
/** Where « Annuler » leads; the shell asks first while the form holds changes. */
|
||||
cancelTo: string
|
||||
}
|
||||
|
||||
/**
|
||||
* The client form, one page on every screen (contract §11.3, §11.4): the kind
|
||||
* first with « Personne » selected, the name whose label follows the kind,
|
||||
* then contact and address. Every field is labelled, errors sit under their
|
||||
* field and the first one takes focus after a refused submit.
|
||||
*/
|
||||
export default function ClientForm({
|
||||
form,
|
||||
submitLabel,
|
||||
cancelTo,
|
||||
}: ClientFormProps) {
|
||||
const t = useT()
|
||||
const locale = useLocaleStore((s) => s.locale)
|
||||
const countries = useMemo(() => countryOptions(locale), [locale])
|
||||
const { values, errors, phase, stored } = form
|
||||
|
||||
useEffect(() => {
|
||||
if (form.errorRound === 0) return
|
||||
const first = FIELD_ORDER.find((key) => errors[key])
|
||||
if (first) document.getElementById(fieldId(first))?.focus()
|
||||
}, [form.errorRound, errors])
|
||||
|
||||
const err = (key: keyof ClientFormValues) =>
|
||||
translateFieldError(t, errors[key])
|
||||
/** After a conflict: the stored value under each field that differs from the typed one. */
|
||||
const storedHint = (key: keyof ClientFormValues): string | undefined => {
|
||||
if (!stored) return undefined
|
||||
const raw = key === 'country' ? stored.country : stored[key]
|
||||
const value = raw ?? ''
|
||||
if (value === values[key]) return undefined
|
||||
const shown =
|
||||
key === 'country' && value
|
||||
? countryName(value, locale)
|
||||
: key === 'kind'
|
||||
? t(`clients.kind.${value}`)
|
||||
: value
|
||||
return shown
|
||||
? t('clients.form.stored_value').replace('{value}', shown)
|
||||
: t('clients.form.stored_empty')
|
||||
}
|
||||
const describedBy = (key: keyof ClientFormValues, hint?: string) =>
|
||||
fieldDescribedBy(fieldId(key), hint, errors[key])
|
||||
|
||||
function textField(
|
||||
key: keyof ClientFormValues,
|
||||
label: string,
|
||||
max: number,
|
||||
extra: Record<string, unknown> = {},
|
||||
) {
|
||||
const hint = storedHint(key)
|
||||
return (
|
||||
<FormField
|
||||
id={fieldId(key)}
|
||||
label={label}
|
||||
optional={t('clients.form.optional')}
|
||||
hint={hint}
|
||||
error={err(key)}
|
||||
>
|
||||
<Input
|
||||
id={fieldId(key)}
|
||||
value={values[key]}
|
||||
onChange={(e) => form.setField(key)(e.target.value)}
|
||||
maxLength={max}
|
||||
aria-invalid={Boolean(errors[key])}
|
||||
aria-describedby={describedBy(key, hint)}
|
||||
{...extra}
|
||||
/>
|
||||
</FormField>
|
||||
)
|
||||
}
|
||||
|
||||
const nameKey = values.kind === 'organization' ? 'organization' : 'person'
|
||||
const nameHint =
|
||||
storedHint('display_name') ?? t(`clients.form.display_name_hint.${nameKey}`)
|
||||
const saving = phase.kind === 'saving'
|
||||
|
||||
return (
|
||||
<form onSubmit={form.submit} noValidate className="space-y-6">
|
||||
{stored && (
|
||||
<Notice tone="error">
|
||||
<p className="font-medium">{t('clients.form.conflict_title')}</p>
|
||||
<p>{t('clients.form.conflict_body')}</p>
|
||||
<Button
|
||||
type="button"
|
||||
variant="secondary"
|
||||
size="sm"
|
||||
className="mt-2"
|
||||
onClick={form.takeStored}
|
||||
>
|
||||
{t('clients.form.conflict_take_stored')}
|
||||
</Button>
|
||||
</Notice>
|
||||
)}
|
||||
<Card as="section" className="space-y-5 p-5 sm:p-6">
|
||||
<fieldset className="space-y-2">
|
||||
<legend className="text-sm font-medium text-[var(--color-ink)]">
|
||||
{t('clients.form.kind_legend')}
|
||||
</legend>
|
||||
<div className="grid grid-cols-2 gap-2 sm:inline-grid sm:w-auto">
|
||||
{(['person', 'organization'] as const).map((kind) => (
|
||||
<label
|
||||
key={kind}
|
||||
className="flex min-h-11 cursor-pointer items-center justify-center gap-2 rounded-lg border border-[var(--color-border)] bg-white px-4 text-sm text-[var(--color-ink)] has-[:checked]:border-[var(--color-accent)] has-[:checked]:bg-[var(--color-accent)]/10 has-[:checked]:font-medium has-[:focus-visible]:ring-2 has-[:focus-visible]:ring-[var(--color-accent)]"
|
||||
>
|
||||
<input
|
||||
type="radio"
|
||||
name="client-kind"
|
||||
value={kind}
|
||||
checked={values.kind === kind}
|
||||
onChange={() => form.setField('kind')(kind)}
|
||||
className="h-4 w-4 accent-[var(--color-accent)]"
|
||||
/>
|
||||
{t(`clients.kind.${kind}`)}
|
||||
</label>
|
||||
))}
|
||||
</div>
|
||||
{storedHint('kind') && (
|
||||
<p className="text-xs text-[var(--color-ink-muted)]">
|
||||
{storedHint('kind')}
|
||||
</p>
|
||||
)}
|
||||
</fieldset>
|
||||
<FormField
|
||||
id={fieldId('display_name')}
|
||||
label={t(`clients.form.display_name.${nameKey}`)}
|
||||
optional={t('clients.form.required')}
|
||||
hint={nameHint}
|
||||
error={err('display_name')}
|
||||
>
|
||||
<Input
|
||||
id={fieldId('display_name')}
|
||||
value={values.display_name}
|
||||
onChange={(e) => form.setField('display_name')(e.target.value)}
|
||||
maxLength={CLIENT_FIELD_LIMITS.displayName}
|
||||
autoComplete="off"
|
||||
aria-invalid={Boolean(errors.display_name)}
|
||||
aria-describedby={describedBy('display_name', nameHint)}
|
||||
required
|
||||
/>
|
||||
</FormField>
|
||||
</Card>
|
||||
|
||||
<Card
|
||||
as="section"
|
||||
aria-labelledby="client-contact-title"
|
||||
className="space-y-4 p-5 sm:p-6"
|
||||
>
|
||||
<h2
|
||||
id="client-contact-title"
|
||||
className="font-serif text-xl text-[var(--color-ink)]"
|
||||
>
|
||||
{t('clients.form.contact_title')}
|
||||
</h2>
|
||||
<div className="grid gap-4 sm:grid-cols-2">
|
||||
{textField(
|
||||
'email',
|
||||
t('clients.form.email'),
|
||||
CLIENT_FIELD_LIMITS.email,
|
||||
{
|
||||
type: 'email',
|
||||
inputMode: 'email',
|
||||
autoComplete: 'off',
|
||||
},
|
||||
)}
|
||||
<FormField
|
||||
id={fieldId('phone')}
|
||||
label={t('clients.form.phone')}
|
||||
optional={t('clients.form.optional')}
|
||||
hint={storedHint('phone') ?? t('clients.form.phone_hint')}
|
||||
error={err('phone')}
|
||||
>
|
||||
<Input
|
||||
id={fieldId('phone')}
|
||||
type="tel"
|
||||
inputMode="tel"
|
||||
autoComplete="off"
|
||||
value={values.phone}
|
||||
onChange={(e) => form.setField('phone')(e.target.value)}
|
||||
onBlur={form.convertPhone}
|
||||
maxLength={32}
|
||||
aria-invalid={Boolean(errors.phone)}
|
||||
aria-describedby={describedBy('phone', 'hint')}
|
||||
/>
|
||||
</FormField>
|
||||
</div>
|
||||
</Card>
|
||||
|
||||
<Card
|
||||
as="section"
|
||||
aria-labelledby="client-address-title"
|
||||
className="space-y-4 p-5 sm:p-6"
|
||||
>
|
||||
<h2
|
||||
id="client-address-title"
|
||||
className="font-serif text-xl text-[var(--color-ink)]"
|
||||
>
|
||||
{t('clients.form.address_title')}
|
||||
</h2>
|
||||
{textField(
|
||||
'address_line1',
|
||||
t('clients.form.address_line1'),
|
||||
CLIENT_FIELD_LIMITS.addressLine,
|
||||
{
|
||||
autoComplete: 'off',
|
||||
},
|
||||
)}
|
||||
{textField(
|
||||
'address_line2',
|
||||
t('clients.form.address_line2'),
|
||||
CLIENT_FIELD_LIMITS.addressLine,
|
||||
{
|
||||
autoComplete: 'off',
|
||||
},
|
||||
)}
|
||||
<div className="grid gap-4 sm:grid-cols-[10rem_1fr]">
|
||||
{textField(
|
||||
'postal_code',
|
||||
t('clients.form.postal_code'),
|
||||
CLIENT_FIELD_LIMITS.postalCode,
|
||||
{
|
||||
autoComplete: 'off',
|
||||
},
|
||||
)}
|
||||
{textField('city', t('clients.form.city'), CLIENT_FIELD_LIMITS.city, {
|
||||
autoComplete: 'off',
|
||||
})}
|
||||
</div>
|
||||
<FormField
|
||||
id={fieldId('country')}
|
||||
label={t('clients.form.country')}
|
||||
optional={t('clients.form.optional')}
|
||||
hint={storedHint('country')}
|
||||
error={err('country')}
|
||||
>
|
||||
<Select
|
||||
id={fieldId('country')}
|
||||
value={values.country}
|
||||
onChange={(e) => form.setField('country')(e.target.value)}
|
||||
aria-invalid={Boolean(errors.country)}
|
||||
aria-describedby={describedBy('country', storedHint('country'))}
|
||||
>
|
||||
<option value="">{t('clients.form.country_none')}</option>
|
||||
{countries.map((c) => (
|
||||
<option key={c.code} value={c.code}>
|
||||
{c.label}
|
||||
</option>
|
||||
))}
|
||||
</Select>
|
||||
</FormField>
|
||||
</Card>
|
||||
|
||||
{phase.kind === 'failed' && !stored && (
|
||||
<Notice tone="error">{t(`clients.failure.${phase.failure}`)}</Notice>
|
||||
)}
|
||||
<div className="sticky bottom-0 -mx-4 flex flex-wrap items-center justify-end gap-3 border-t border-[var(--color-border)] bg-[var(--color-bg)] px-4 py-3 sm:static sm:mx-0 sm:border-0 sm:bg-transparent sm:px-0 sm:py-0">
|
||||
<Link
|
||||
to={cancelTo}
|
||||
className={buttonClassName('ghost', 'md', 'min-h-11')}
|
||||
>
|
||||
{t('common.cancel')}
|
||||
</Link>
|
||||
<Button
|
||||
type="submit"
|
||||
disabled={saving}
|
||||
aria-disabled={saving}
|
||||
className="min-h-11"
|
||||
>
|
||||
{saving ? t('clients.form.saving') : submitLabel}
|
||||
</Button>
|
||||
</div>
|
||||
</form>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import ButtonLink from '@/components/ui/ButtonLink'
|
||||
import Card from '@/components/ui/Card'
|
||||
import { useT } from '@/i18n/useT'
|
||||
|
||||
/** The same screen for an unknown id and an id of another workspace (contract §11.1). */
|
||||
export default function ClientNotFound() {
|
||||
const t = useT()
|
||||
return (
|
||||
<Card as="section" className="space-y-3 p-5 sm:p-6">
|
||||
<h1 className="font-serif text-2xl text-[var(--color-ink)]">
|
||||
{t('clients.not_found.title')}
|
||||
</h1>
|
||||
<p className="text-sm text-[var(--color-ink-muted)]">
|
||||
{t('clients.not_found.body')}
|
||||
</p>
|
||||
<ButtonLink to="/app/clients" variant="secondary">
|
||||
{t('clients.not_found.back')}
|
||||
</ButtonLink>
|
||||
</Card>
|
||||
)
|
||||
}
|
||||
@@ -1,13 +1,16 @@
|
||||
import { useState } from 'react'
|
||||
import { Link, Outlet, useLocation } from 'react-router-dom'
|
||||
import { useState, type MouseEvent } from 'react'
|
||||
import { Link, Outlet, useLocation, useNavigate } from 'react-router-dom'
|
||||
import Brand from '@/components/brand/Brand'
|
||||
import LanguageSwitcher from '@/components/brand/LanguageSwitcher'
|
||||
import Button from '@/components/ui/Button'
|
||||
import Dialog from '@/components/ui/Dialog'
|
||||
import Icon from '@/components/ui/Icon'
|
||||
import MobileDrawer from './MobileDrawer'
|
||||
import SidebarNav, { NAV_ITEMS } from './SidebarNav'
|
||||
import SignOutButton from './SignOutButton'
|
||||
import UserBadge, { AvatarCircle } from './UserBadge'
|
||||
import { useAuthStore } from '@/state/authStore'
|
||||
import { useLeaveGuardStore } from '@/state/leaveGuardStore'
|
||||
import { useT } from '@/i18n/useT'
|
||||
|
||||
/** Title of the current /app page for the top bar, from the nav table. */
|
||||
@@ -18,20 +21,53 @@ function pageTitleKey(pathname: string): string {
|
||||
return match?.labelKey ?? 'shell.nav.dashboard'
|
||||
}
|
||||
|
||||
/** An in-app link a plain click would follow: same origin, same tab, no modifier key. */
|
||||
function followedLink(event: MouseEvent): HTMLAnchorElement | null {
|
||||
if (event.defaultPrevented || event.button !== 0) return null
|
||||
if (event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return null
|
||||
const anchor = (event.target as Element).closest('a[href]') as HTMLAnchorElement | null
|
||||
if (!anchor || anchor.target === '_blank' || anchor.origin !== window.location.origin) return null
|
||||
return anchor
|
||||
}
|
||||
|
||||
/**
|
||||
* Authenticated shell: fixed sidebar on large screens, light top bar with a
|
||||
* drawer button below. The page content scrolls on its own.
|
||||
* drawer button below. The page content scrolls on its own. While a page
|
||||
* holds unsaved changes (useLeaveGuard), following any link of the shell or
|
||||
* of the page asks first; staying is the default.
|
||||
*/
|
||||
export default function AppLayout() {
|
||||
const t = useT()
|
||||
const location = useLocation()
|
||||
const navigate = useNavigate()
|
||||
const [drawerOpen, setDrawerOpen] = useState(false)
|
||||
const guarded = useLeaveGuardStore((s) => s.armed)
|
||||
const arm = useLeaveGuardStore((s) => s.arm)
|
||||
const [leaveTo, setLeaveTo] = useState<string | null>(null)
|
||||
|
||||
function askBeforeLeaving(event: MouseEvent) {
|
||||
if (!guarded) return
|
||||
const anchor = followedLink(event)
|
||||
if (!anchor) return
|
||||
event.preventDefault()
|
||||
event.stopPropagation()
|
||||
setLeaveTo(anchor.pathname + anchor.search + anchor.hash)
|
||||
}
|
||||
|
||||
function leave() {
|
||||
const to = leaveTo
|
||||
if (!to) return
|
||||
arm(false)
|
||||
setLeaveTo(null)
|
||||
setDrawerOpen(false)
|
||||
navigate(to)
|
||||
}
|
||||
const profile = useAuthStore((s) => s.profile)
|
||||
const user = useAuthStore((s) => s.user)
|
||||
const displayName = profile?.display_name ?? user?.email ?? ''
|
||||
|
||||
return (
|
||||
<div className="flex min-h-dvh bg-[var(--color-bg)]">
|
||||
<div className="flex min-h-dvh bg-[var(--color-bg)]" onClickCapture={askBeforeLeaving}>
|
||||
<aside className="hidden lg:flex lg:w-64 lg:shrink-0 lg:flex-col border-r border-[var(--color-border)] bg-[var(--color-surface)]">
|
||||
<div className="px-5 py-5">
|
||||
<Link to="/app" aria-label={t('shell.nav.dashboard')} className="inline-block">
|
||||
@@ -85,6 +121,22 @@ export default function AppLayout() {
|
||||
</div>
|
||||
|
||||
<MobileDrawer open={drawerOpen} onClose={() => setDrawerOpen(false)} />
|
||||
<Dialog
|
||||
open={leaveTo !== null}
|
||||
onClose={() => setLeaveTo(null)}
|
||||
title={t('leave.title')}
|
||||
description={<p className="text-sm text-[var(--color-ink-muted)]">{t('leave.body')}</p>}
|
||||
size="sm"
|
||||
>
|
||||
<div className="mt-4 flex flex-wrap justify-end gap-2">
|
||||
<Button type="button" variant="ghost" onClick={() => setLeaveTo(null)}>
|
||||
{t('leave.stay')}
|
||||
</Button>
|
||||
<Button type="button" variant="danger" onClick={leave}>
|
||||
{t('leave.confirm')}
|
||||
</Button>
|
||||
</div>
|
||||
</Dialog>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -12,6 +12,7 @@ interface NavItem {
|
||||
|
||||
export const NAV_ITEMS: readonly NavItem[] = [
|
||||
{ to: '/app', icon: 'dashboard', labelKey: 'shell.nav.dashboard', end: true },
|
||||
{ to: '/app/clients', icon: 'users', labelKey: 'shell.nav.clients' },
|
||||
{ to: '/app/account', icon: 'user', labelKey: 'shell.nav.account' },
|
||||
{
|
||||
to: '/',
|
||||
@@ -31,7 +32,7 @@ function linkClass(active: boolean): string {
|
||||
].join(' ')
|
||||
}
|
||||
|
||||
/** The three destinations of the shell; `onNavigate` closes the mobile drawer. */
|
||||
/** The destinations of the shell, in the validated mockup's order; `onNavigate` closes the mobile drawer. */
|
||||
export default function SidebarNav({ onNavigate }: { onNavigate?: () => void }) {
|
||||
const t = useT()
|
||||
return (
|
||||
|
||||
@@ -5,6 +5,7 @@ export type IconName =
|
||||
| 'close'
|
||||
| 'dashboard'
|
||||
| 'user'
|
||||
| 'users'
|
||||
| 'tool'
|
||||
| 'logout'
|
||||
| 'eye'
|
||||
@@ -20,6 +21,7 @@ const PATHS: Record<IconName, string> = {
|
||||
close: 'M6 6l12 12M18 6L6 18',
|
||||
dashboard: 'M4 4h7v7H4zM13 4h7v4h-7zM13 11h7v9h-7zM4 14h7v6H4z',
|
||||
user: 'M12 12a4 4 0 1 0 0-8 4 4 0 0 0 0 8zM4 21a8 8 0 0 1 16 0',
|
||||
users: 'M9 11a4 4 0 1 0 0-8 4 4 0 0 0 0 8zM2 21a7 7 0 0 1 14 0M16 3.5a4 4 0 0 1 0 7M22 21a7 7 0 0 0-4.5-6.5',
|
||||
tool: 'M14 3l7 7-4 4-7-7zM10 10l-6 6 4 4 6-6M3 21l3-3',
|
||||
logout: 'M10 4H5v16h5M15 8l5 4-5 4M20 12H9',
|
||||
eye: 'M2 12s4-7 10-7 10 7 10 7-4 7-10 7S2 12 2 12zM12 15a3 3 0 1 0 0-6 3 3 0 0 0 0 6z',
|
||||
|
||||
Reference in new issue
Block a user