Files
Investidor-V2/resources/js/pages/desk/service-editor.tsx
T
2026-11-02 20:31:15 +02:00

599 lines
27 KiB
TypeScript

import { Link, useForm } from '@inertiajs/react';
import { useState } from 'react';
import DeskServiceHistory from '@/components/desk-service-history';
import { Frame, card, button, input } from '@/pages/investment/shared';
export type Field = {
label: string;
type: string;
required: boolean;
min?: number;
options?: Record<string, string>;
when?: { field: string; equals: string };
};
export type Definition = {
title: string;
theme: string;
profile: string;
stage: string;
entity: string;
result: string;
fields: Record<string, Field>;
};
type Service = {
code: string;
version: number;
definition: Definition;
fixedTypes?: Record<string, string>;
editable?: boolean;
};
const types: Record<string, string> = {
text: 'Texto curto',
textarea: 'Texto longo',
number: 'Número',
date: 'Data',
select: 'Lista de opções',
territory: 'Localização do cadastro',
email: 'Email',
tel: 'Telefone',
url: 'Ligação web',
};
const labels = {
title: 'Nome do serviço',
theme: 'Tema',
profile: 'Destinatários',
entity: 'Entidade indicada',
result: 'Resultado esperado',
};
function describeField(field: Field | undefined): string {
if (!field) return 'Ausente';
return [
field.label,
types[field.type] ?? field.type,
field.required ? 'Obrigatório' : 'Opcional',
...(field.min !== undefined ? [`Mínimo: ${field.min}`] : []),
...(field.options
? [
`Opções: ${Object.entries(field.options)
.map(([code, label]) => `${label} (${code})`)
.join('; ')}`,
]
: []),
field.when
? `Aplica-se quando ${field.when.field} = ${field.when.equals}`
: 'Sempre aplicável',
].join(' · ');
}
export default function ServiceEditor({ services }: { services: Service[] }) {
const [code, setCode] = useState(services[0]?.code ?? '');
const service = services.find((row) => row.code === code);
return (
<Frame title="Formulários do Balcão — versões">
<Link href="/management/balcao-install" className="underline">
Voltar à administração do Balcão
</Link>
<p>
Prepare a definição, confira as alterações e publique uma nova
versão. Pedidos existentes conservam os campos, respostas e
regras da versão em que foram criados.
</p>
<label className="block">
Serviço
<select
className={input}
value={code}
onChange={(event) => setCode(event.target.value)}
>
{services.map((row) => (
<option key={row.code} value={row.code}>
{row.definition.title} — versão {row.version}
</option>
))}
</select>
</label>
{service && service.editable === false && (
<p>
Este serviço conserva definições históricas fora da
configuração actual. Neste editor estão disponíveis para
consulta.
</p>
)}
{service && service.editable !== false ? (
<Editor
key={`${service.code}:${service.version}`}
service={service}
/>
) : !service ? (
<p>Não existem serviços disponíveis para edição.</p>
) : null}
{service && (
<DeskServiceHistory
key={`history:${service.code}:${service.version}`}
code={service.code}
formatField={describeField}
/>
)}
</Frame>
);
}
function Editor({ service }: { service: Service }) {
const form = useForm(
`desk-service-editor:${service.code}:${service.version}`,
{
base_version: service.version,
definition: structuredClone(service.definition),
reason: '',
confirm: false,
},
);
const [newKey, setNewKey] = useState('');
const [localError, setLocalError] = useState('');
const fields = form.data.definition.fields;
const setFields = (value: Record<string, Field>) =>
form.setData((data) => ({
...data,
confirm: false,
definition: { ...data.definition, fields: value },
}));
const update = (key: string, patch: Partial<Field>) =>
setFields({ ...fields, [key]: { ...fields[key], ...patch } });
const changed = [
...new Set([
...Object.keys(service.definition.fields),
...Object.keys(fields),
]),
].filter(
(key) =>
JSON.stringify(service.definition.fields[key]) !==
JSON.stringify(fields[key]),
);
const metaChanges = (Object.keys(labels) as (keyof typeof labels)[]).filter(
(key) => service.definition[key] !== form.data.definition[key],
);
return (
<form
className="space-y-5"
onSubmit={(event) => {
event.preventDefault();
if (form.processing) return;
form.put(
`/management/balcao/servicos/${encodeURIComponent(service.code)}`,
{ preserveScroll: true },
);
}}
>
<fieldset
disabled={form.processing}
className={card + ' space-y-4'}
>
<legend>Identificação do formulário</legend>
{(Object.keys(labels) as (keyof typeof labels)[]).map((key) => (
<label key={key} className="block">
{labels[key]}
<input
required
maxLength={
key === 'result'
? 2000
: key === 'entity'
? 300
: key === 'profile' || key === 'title'
? 200
: 150
}
className={input}
value={form.data.definition[key]}
onChange={(event) =>
form.setData((data) => ({
...data,
confirm: false,
definition: {
...data.definition,
[key]: event.target.value,
},
}))
}
/>
</label>
))}
<p>
Etapa do percurso existente: {service.definition.stage}.
Este editor não altera a sequência de tramitação.
</p>
</fieldset>
<fieldset disabled={form.processing} className="space-y-4">
<legend className="text-xl font-bold">
Campos e condições de preenchimento
</legend>
{Object.entries(fields).map(([key, field]) => (
<section
key={key}
className={card + ' space-y-3'}
aria-label={`Campo ${key}`}
>
<h3 className="font-bold">
{field.label}{' '}
<span className="font-normal">({key})</span>
</h3>
<label className="block">
Pergunta ou rótulo
<input
required
maxLength={300}
className={input}
value={field.label}
onChange={(event) =>
update(key, { label: event.target.value })
}
/>
</label>
<label className="block">
Tipo de resposta
<select
disabled={Boolean(service.fixedTypes?.[key])}
className={input}
value={field.type}
onChange={(event) => {
const next = {
...field,
type: event.target.value,
};
if (next.type !== 'select')
delete next.options;
else
next.options ??= {
opcao_1: 'Primeira opção',
};
if (next.type !== 'number') delete next.min;
setFields({ ...fields, [key]: next });
}}
>
{Object.entries(types).map(([value, label]) => (
<option key={value} value={value}>
{label}
</option>
))}
</select>
{service.fixedTypes?.[key] && (
<span className="text-sm">
O tipo deste campo é usado pelo motor de
validação existente. Para outra resposta,
acrescente um novo campo.
</span>
)}
</label>
<label className="flex gap-2">
<input
type="checkbox"
checked={field.required}
onChange={(event) =>
update(key, {
required: event.target.checked,
})
}
/>
Obrigatório ao submeter, quando aplicável
</label>
{field.type === 'number' && (
<label className="block">
Valor mínimo
<input
className={input}
type="number"
min="0"
step="0.01"
value={field.min ?? 0}
onChange={(event) =>
update(key, {
min: Number(event.target.value),
})
}
/>
</label>
)}
{field.type === 'select' && (
<fieldset className="space-y-2">
<legend>Opções de resposta</legend>
{Object.entries(field.options ?? {}).map(
([value, label]) => (
<div key={value} className="flex gap-2">
<label className="flex-1">
Texto da opção ({value})
<input
required
maxLength={300}
className={input}
value={label}
onChange={(event) =>
update(key, {
options: {
...field.options,
[value]:
event.target
.value,
},
})
}
/>
</label>
<button
type="button"
className="underline"
onClick={() => {
const next = {
...field.options,
};
delete next[value];
update(key, {
options: next,
});
}}
>
Remover opção
</button>
</div>
),
)}
<button
type="button"
className="underline"
onClick={() => {
let index = 1;
while (
`opcao_${index}` in
(field.options ?? {})
)
index++;
update(key, {
options: {
...field.options,
[`opcao_${index}`]:
'Nova opção',
},
});
}}
>
Acrescentar opção
</button>
</fieldset>
)}
<label className="block">
Apresentar este campo
<select
className={input}
value={field.when?.field ?? ''}
onChange={(event) => {
const next = { ...field };
if (event.target.value)
next.when = {
field: event.target.value,
equals: '',
};
else delete next.when;
setFields({ ...fields, [key]: next });
}}
>
<option value="">Sempre</option>
{Object.entries(fields)
.filter(
([other, source]) =>
other !== key &&
source.type !== 'number',
)
.map(([other, value]) => (
<option key={other} value={other}>
Quando a resposta a «{value.label}»
for…
</option>
))}
</select>
</label>
{field.when && (
<label className="block">
Resposta que torna o campo aplicável
{fields[field.when.field]?.type === 'select' ? (
<select
required
className={input}
value={field.when.equals}
onChange={(event) =>
update(key, {
when: {
field: field.when!.field,
equals: event.target.value,
},
})
}
>
<option value="">
Seleccionar resposta
</option>
{Object.entries(
fields[field.when.field].options ??
{},
).map(([value, label]) => (
<option key={value} value={value}>
{label}
</option>
))}
</select>
) : (
<input
required
className={input}
value={field.when.equals}
onChange={(event) =>
update(key, {
when: {
field: field.when!.field,
equals: event.target.value,
},
})
}
/>
)}
</label>
)}
<button
type="button"
className="underline"
onClick={() => {
const next = { ...fields };
delete next[key];
setFields(next);
}}
>
Retirar campo da nova versão
</button>
</section>
))}
<div className={card}>
<label className="block">
Identificador do novo campo
<input
className={input}
pattern="[a-z][a-z0-9_]*"
maxLength={64}
value={newKey}
placeholder="ex.: observacoes_adicionais"
onChange={(event) => setNewKey(event.target.value)}
/>
</label>
<button
type="button"
className="underline"
onClick={() => {
if (
!/^[a-z][a-z0-9_]{0,63}$/.test(newKey) ||
newKey in fields
) {
setLocalError(
'Indique um identificador novo, com letras minúsculas, números ou sublinhado.',
);
return;
}
setFields({
...fields,
[newKey]: {
label: 'Nova pergunta',
type: 'text',
required: false,
},
});
setNewKey('');
setLocalError('');
}}
>
Acrescentar campo
</button>
{localError && <p role="alert">{localError}</p>}
</div>
</fieldset>
<section
className={card + ' space-y-3'}
aria-label="Conferir efeito da publicação"
>
<h2 className="text-xl font-bold">
Conferir nova versão {service.version + 1}
</h2>
<p>
Alterações de identificação:{' '}
{metaChanges.map((key) => labels[key]).join(', ') ||
'nenhuma'}
. Campos acrescentados, alterados ou retirados:{' '}
{changed.join(', ') || 'nenhum'}.
</p>
{(changed.length > 0 || metaChanges.length > 0) && (
<div className="overflow-x-auto">
<table className="w-full text-left text-sm">
<caption>
Comparação com a versão {service.version}
</caption>
<thead>
<tr>
<th className="p-2">Elemento</th>
<th className="p-2">Versão anterior</th>
<th className="p-2">Nova versão</th>
</tr>
</thead>
<tbody>
{metaChanges.map((key) => (
<tr key={key}>
<th className="p-2">{labels[key]}</th>
<td className="p-2">
{service.definition[key]}
</td>
<td className="p-2">
{form.data.definition[key]}
</td>
</tr>
))}
{changed.map((key) => (
<tr key={key}>
<th className="p-2">{key}</th>
<td className="p-2">
{describeField(
service.definition.fields[key],
)}
</td>
<td className="p-2">
{describeField(fields[key])}
</td>
</tr>
))}
</tbody>
</table>
</div>
)}
<p>
Aplica-se aos pedidos criados com esta nova versão. Pedidos
e submissões anteriores continuam vinculados à sua definição
original; as respectivas respostas não são reescritas.
</p>
<label className="block">
Motivo da alteração
<textarea
required
minLength={20}
maxLength={2000}
className={input}
value={form.data.reason}
onChange={(event) =>
form.setData('reason', event.target.value)
}
/>
</label>
<label className="flex gap-2">
<input
type="checkbox"
checked={form.data.confirm}
onChange={(event) =>
form.setData('confirm', event.target.checked)
}
/>
Conferi as alterações e o efeito sobre pedidos futuros.
</label>
{Object.entries(form.errors).map(([key, value]) => (
<p key={key} role="alert">
{value}
</p>
))}
<button
className={button}
disabled={
form.processing ||
!form.data.confirm ||
(service.version > 0 &&
changed.length === 0 &&
metaChanges.length === 0)
}
>
{form.processing ? 'A publicar…' : 'Publicar nova versão'}
</button>
</section>
</form>
);
}