@@ -1,4 +1,7 @@
|
||||
import { Head, Link, useForm } from '@inertiajs/react';
|
||||
import { Head, Link, router, useForm, usePage } from '@inertiajs/react';
|
||||
import { usePreparationDirty } from '@/hooks/use-preparation-dirty';
|
||||
import { hasPreparationChanges } from '@/lib/requisition-history';
|
||||
import type { WorkContextState } from '@/types/work-context';
|
||||
import { PageTitle, Panel } from '@/components/procurement';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Input } from '@/components/ui/input';
|
||||
@@ -42,19 +45,53 @@ const states: Record<string, string> = {
|
||||
cancelled: 'Cancelado',
|
||||
};
|
||||
|
||||
function ReasonForm({
|
||||
function useControlIdentity() {
|
||||
const page = usePage().props;
|
||||
const actor = page.auth?.user;
|
||||
const actorId = typeof page.actorId === 'number' ? page.actorId : actor?.id;
|
||||
const context = page.workContext as WorkContextState | null | undefined;
|
||||
const permissions = Array.isArray(page.permissions)
|
||||
? page.permissions
|
||||
.filter((p) => typeof p === 'string')
|
||||
.sort()
|
||||
.join(',')
|
||||
: '';
|
||||
return `purchase-controls:${actor?.tenant_id}:${actorId}:${context?.active ?? 'legacy'}:${permissions}`;
|
||||
}
|
||||
|
||||
function ReasonForm(input: { url: string; decisions?: boolean }) {
|
||||
const identity = `${useControlIdentity()}:reason:${input.url}:${input.decisions ?? false}`;
|
||||
return <ReasonFields key={identity} {...input} identity={identity} />;
|
||||
}
|
||||
|
||||
function ReasonFields({
|
||||
url,
|
||||
decisions = false,
|
||||
identity,
|
||||
}: {
|
||||
url: string;
|
||||
decisions?: boolean;
|
||||
identity: string;
|
||||
}) {
|
||||
const form = useForm({ reason: '', decision: 'approved' });
|
||||
const initial = { reason: '', decision: 'approved' };
|
||||
const form = useForm(identity, initial);
|
||||
const dirty = hasPreparationChanges(initial, form.data);
|
||||
const ref = usePreparationDirty(dirty);
|
||||
return (
|
||||
<form
|
||||
ref={ref}
|
||||
data-preparation-dirty={dirty ? 'true' : 'false'}
|
||||
onSubmit={(event) => {
|
||||
event.preventDefault();
|
||||
form.post(url, { preserveScroll: true });
|
||||
form.post(url, {
|
||||
preserveScroll: true,
|
||||
onSuccess: () => {
|
||||
form.setData(initial);
|
||||
form.clearErrors();
|
||||
router.remember(initial, `${identity}:data`);
|
||||
router.remember({}, `${identity}:errors`);
|
||||
},
|
||||
});
|
||||
}}
|
||||
className="mt-3 space-y-3"
|
||||
>
|
||||
@@ -98,20 +135,48 @@ function ReasonForm({
|
||||
}
|
||||
|
||||
function PolicyForm({ previousId }: { previousId: number }) {
|
||||
const form = useForm({
|
||||
const identity = `${useControlIdentity()}:policy:${previousId}`;
|
||||
return (
|
||||
<PolicyFields
|
||||
key={identity}
|
||||
previousId={previousId}
|
||||
identity={identity}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
function PolicyFields({
|
||||
previousId,
|
||||
identity,
|
||||
}: {
|
||||
previousId: number;
|
||||
identity: string;
|
||||
}) {
|
||||
const initial = {
|
||||
previous_id: previousId,
|
||||
mode: 'unissued_award',
|
||||
enabled: false,
|
||||
valid_from: '',
|
||||
valid_until: '',
|
||||
reason: '',
|
||||
});
|
||||
};
|
||||
const form = useForm(identity, initial);
|
||||
const dirty = hasPreparationChanges(initial, form.data);
|
||||
const ref = usePreparationDirty(dirty);
|
||||
return (
|
||||
<form
|
||||
ref={ref}
|
||||
data-preparation-dirty={dirty ? 'true' : 'false'}
|
||||
onSubmit={(event) => {
|
||||
event.preventDefault();
|
||||
form.post('/revisao-compras/politicas', {
|
||||
preserveScroll: true,
|
||||
onSuccess: () => {
|
||||
form.setData(initial);
|
||||
form.clearErrors();
|
||||
router.remember(initial, `${identity}:data`);
|
||||
router.remember({}, `${identity}:errors`);
|
||||
},
|
||||
});
|
||||
}}
|
||||
className="grid gap-4 sm:grid-cols-2"
|
||||
@@ -170,12 +235,29 @@ function PolicyForm({ previousId }: { previousId: number }) {
|
||||
}
|
||||
|
||||
function RevisionForm({ id }: { id: number }) {
|
||||
const form = useForm({ needed_by: '', justification: '' });
|
||||
const identity = `${useControlIdentity()}:revision:${id}`;
|
||||
return <RevisionFields key={identity} id={id} identity={identity} />;
|
||||
}
|
||||
|
||||
function RevisionFields({ id, identity }: { id: number; identity: string }) {
|
||||
const initial = { needed_by: '', justification: '' };
|
||||
const form = useForm(identity, initial);
|
||||
const dirty = hasPreparationChanges(initial, form.data);
|
||||
const ref = usePreparationDirty(dirty);
|
||||
return (
|
||||
<form
|
||||
ref={ref}
|
||||
data-preparation-dirty={dirty ? 'true' : 'false'}
|
||||
onSubmit={(event) => {
|
||||
event.preventDefault();
|
||||
form.post(`/cancelamentos/${id}/revisao`);
|
||||
form.post(`/cancelamentos/${id}/revisao`, {
|
||||
onSuccess: () => {
|
||||
form.setData(initial);
|
||||
form.clearErrors();
|
||||
router.remember(initial, `${identity}:data`);
|
||||
router.remember({}, `${identity}:errors`);
|
||||
},
|
||||
});
|
||||
}}
|
||||
className="mt-3 grid gap-3"
|
||||
>
|
||||
|
||||
Reference in New Issue
Block a user