commit13
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { Head, Link, useForm } from '@inertiajs/react';
|
||||
import { Head, Link, useForm, usePage } from '@inertiajs/react';
|
||||
import { Plus, Save, Trash2 } from 'lucide-react';
|
||||
import { useEffect, useRef } from 'react';
|
||||
import {
|
||||
FormErrors,
|
||||
PageTitle,
|
||||
@@ -97,7 +98,10 @@ export default function RequisitionForm({
|
||||
defaultTaxRate?: string;
|
||||
catalogue?: CatalogueOptions;
|
||||
}) {
|
||||
const ready = usePage().props.internalRequisitionReady === true;
|
||||
const form = useForm({
|
||||
operation_key: ready ? crypto.randomUUID() : null,
|
||||
process_version: requisition?.process_version ?? null,
|
||||
cost_center_id: requisition ? String(requisition.cost_center_id) : '',
|
||||
priority: requisition?.priority ?? 'normal',
|
||||
needed_by: requisition?.needed_by ?? '',
|
||||
@@ -125,6 +129,14 @@ export default function RequisitionForm({
|
||||
downtime_started_at: i.downtime_started_at?.slice(0, 16) ?? '',
|
||||
})) ?? [emptyLine(defaultTaxRate)],
|
||||
});
|
||||
const formElement = useRef<HTMLFormElement>(null);
|
||||
useEffect(() => {
|
||||
window.dispatchEvent(
|
||||
new CustomEvent('sgc:context-dirty', {
|
||||
detail: { form: formElement.current, dirty: form.isDirty },
|
||||
}),
|
||||
);
|
||||
}, [form.isDirty]);
|
||||
const changeLine = (index: number, field: keyof Line, value: string) =>
|
||||
form.setData(
|
||||
'items',
|
||||
@@ -157,12 +169,25 @@ export default function RequisitionForm({
|
||||
description="Descreva a necessidade e os artigos. Guarde o rascunho e confira os valores antes de submeter."
|
||||
/>
|
||||
<form
|
||||
ref={formElement}
|
||||
data-context-save
|
||||
className="space-y-6"
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault();
|
||||
const element = e.currentTarget;
|
||||
const result = (success: boolean) =>
|
||||
window.dispatchEvent(
|
||||
new CustomEvent('sgc:context-save-result', {
|
||||
detail: { form: element, success },
|
||||
}),
|
||||
);
|
||||
const options = {
|
||||
onSuccess: () => result(true),
|
||||
onError: () => result(false),
|
||||
};
|
||||
if (requisition)
|
||||
form.put(`/requisicoes/${requisition.id}`);
|
||||
else form.post('/requisicoes');
|
||||
form.put(`/requisicoes/${requisition.id}`, options);
|
||||
else form.post('/requisicoes', options);
|
||||
}}
|
||||
>
|
||||
<FormErrors errors={form.errors} />
|
||||
|
||||
Reference in New Issue
Block a user