@@ -1,4 +1,7 @@
|
||||
import { Head, Link, useForm, usePage } from '@inertiajs/react';
|
||||
import { Head, Link, router, useForm, usePage } 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 {
|
||||
TableViewport,
|
||||
FormErrors,
|
||||
@@ -23,20 +26,28 @@ type Props = {
|
||||
requisitions: { id: number; number: string }[];
|
||||
};
|
||||
|
||||
export default function Conflicts({
|
||||
export default function Conflicts(props: Props) {
|
||||
const identity = `${useConflictPreparationIdentity()}:new`;
|
||||
return <DeclarationFields key={identity} {...props} identity={identity} />;
|
||||
}
|
||||
function DeclarationFields({
|
||||
declarations,
|
||||
suppliers,
|
||||
requisitions,
|
||||
}: Props) {
|
||||
identity,
|
||||
}: Props & { identity: string }) {
|
||||
const { auth } = usePage().props;
|
||||
const form = useForm({
|
||||
const initial = {
|
||||
scope: 'supplier',
|
||||
supplier_id: '',
|
||||
requisition_id: '',
|
||||
has_conflict: '1',
|
||||
known_since: '',
|
||||
reason: '',
|
||||
});
|
||||
};
|
||||
const form = useForm(identity, initial);
|
||||
const dirty = hasPreparationChanges(initial, form.data);
|
||||
const ref = usePreparationDirty(dirty);
|
||||
return (
|
||||
<>
|
||||
<Head title="Conflitos de interesses" />
|
||||
@@ -58,6 +69,8 @@ export default function Conflicts({
|
||||
description={`Autor: ${auth.user.name}. A data e a hora do registo são guardadas pelo servidor.`}
|
||||
>
|
||||
<form
|
||||
ref={ref}
|
||||
data-preparation-dirty={dirty ? 'true' : 'false'}
|
||||
className="space-y-5"
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault();
|
||||
@@ -75,7 +88,15 @@ export default function Conflicts({
|
||||
}));
|
||||
form.post('/conflitos', {
|
||||
preserveScroll: true,
|
||||
onSuccess: () => form.reset(),
|
||||
onSuccess: () => {
|
||||
form.setData(initial);
|
||||
form.clearErrors();
|
||||
router.remember(
|
||||
initial,
|
||||
`${identity}:data`,
|
||||
);
|
||||
router.remember({}, `${identity}:errors`);
|
||||
},
|
||||
});
|
||||
}}
|
||||
>
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
import { Head, Link, useForm } from '@inertiajs/react';
|
||||
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,
|
||||
@@ -12,18 +15,31 @@ import { conflictScope } from '@/types/conflict';
|
||||
import type { ConflictDeclaration } from '@/types/conflict';
|
||||
import { date } from '@/types/procurement';
|
||||
|
||||
export default function DeclarationHistory({
|
||||
export default function DeclarationHistory(props: {
|
||||
history: ConflictDeclaration[];
|
||||
canCorrect: boolean;
|
||||
}) {
|
||||
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,
|
||||
}: {
|
||||
identity: string;
|
||||
history: ConflictDeclaration[];
|
||||
canCorrect: boolean;
|
||||
}) {
|
||||
const current = history[history.length - 1];
|
||||
const form = useForm({
|
||||
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" />
|
||||
@@ -77,11 +93,25 @@ export default function DeclarationHistory({
|
||||
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`,
|
||||
);
|
||||
},
|
||||
});
|
||||
}}
|
||||
>
|
||||
|
||||
Reference in New Issue
Block a user