266 lines
11 KiB
TypeScript
266 lines
11 KiB
TypeScript
import { Head, Link, router, useForm } from '@inertiajs/react';
|
|
import { useConflictPreparationIdentity } from '@/hooks/use-conflict-preparation-identity';
|
|
import { usePreparationDirty } from '@/hooks/use-preparation-dirty';
|
|
import { hasPreparationChanges } from '@/lib/requisition-history';
|
|
import {
|
|
FormErrors,
|
|
PageTitle,
|
|
Panel,
|
|
controlClass,
|
|
} from '@/components/procurement';
|
|
import { Button } from '@/components/ui/button';
|
|
import { Input } from '@/components/ui/input';
|
|
import { Label } from '@/components/ui/label';
|
|
import { conflictScope } from '@/types/conflict';
|
|
import type { ConflictDeclaration } from '@/types/conflict';
|
|
import { date } from '@/types/procurement';
|
|
|
|
export default function DeclarationHistory(props: {
|
|
history: ConflictDeclaration[];
|
|
canCorrect: boolean;
|
|
canAuthoriseRelease?: boolean;
|
|
release?: {
|
|
id: number;
|
|
authorised_by: number;
|
|
authorised_at: string;
|
|
reason: string;
|
|
} | null;
|
|
}) {
|
|
const current = props.history[props.history.length - 1];
|
|
const identity = `${useConflictPreparationIdentity()}:correction:${current.id}:${current.declared_at}:${props.canCorrect}`;
|
|
return <HistoryFields key={identity} {...props} identity={identity} />;
|
|
}
|
|
function HistoryFields({
|
|
history,
|
|
canCorrect,
|
|
identity,
|
|
canAuthoriseRelease,
|
|
release,
|
|
}: {
|
|
identity: string;
|
|
history: ConflictDeclaration[];
|
|
canCorrect: boolean;
|
|
canAuthoriseRelease?: boolean;
|
|
release?: {
|
|
id: number;
|
|
authorised_by: number;
|
|
authorised_at: string;
|
|
reason: string;
|
|
} | null;
|
|
}) {
|
|
const current = history[history.length - 1];
|
|
const initial = {
|
|
reason: current.reason,
|
|
known_since: current.known_since.slice(0, 10),
|
|
};
|
|
const form = useForm(identity, initial);
|
|
const dirty = canCorrect && hasPreparationChanges(initial, form.data);
|
|
const ref = usePreparationDirty(dirty, canCorrect);
|
|
return (
|
|
<>
|
|
<Head title="Histórico da declaração" />
|
|
<div className="mx-auto flex w-full max-w-4xl flex-col gap-7 p-5 lg:p-8">
|
|
<PageTitle
|
|
eyebrow="Conflitos de interesses"
|
|
title="Histórico da declaração"
|
|
description={`${current.author.name} · ${conflictScope(current)}`}
|
|
action={
|
|
<Button asChild variant="outline">
|
|
<Link href="/conflitos">Voltar às declarações</Link>
|
|
</Button>
|
|
}
|
|
/>
|
|
<Panel
|
|
title={
|
|
release
|
|
? 'Impedimento levantado pela direcção'
|
|
: current.has_conflict
|
|
? 'Conflito registado com impedimento'
|
|
: 'Ausência de conflito declarada'
|
|
}
|
|
description="Uma ausência declarada não retira outros conflitos registados. Não há caducidade ou levantamento automático."
|
|
>
|
|
<ol className="space-y-5">
|
|
{history.map((d) => (
|
|
<li
|
|
className="border-l-2 border-teal-600/40 pl-4"
|
|
key={d.id}
|
|
>
|
|
<p className="text-sm font-semibold">
|
|
Registo #{d.id}
|
|
{d.previous_id
|
|
? ` · Correcção de #${d.previous_id}`
|
|
: ' · Declaração inicial'}
|
|
</p>
|
|
<p className="mt-1 text-xs text-muted-foreground">
|
|
{d.author.name} · Registado em{' '}
|
|
{date(d.declared_at)} às{' '}
|
|
{d.declared_at.slice(11, 19)} UTC ·
|
|
Conhecimento em {date(d.known_since)}
|
|
</p>
|
|
<p className="mt-3 text-sm whitespace-pre-wrap">
|
|
{d.reason}
|
|
</p>
|
|
</li>
|
|
))}
|
|
</ol>
|
|
</Panel>
|
|
{release && (
|
|
<Panel
|
|
title="Autorização da direcção"
|
|
description={`Autorizada por #${release.authorised_by} em ${date(release.authorised_at)}. Aplica-se apenas a esta declaração e às suas correcções.`}
|
|
>
|
|
<p className="text-sm whitespace-pre-wrap">
|
|
{release.reason}
|
|
</p>
|
|
</Panel>
|
|
)}
|
|
{canAuthoriseRelease && (
|
|
<ReleaseForm
|
|
identity={identity}
|
|
declarationId={current.id}
|
|
/>
|
|
)}
|
|
{canCorrect && (
|
|
<Panel
|
|
title="Corrigir fundamento ou data de conhecimento"
|
|
description="A correcção cria um novo registo e mantém os anteriores. O autor, âmbito e situação declarada permanecem iguais."
|
|
>
|
|
<form
|
|
ref={ref}
|
|
data-preparation-dirty={dirty ? 'true' : 'false'}
|
|
className="space-y-4"
|
|
onSubmit={(e) => {
|
|
e.preventDefault();
|
|
form.put(`/conflitos/${current.id}`, {
|
|
preserveScroll: true,
|
|
onSuccess: () => {
|
|
form.setData(initial);
|
|
form.clearErrors();
|
|
router.remember(
|
|
initial,
|
|
`${identity}:data`,
|
|
);
|
|
router.remember(
|
|
{},
|
|
`${identity}:errors`,
|
|
);
|
|
},
|
|
});
|
|
}}
|
|
>
|
|
<FormErrors errors={form.errors} />
|
|
<div className="space-y-2">
|
|
<Label htmlFor="correct-date">
|
|
Data de conhecimento
|
|
</Label>
|
|
<Input
|
|
id="correct-date"
|
|
type="date"
|
|
required
|
|
value={form.data.known_since}
|
|
onChange={(e) =>
|
|
form.setData(
|
|
'known_since',
|
|
e.target.value,
|
|
)
|
|
}
|
|
/>
|
|
</div>
|
|
<div className="space-y-2">
|
|
<Label htmlFor="correct-reason">
|
|
Fundamento corrigido
|
|
</Label>
|
|
<textarea
|
|
id="correct-reason"
|
|
className={`${controlClass} h-28 py-3`}
|
|
required
|
|
minLength={10}
|
|
maxLength={5000}
|
|
value={form.data.reason}
|
|
onChange={(e) =>
|
|
form.setData('reason', e.target.value)
|
|
}
|
|
/>
|
|
</div>
|
|
<Button disabled={form.processing}>
|
|
Registar correcção no histórico
|
|
</Button>
|
|
</form>
|
|
</Panel>
|
|
)}
|
|
</div>
|
|
</>
|
|
);
|
|
}
|
|
function ReleaseForm({
|
|
identity,
|
|
declarationId,
|
|
}: {
|
|
identity: string;
|
|
declarationId: number;
|
|
}) {
|
|
const form = useForm(`${identity}:release`, {
|
|
reason: '',
|
|
operation_key: crypto.randomUUID(),
|
|
});
|
|
const dirty = form.data.reason.trim() !== '';
|
|
const ref = usePreparationDirty(dirty, true);
|
|
return (
|
|
<Panel
|
|
title="Autorizar levantamento do impedimento"
|
|
description="Decisão expressa da direcção. A declaração original permanece no histórico; outros conflitos continuam a impedir a participação."
|
|
>
|
|
<form
|
|
ref={ref}
|
|
data-preparation-dirty={dirty ? 'true' : 'false'}
|
|
className="space-y-4"
|
|
onSubmit={(event) => {
|
|
event.preventDefault();
|
|
form.post(`/conflitos/${declarationId}/levantamento`, {
|
|
preserveScroll: true,
|
|
onSuccess: () => {
|
|
const initial = {
|
|
reason: '',
|
|
operation_key: crypto.randomUUID(),
|
|
};
|
|
form.setData(initial);
|
|
form.clearErrors();
|
|
router.remember(
|
|
initial,
|
|
`${identity}:release:data`,
|
|
);
|
|
router.remember({}, `${identity}:release:errors`);
|
|
},
|
|
});
|
|
}}
|
|
>
|
|
<FormErrors errors={form.errors} />
|
|
<Label htmlFor="release-reason">
|
|
Fundamento da autorização
|
|
</Label>
|
|
<textarea
|
|
id="release-reason"
|
|
required
|
|
minLength={10}
|
|
maxLength={5000}
|
|
className={`${controlClass} h-28 py-3`}
|
|
value={form.data.reason}
|
|
onChange={(event) =>
|
|
form.setData('reason', event.target.value)
|
|
}
|
|
/>
|
|
<Button disabled={form.processing}>
|
|
Autorizar levantamento
|
|
</Button>
|
|
</form>
|
|
</Panel>
|
|
);
|
|
}
|
|
DeclarationHistory.layout = {
|
|
breadcrumbs: [
|
|
{ title: 'Conflitos de interesses', href: '/conflitos' },
|
|
{ title: 'Histórico', href: '#' },
|
|
],
|
|
};
|