@@ -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 {
|
||||
Panel,
|
||||
PageTitle,
|
||||
@@ -31,14 +34,38 @@ type Policy = {
|
||||
};
|
||||
|
||||
function Decision({ url }: { url: string }) {
|
||||
const form = useForm({ decision: 'rejected', reason: '' });
|
||||
const identity = `${useOrderControlIdentity()}:decision:${url}`;
|
||||
return <DecisionFields key={identity} url={url} identity={identity} />;
|
||||
}
|
||||
|
||||
function useOrderControlIdentity() {
|
||||
const page = usePage().props;
|
||||
const actor = page.auth?.user;
|
||||
const context = page.workContext as WorkContextState | null | undefined;
|
||||
return `order-cancellations:${actor?.tenant_id}:${actor?.id}:${context?.active ?? 'legacy'}:${page.canAdminister === true}`;
|
||||
}
|
||||
|
||||
function DecisionFields({ url, identity }: { url: string; identity: string }) {
|
||||
const initial = { decision: 'rejected', 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'}
|
||||
className="mt-3 space-y-3"
|
||||
onSubmit={(event) => {
|
||||
event.preventDefault();
|
||||
if (window.confirm('Confirmar esta decisão definitiva?'))
|
||||
form.post(url);
|
||||
form.post(url, {
|
||||
onSuccess: () => {
|
||||
form.setData(initial);
|
||||
form.clearErrors();
|
||||
router.remember(initial, `${identity}:data`);
|
||||
router.remember({}, `${identity}:errors`);
|
||||
},
|
||||
});
|
||||
}}
|
||||
>
|
||||
<FormErrors errors={form.errors} />
|
||||
@@ -74,13 +101,38 @@ function Decision({ url }: { url: string }) {
|
||||
}
|
||||
|
||||
function Revision({ record }: { record: Record }) {
|
||||
const form = useForm({ needed_by: '', reason: '' });
|
||||
const identity = `${useOrderControlIdentity()}:revision:${record.id}:${record.status}:${record.revised_requisition_id ?? 0}`;
|
||||
return (
|
||||
<RevisionFields key={identity} record={record} identity={identity} />
|
||||
);
|
||||
}
|
||||
|
||||
function RevisionFields({
|
||||
record,
|
||||
identity,
|
||||
}: {
|
||||
record: Record;
|
||||
identity: string;
|
||||
}) {
|
||||
const initial = { needed_by: '', 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'}
|
||||
className="mt-3 space-y-3"
|
||||
onSubmit={(event) => {
|
||||
event.preventDefault();
|
||||
form.post(`/ordens/cancelamentos/${record.id}/rever`);
|
||||
form.post(`/ordens/cancelamentos/${record.id}/rever`, {
|
||||
onSuccess: () => {
|
||||
form.setData(initial);
|
||||
form.clearErrors();
|
||||
router.remember(initial, `${identity}:data`);
|
||||
router.remember({}, `${identity}:errors`);
|
||||
},
|
||||
});
|
||||
}}
|
||||
>
|
||||
<FormErrors errors={form.errors} />
|
||||
@@ -118,11 +170,7 @@ function Revision({ record }: { record: Record }) {
|
||||
);
|
||||
}
|
||||
|
||||
export default function OrderCancellations({
|
||||
records,
|
||||
policies,
|
||||
canAdminister,
|
||||
}: {
|
||||
type Props = {
|
||||
records: {
|
||||
data: Record[];
|
||||
prev_page_url: string | null;
|
||||
@@ -130,15 +178,37 @@ export default function OrderCancellations({
|
||||
};
|
||||
policies: Policy[];
|
||||
canAdminister: boolean;
|
||||
}) {
|
||||
const policy = useForm({
|
||||
};
|
||||
|
||||
export default function OrderCancellationsPage(props: Props) {
|
||||
const identity = useOrderControlIdentity();
|
||||
return (
|
||||
<OrderCancellations
|
||||
key={`${identity}:policy:${props.policies[0]?.id ?? 0}`}
|
||||
{...props}
|
||||
identity={identity}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
function OrderCancellations({
|
||||
records,
|
||||
policies,
|
||||
canAdminister,
|
||||
identity,
|
||||
}: Props & { identity: string }) {
|
||||
const policyKey = `${identity}:policy:${policies[0]?.id ?? 0}`;
|
||||
const initial = {
|
||||
previous_id: policies[0]?.id ?? 0,
|
||||
mode: 'issued_order',
|
||||
enabled: false,
|
||||
valid_from: '',
|
||||
valid_until: '',
|
||||
reason: '',
|
||||
});
|
||||
};
|
||||
const policy = useForm(policyKey, initial);
|
||||
const dirty = hasPreparationChanges(initial, policy.data);
|
||||
const ref = usePreparationDirty(dirty, canAdminister);
|
||||
const labels: { [key: string]: string } = {
|
||||
pending: 'Pendente',
|
||||
approved: 'Aprovado',
|
||||
@@ -242,6 +312,10 @@ export default function OrderCancellations({
|
||||
<details className="mt-4">
|
||||
<summary>Propor nova versão</summary>
|
||||
<form
|
||||
ref={ref}
|
||||
data-preparation-dirty={
|
||||
dirty ? 'true' : 'false'
|
||||
}
|
||||
className="mt-3 space-y-3"
|
||||
onSubmit={(event) => {
|
||||
event.preventDefault();
|
||||
@@ -251,6 +325,20 @@ export default function OrderCancellations({
|
||||
}));
|
||||
policy.post(
|
||||
'/ordens/cancelamentos/politicas',
|
||||
{
|
||||
onSuccess: () => {
|
||||
policy.setData(initial);
|
||||
policy.clearErrors();
|
||||
router.remember(
|
||||
initial,
|
||||
`${policyKey}:data`,
|
||||
);
|
||||
router.remember(
|
||||
{},
|
||||
`${policyKey}:errors`,
|
||||
);
|
||||
},
|
||||
},
|
||||
);
|
||||
}}
|
||||
>
|
||||
@@ -325,7 +413,7 @@ export default function OrderCancellations({
|
||||
</>
|
||||
);
|
||||
}
|
||||
OrderCancellations.layout = {
|
||||
OrderCancellationsPage.layout = {
|
||||
breadcrumbs: [
|
||||
{ title: 'Ordens', href: '/ordens' },
|
||||
{ title: 'Cancelamentos', href: '/ordens/cancelamentos' },
|
||||
|
||||
Reference in New Issue
Block a user