@@ -0,0 +1,385 @@
|
||||
import { Head, Link, useForm } from '@inertiajs/react';
|
||||
import { useEffect, useRef } from 'react';
|
||||
import {
|
||||
FormErrors,
|
||||
PageTitle,
|
||||
Panel,
|
||||
controlClass,
|
||||
} from '@/components/procurement';
|
||||
import { Button } from '@/components/ui/button';
|
||||
|
||||
type Level = {
|
||||
role: string;
|
||||
expire_minutes: number;
|
||||
reminder_minutes: number | null;
|
||||
escalation_minutes: number | null;
|
||||
profile_ids: number[];
|
||||
};
|
||||
type Policy = {
|
||||
id: number;
|
||||
version: number;
|
||||
state: string;
|
||||
reason: string;
|
||||
data: { enabled: boolean; levels: Level[] };
|
||||
};
|
||||
type Props = {
|
||||
current: Policy | null;
|
||||
profiles: { id: number; name: string }[];
|
||||
roles: Record<string, string>;
|
||||
actorId: number;
|
||||
tenantId: number;
|
||||
history: {
|
||||
data: Policy[];
|
||||
prev_page_url: string | null;
|
||||
next_page_url: string | null;
|
||||
};
|
||||
};
|
||||
type Draft = {
|
||||
role: string;
|
||||
expire_minutes: string;
|
||||
reminder_minutes: string;
|
||||
escalation_minutes: string;
|
||||
profile_ids: number[];
|
||||
};
|
||||
const draft = (level: Level): Draft => ({
|
||||
...level,
|
||||
expire_minutes: String(level.expire_minutes),
|
||||
reminder_minutes:
|
||||
level.reminder_minutes === null ? '' : String(level.reminder_minutes),
|
||||
escalation_minutes:
|
||||
level.escalation_minutes === null
|
||||
? ''
|
||||
: String(level.escalation_minutes),
|
||||
profile_ids: [...level.profile_ids],
|
||||
});
|
||||
export default function ApprovalTimeouts(props: Props) {
|
||||
const url = '/administracao/prazos-aprovacao';
|
||||
const form = useForm(
|
||||
`approval-timeouts:${props.tenantId}:${props.actorId}`,
|
||||
{
|
||||
operation_key: '',
|
||||
version: props.current?.version ?? 0,
|
||||
enabled: props.current?.data.enabled ?? false,
|
||||
levels: props.current?.data.levels.map(draft) ?? ([] as Draft[]),
|
||||
reason: '',
|
||||
},
|
||||
);
|
||||
const current = form.data.version === (props.current?.version ?? 0);
|
||||
const ref = useRef<HTMLFormElement>(null);
|
||||
useEffect(() => {
|
||||
const node = ref.current;
|
||||
window.dispatchEvent(
|
||||
new CustomEvent('sgc:context-dirty', {
|
||||
detail: { form: node, dirty: form.isDirty },
|
||||
}),
|
||||
);
|
||||
return () => {
|
||||
window.dispatchEvent(
|
||||
new CustomEvent('sgc:context-dirty', {
|
||||
detail: { form: node, dirty: false },
|
||||
}),
|
||||
);
|
||||
};
|
||||
}, [form.isDirty]);
|
||||
const edit = () =>
|
||||
form.setData({
|
||||
operation_key: '',
|
||||
version: props.current?.version ?? 0,
|
||||
enabled: props.current?.data.enabled ?? false,
|
||||
levels: props.current?.data.levels.map(draft) ?? [],
|
||||
reason: '',
|
||||
});
|
||||
const change = (index: number, patch: Partial<Draft>) =>
|
||||
form.setData((data) => ({
|
||||
...data,
|
||||
levels: data.levels.map((level, i) =>
|
||||
i === index
|
||||
? {
|
||||
...level,
|
||||
...patch,
|
||||
...(patch.escalation_minutes === ''
|
||||
? { profile_ids: [] }
|
||||
: {}),
|
||||
}
|
||||
: level,
|
||||
),
|
||||
operation_key: '',
|
||||
}));
|
||||
return (
|
||||
<div className="mx-auto grid w-full max-w-6xl min-w-0 gap-6 p-5 lg:p-8">
|
||||
<Head title="Prazos das aprovações" />
|
||||
<PageTitle
|
||||
eyebrow="Administração"
|
||||
title="Prazos das aprovações"
|
||||
description="Configure os prazos que a empresa pretende aplicar nas novas submissões. Não são atribuídas permissões aos destinatários do escalamento."
|
||||
/>
|
||||
<Link className="underline" href="/administracao/parametrizacao">
|
||||
Voltar à parametrização
|
||||
</Link>
|
||||
<Panel
|
||||
title={`Política actual · versão ${props.current?.version ?? 0}`}
|
||||
description="Os valores são minutos corridos desde a activação da etapa, sem calendário de dias úteis implícito."
|
||||
>
|
||||
<p>
|
||||
{props.current?.data.enabled
|
||||
? 'Aplicável a novas submissões'
|
||||
: 'Sem prazos activos para novas submissões'}
|
||||
</p>
|
||||
<form
|
||||
ref={ref}
|
||||
className="mt-5 space-y-5"
|
||||
onSubmit={(event) => {
|
||||
event.preventDefault();
|
||||
const operation =
|
||||
form.data.operation_key || crypto.randomUUID();
|
||||
form.setData('operation_key', operation);
|
||||
form.transform((data) => ({
|
||||
...data,
|
||||
operation_key: operation,
|
||||
levels: data.levels.map((level) => ({
|
||||
...level,
|
||||
reminder_minutes:
|
||||
level.reminder_minutes || null,
|
||||
escalation_minutes:
|
||||
level.escalation_minutes || null,
|
||||
})),
|
||||
}));
|
||||
form.post(url, {
|
||||
preserveScroll: true,
|
||||
onSuccess: () => {
|
||||
form.reset();
|
||||
form.clearErrors();
|
||||
},
|
||||
});
|
||||
}}
|
||||
>
|
||||
<FormErrors errors={form.errors} />
|
||||
{!current && (
|
||||
<p>
|
||||
A política mudou. Reveja a versão actual antes de
|
||||
guardar outra alteração.
|
||||
</p>
|
||||
)}
|
||||
<Button type="button" variant="outline" onClick={edit}>
|
||||
Preparar a política actual
|
||||
</Button>
|
||||
<label className="flex items-center gap-2">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={form.data.enabled}
|
||||
onChange={(e) =>
|
||||
form.setData('enabled', e.target.checked)
|
||||
}
|
||||
/>
|
||||
Aplicar a novas submissões
|
||||
</label>
|
||||
{form.data.levels.map((level, index) => (
|
||||
<fieldset
|
||||
className="min-w-0 space-y-4 rounded-lg border p-4"
|
||||
key={index}
|
||||
>
|
||||
<legend>Nível {index + 1}</legend>
|
||||
<label className="grid gap-2">
|
||||
Etapa
|
||||
<select
|
||||
className={controlClass}
|
||||
value={level.role}
|
||||
onChange={(e) =>
|
||||
change(index, { role: e.target.value })
|
||||
}
|
||||
>
|
||||
{Object.entries(props.roles).map(
|
||||
([key, label]) => (
|
||||
<option key={key} value={key}>
|
||||
{label}
|
||||
</option>
|
||||
),
|
||||
)}
|
||||
</select>
|
||||
</label>
|
||||
<div className="grid gap-4 md:grid-cols-3">
|
||||
{(
|
||||
[
|
||||
'reminder_minutes',
|
||||
'escalation_minutes',
|
||||
'expire_minutes',
|
||||
] as const
|
||||
).map((field) => (
|
||||
<label key={field} className="grid gap-2">
|
||||
{field === 'expire_minutes'
|
||||
? 'Expiração · minutos'
|
||||
: field === 'reminder_minutes'
|
||||
? 'Lembrete · minutos, se aplicável'
|
||||
: 'Escalamento · minutos, se aplicável'}
|
||||
<input
|
||||
type="number"
|
||||
min={1}
|
||||
max={5256000}
|
||||
step={1}
|
||||
required={
|
||||
field === 'expire_minutes'
|
||||
}
|
||||
className={controlClass}
|
||||
value={level[field]}
|
||||
onChange={(e) =>
|
||||
change(index, {
|
||||
[field]: e.target.value,
|
||||
})
|
||||
}
|
||||
/>
|
||||
</label>
|
||||
))}
|
||||
</div>
|
||||
{level.escalation_minutes !== '' && (
|
||||
<fieldset className="space-y-2">
|
||||
<legend>
|
||||
Perfis destinatários do escalamento
|
||||
</legend>
|
||||
{props.profiles.length === 0 && (
|
||||
<p>
|
||||
Configure os perfis da empresa antes
|
||||
de seleccionar destinatários.
|
||||
</p>
|
||||
)}
|
||||
{props.profiles.map((profile) => (
|
||||
<label
|
||||
key={profile.id}
|
||||
className="flex items-center gap-2"
|
||||
>
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={level.profile_ids.includes(
|
||||
profile.id,
|
||||
)}
|
||||
onChange={(e) =>
|
||||
change(index, {
|
||||
profile_ids: e.target
|
||||
.checked
|
||||
? [
|
||||
...level.profile_ids,
|
||||
profile.id,
|
||||
]
|
||||
: level.profile_ids.filter(
|
||||
(id) =>
|
||||
id !==
|
||||
profile.id,
|
||||
),
|
||||
})
|
||||
}
|
||||
/>
|
||||
{profile.name}
|
||||
</label>
|
||||
))}
|
||||
</fieldset>
|
||||
)}
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
onClick={() =>
|
||||
form.setData((data) => ({
|
||||
...data,
|
||||
levels: data.levels.filter(
|
||||
(_, i) => i !== index,
|
||||
),
|
||||
operation_key: '',
|
||||
}))
|
||||
}
|
||||
>
|
||||
Retirar este nível da preparação
|
||||
</Button>
|
||||
</fieldset>
|
||||
))}
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
disabled={form.data.levels.length >= 8}
|
||||
onClick={() => {
|
||||
const role = Object.keys(props.roles).find(
|
||||
(key) =>
|
||||
!form.data.levels.some(
|
||||
(level) => level.role === key,
|
||||
),
|
||||
);
|
||||
if (role)
|
||||
form.setData((data) => ({
|
||||
...data,
|
||||
levels: [
|
||||
...data.levels,
|
||||
{
|
||||
role,
|
||||
expire_minutes: '',
|
||||
reminder_minutes: '',
|
||||
escalation_minutes: '',
|
||||
profile_ids: [],
|
||||
},
|
||||
],
|
||||
operation_key: '',
|
||||
}));
|
||||
}}
|
||||
>
|
||||
Acrescentar etapa
|
||||
</Button>
|
||||
<label className="grid gap-2">
|
||||
Motivo desta versão
|
||||
<textarea
|
||||
required
|
||||
minLength={10}
|
||||
maxLength={4000}
|
||||
className={controlClass}
|
||||
value={form.data.reason}
|
||||
onChange={(e) =>
|
||||
form.setData('reason', e.target.value)
|
||||
}
|
||||
/>
|
||||
</label>
|
||||
<p className="text-sm text-muted-foreground">
|
||||
Na expiração, o pedido regressa ao requisitante para
|
||||
nova submissão. Os ciclos anteriores ficam preservados;
|
||||
uma alteração desta política não muda os prazos já
|
||||
submetidos.
|
||||
</p>
|
||||
<Button disabled={form.processing || !current}>
|
||||
Guardar política
|
||||
</Button>
|
||||
</form>
|
||||
</Panel>
|
||||
<Panel title="Histórico da política">
|
||||
{props.history.data.map((policy) => (
|
||||
<article
|
||||
key={policy.id}
|
||||
className="space-y-2 border-b py-3"
|
||||
>
|
||||
<h3 className="font-semibold">
|
||||
Versão {policy.version} ·{' '}
|
||||
{policy.data.enabled
|
||||
? 'Activa para novas submissões'
|
||||
: 'Inactiva para novas submissões'}
|
||||
</h3>
|
||||
<p>{policy.reason}</p>
|
||||
{policy.data.levels.map((level) => (
|
||||
<p key={level.role}>
|
||||
{props.roles[level.role]} · expiração em{' '}
|
||||
{level.expire_minutes} minutos corridos
|
||||
</p>
|
||||
))}
|
||||
</article>
|
||||
))}
|
||||
<nav
|
||||
className="mt-3 flex gap-4"
|
||||
aria-label="Histórico da política"
|
||||
>
|
||||
{props.history.prev_page_url && (
|
||||
<Link preserveState href={props.history.prev_page_url}>
|
||||
Anterior
|
||||
</Link>
|
||||
)}
|
||||
{props.history.next_page_url && (
|
||||
<Link preserveState href={props.history.next_page_url}>
|
||||
Seguinte
|
||||
</Link>
|
||||
)}
|
||||
</nav>
|
||||
</Panel>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user