217 lines
9.1 KiB
TypeScript
217 lines
9.1 KiB
TypeScript
import { Head, Link, useForm } from '@inertiajs/react';
|
|
import { usePreparationDirty } from '@/hooks/use-preparation-dirty';
|
|
import {
|
|
PageTitle,
|
|
Panel,
|
|
FormErrors,
|
|
controlClass,
|
|
} from '@/components/procurement';
|
|
import { Button } from '@/components/ui/button';
|
|
|
|
type Policy = {
|
|
id: number;
|
|
version: number;
|
|
state: string;
|
|
reason: string;
|
|
data: { profile_ids: number[] };
|
|
};
|
|
export default function UrgentReviewPolicy(props: {
|
|
current: Policy | null;
|
|
profiles: { id: number; name: string }[];
|
|
actorId: number;
|
|
tenantId: number;
|
|
history: {
|
|
data: Policy[];
|
|
prev_page_url: string | null;
|
|
next_page_url: string | null;
|
|
};
|
|
}) {
|
|
const form = useForm(
|
|
`urgent-review-policy:${props.tenantId}:${props.actorId}`,
|
|
{
|
|
operation_key: '',
|
|
version: props.current?.version ?? 0,
|
|
enabled: props.current?.state === 'active',
|
|
profile_ids: props.current?.data.profile_ids ?? [],
|
|
reason: '',
|
|
},
|
|
);
|
|
const ref = usePreparationDirty(form.isDirty);
|
|
const current = form.data.version === (props.current?.version ?? 0);
|
|
return (
|
|
<main className="mx-auto flex w-full max-w-6xl flex-col gap-6 p-5 lg:p-8">
|
|
<Head title="Perfis de revisão das urgências" />
|
|
<PageTitle
|
|
eyebrow="Administração"
|
|
title="Revisão posterior das urgências"
|
|
description="Designe os perfis da direcção de Compras que devem efectuar a revisão. A designação utiliza funções existentes e não concede permissões."
|
|
/>
|
|
<Link className="underline" href="/requisicoes/urgencias/revisoes">
|
|
Consultar as urgências
|
|
</Link>
|
|
<Panel
|
|
title={`Designação actual · versão ${props.current?.version ?? 0}`}
|
|
description="A revisão fica disponível após a decisão final da requisição, incluindo rejeição ou expiração, para cada ciclo concluído."
|
|
>
|
|
<form
|
|
ref={ref}
|
|
className="space-y-4"
|
|
onSubmit={(event) => {
|
|
event.preventDefault();
|
|
const data = {
|
|
...form.data,
|
|
operation_key:
|
|
form.data.operation_key || crypto.randomUUID(),
|
|
};
|
|
form.setData(data);
|
|
form.transform(() => data);
|
|
form.post('/administracao/revisao-urgencias', {
|
|
preserveScroll: true,
|
|
onSuccess: () => {
|
|
const next = {
|
|
...data,
|
|
operation_key: '',
|
|
version: data.version + 1,
|
|
reason: '',
|
|
};
|
|
form.setData(next);
|
|
form.setDefaults(next);
|
|
},
|
|
});
|
|
}}
|
|
>
|
|
<FormErrors errors={form.errors} />
|
|
{!current && (
|
|
<p role="alert">
|
|
A designação mudou. Confira a versão actual.
|
|
</p>
|
|
)}
|
|
<Button
|
|
type="button"
|
|
variant="outline"
|
|
onClick={() => {
|
|
if (
|
|
form.isDirty &&
|
|
!window.confirm(
|
|
'Descartar esta preparação e usar a designação actual?',
|
|
)
|
|
)
|
|
return;
|
|
form.setData({
|
|
operation_key: '',
|
|
version: props.current?.version ?? 0,
|
|
enabled: props.current?.state === 'active',
|
|
profile_ids:
|
|
props.current?.data.profile_ids ?? [],
|
|
reason: '',
|
|
});
|
|
}}
|
|
>
|
|
Preparar designação actual
|
|
</Button>
|
|
<label className="flex gap-2">
|
|
<input
|
|
type="checkbox"
|
|
checked={form.data.enabled}
|
|
onChange={(event) =>
|
|
form.setData('enabled', event.target.checked)
|
|
}
|
|
/>
|
|
Aplicar esta designação
|
|
</label>
|
|
<fieldset className="space-y-3">
|
|
<legend className="mb-3 font-semibold">
|
|
Perfis responsáveis
|
|
</legend>
|
|
{props.profiles.length === 0 && (
|
|
<p>
|
|
Configure os perfis da empresa antes de efectuar
|
|
a designação.
|
|
</p>
|
|
)}
|
|
{props.profiles.map((profile) => (
|
|
<label key={profile.id} className="flex gap-2">
|
|
<input
|
|
type="checkbox"
|
|
checked={form.data.profile_ids.includes(
|
|
profile.id,
|
|
)}
|
|
onChange={(event) =>
|
|
form.setData(
|
|
'profile_ids',
|
|
event.target.checked
|
|
? [
|
|
...form.data.profile_ids,
|
|
profile.id,
|
|
]
|
|
: form.data.profile_ids.filter(
|
|
(id) => id !== profile.id,
|
|
),
|
|
)
|
|
}
|
|
/>
|
|
{profile.name}
|
|
</label>
|
|
))}
|
|
</fieldset>
|
|
<p className="text-sm">
|
|
O interveniente precisa também da função de Compras, de
|
|
atribuição válida e do acesso ao pedido. Sem esses
|
|
requisitos, o perfil seleccionado não pode registar a
|
|
revisão.
|
|
</p>
|
|
<label className="block">
|
|
Motivo desta versão
|
|
<textarea
|
|
className={controlClass}
|
|
required
|
|
minLength={10}
|
|
maxLength={4000}
|
|
value={form.data.reason}
|
|
onChange={(event) =>
|
|
form.setData('reason', event.target.value)
|
|
}
|
|
/>
|
|
</label>
|
|
<Button disabled={form.processing || !current}>
|
|
Guardar designação
|
|
</Button>
|
|
</form>
|
|
</Panel>
|
|
<Panel title="Versões preservadas">
|
|
<ul className="space-y-3">
|
|
{props.history.data.map((row) => (
|
|
<li key={row.id}>
|
|
Versão {row.version} ·{' '}
|
|
{row.state === 'active' ? 'Activa' : 'Inactiva'} ·{' '}
|
|
{row.reason}
|
|
</li>
|
|
))}
|
|
</ul>
|
|
<nav className="mt-4 flex gap-4">
|
|
{props.history.prev_page_url && (
|
|
<Link
|
|
preserveState
|
|
preserveScroll
|
|
className="underline"
|
|
href={props.history.prev_page_url}
|
|
>
|
|
Anterior
|
|
</Link>
|
|
)}
|
|
{props.history.next_page_url && (
|
|
<Link
|
|
preserveState
|
|
preserveScroll
|
|
className="underline"
|
|
href={props.history.next_page_url}
|
|
>
|
|
Seguinte
|
|
</Link>
|
|
)}
|
|
</nav>
|
|
</Panel>
|
|
</main>
|
|
);
|
|
}
|