@@ -1,4 +1,7 @@
|
||||
import { Link, useForm } from '@inertiajs/react';
|
||||
import { Link, router, useForm } from '@inertiajs/react';
|
||||
import { useProcurementPreparationIdentity } from '@/hooks/use-procurement-preparation-identity';
|
||||
import { usePreparationDirty } from '@/hooks/use-preparation-dirty';
|
||||
import { hasPreparationChanges } from '@/lib/requisition-history';
|
||||
import { Panel, FormErrors, controlClass } from '@/components/procurement';
|
||||
import { Button } from '@/components/ui/button';
|
||||
|
||||
@@ -8,18 +11,29 @@ export type OrderCancellationState = {
|
||||
canRequest: boolean;
|
||||
};
|
||||
|
||||
export default function OrderCancellationRequest({
|
||||
type Props = { orderId: number; state: OrderCancellationState };
|
||||
export default function OrderCancellationRequest(props: Props) {
|
||||
const identity = useProcurementPreparationIdentity(
|
||||
`order-cancellation:${props.orderId}:${props.state.enabled}:${props.state.state}:${props.state.canRequest}`,
|
||||
);
|
||||
return <CancellationFields key={identity} {...props} identity={identity} />;
|
||||
}
|
||||
function CancellationFields({
|
||||
orderId,
|
||||
state,
|
||||
}: {
|
||||
orderId: number;
|
||||
state: OrderCancellationState;
|
||||
}) {
|
||||
const form = useForm({
|
||||
identity,
|
||||
}: Props & { identity: string }) {
|
||||
const initial = {
|
||||
operation_key: crypto.randomUUID(),
|
||||
reason: '',
|
||||
evidence: '',
|
||||
});
|
||||
};
|
||||
const form = useForm(identity, initial);
|
||||
const dirty = hasPreparationChanges(initial, form.data);
|
||||
const ref = usePreparationDirty(
|
||||
dirty,
|
||||
state.enabled && state.canRequest && state.state === 'issued',
|
||||
);
|
||||
if (!state.enabled && state.state === 'issued') return null;
|
||||
return (
|
||||
<Panel title="Estado operacional e cancelamento">
|
||||
@@ -42,10 +56,23 @@ export default function OrderCancellationRequest({
|
||||
<details className="mt-4 rounded border p-3">
|
||||
<summary>Pedir cancelamento integral</summary>
|
||||
<form
|
||||
ref={ref}
|
||||
data-preparation-dirty={dirty ? 'true' : 'false'}
|
||||
className="mt-4 space-y-4"
|
||||
onSubmit={(event) => {
|
||||
event.preventDefault();
|
||||
form.post(`/ordens/${orderId}/cancelamento`);
|
||||
form.post(`/ordens/${orderId}/cancelamento`, {
|
||||
onSuccess: () => {
|
||||
const next = {
|
||||
...initial,
|
||||
operation_key: crypto.randomUUID(),
|
||||
};
|
||||
form.setData(next);
|
||||
form.clearErrors();
|
||||
router.remember(next, `${identity}:data`);
|
||||
router.remember({}, `${identity}:errors`);
|
||||
},
|
||||
});
|
||||
}}
|
||||
>
|
||||
<FormErrors errors={form.errors} />
|
||||
|
||||
Reference in New Issue
Block a user