138 lines
5.2 KiB
TypeScript
138 lines
5.2 KiB
TypeScript
import { useForm } from '@inertiajs/react';
|
|
import { Management } from '../management/shared';
|
|
import { Field, input, button, card, groupLabels } from './shared';
|
|
import type { Option } from './shared';
|
|
export default function References({ options }: { options: Option[] }) {
|
|
const groups = [...new Set(options.map((o) => o.group))];
|
|
return (
|
|
<Management title="Opções dos formulários e filtros">
|
|
<p>
|
|
Os códigos são estáveis; pode corrigir etiquetas ou desactivar
|
|
opções sem eliminar referências históricas. Cada grupo conserva
|
|
Outro — especificar.
|
|
</p>
|
|
<OptionForm groups={groups} />
|
|
<div className="space-y-4">
|
|
{groups.map((group) => (
|
|
<details key={group} className={card}>
|
|
<summary className="cursor-pointer font-bold">
|
|
{groupLabels[group] ?? group}
|
|
</summary>
|
|
<div className="mt-4 space-y-5">
|
|
{options
|
|
.filter((o) => o.group === group)
|
|
.map((o) => (
|
|
<OptionForm
|
|
key={o.id}
|
|
option={o}
|
|
groups={groups}
|
|
/>
|
|
))}
|
|
</div>
|
|
</details>
|
|
))}
|
|
</div>
|
|
</Management>
|
|
);
|
|
}
|
|
function OptionForm({ option, groups }: { option?: Option; groups: string[] }) {
|
|
const form = useForm({
|
|
group: option?.group ?? groups[0],
|
|
code: option?.code ?? '',
|
|
label: option?.label ?? '',
|
|
active: option?.active ?? true,
|
|
requires_description: option?.requires_description ?? false,
|
|
sort_order: option?.sort_order ?? 0,
|
|
});
|
|
return (
|
|
<form
|
|
className="space-y-3 rounded border p-4"
|
|
onSubmit={(e) => {
|
|
e.preventDefault();
|
|
form.post(
|
|
'/management/investment/references' +
|
|
(option ? '/' + option.id : ''),
|
|
{
|
|
preserveScroll: true,
|
|
},
|
|
);
|
|
}}
|
|
>
|
|
<h2 className="font-semibold">
|
|
{option ? 'Editar opção' : 'Criar opção'}
|
|
</h2>
|
|
<div className="grid gap-3 md:grid-cols-3">
|
|
<Field label="Grupo">
|
|
<select
|
|
disabled={!!option}
|
|
className={input}
|
|
value={form.data.group}
|
|
onChange={(e) => form.setData('group', e.target.value)}
|
|
>
|
|
{groups.map((g) => (
|
|
<option key={g} value={g}>
|
|
{groupLabels[g] ?? g}
|
|
</option>
|
|
))}
|
|
</select>
|
|
</Field>
|
|
<Field label="Código">
|
|
<input
|
|
className={input}
|
|
required
|
|
readOnly={!!option}
|
|
value={form.data.code}
|
|
onChange={(e) => form.setData('code', e.target.value)}
|
|
/>
|
|
</Field>
|
|
<Field label="Etiqueta">
|
|
<input
|
|
className={input}
|
|
required
|
|
value={form.data.label}
|
|
onChange={(e) => form.setData('label', e.target.value)}
|
|
/>
|
|
</Field>
|
|
</div>
|
|
<label className="mr-4 inline-flex gap-2">
|
|
<input
|
|
type="checkbox"
|
|
checked={form.data.active}
|
|
onChange={(e) => form.setData('active', e.target.checked)}
|
|
/>
|
|
Activa
|
|
</label>
|
|
<label className="inline-flex gap-2">
|
|
<input
|
|
type="checkbox"
|
|
checked={form.data.requires_description}
|
|
onChange={(e) =>
|
|
form.setData('requires_description', e.target.checked)
|
|
}
|
|
/>
|
|
Exige especificação
|
|
</label>
|
|
<Field label="Ordem">
|
|
<input
|
|
type="number"
|
|
min="0"
|
|
max="10000"
|
|
className={input}
|
|
value={form.data.sort_order}
|
|
onChange={(e) =>
|
|
form.setData('sort_order', Number(e.target.value))
|
|
}
|
|
/>
|
|
</Field>
|
|
{Object.entries(form.errors).map(([key, value]) => (
|
|
<p role="alert" key={key}>
|
|
{value}
|
|
</p>
|
|
))}
|
|
<button disabled={form.processing} className={button}>
|
|
Guardar opção
|
|
</button>
|
|
</form>
|
|
);
|
|
}
|