@@ -1,4 +1,7 @@
|
||||
import { 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 { router, useForm } from '@inertiajs/react';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { FormErrors, Panel, controlClass } from '@/components/procurement';
|
||||
|
||||
@@ -89,16 +92,28 @@ function Fields({
|
||||
);
|
||||
}
|
||||
|
||||
export function CompanyDetailsSettings({
|
||||
export function CompanyDetailsSettings(
|
||||
props: Omit<Parameters<typeof CompanyDetailsFields>[0], 'identity'>,
|
||||
) {
|
||||
const identity = useProcurementPreparationIdentity(
|
||||
`company-details:${props.version}:${props.ready}`,
|
||||
);
|
||||
return (
|
||||
<CompanyDetailsFields key={identity} {...props} identity={identity} />
|
||||
);
|
||||
}
|
||||
function CompanyDetailsFields({
|
||||
details,
|
||||
ready,
|
||||
version,
|
||||
identity,
|
||||
}: {
|
||||
identity: string;
|
||||
details: CompanyDetails | null;
|
||||
ready: boolean;
|
||||
version: number;
|
||||
}) {
|
||||
const form = useForm({
|
||||
const initial = {
|
||||
...(Object.fromEntries(
|
||||
Object.keys(identityFields).map((key) => [
|
||||
key,
|
||||
@@ -109,7 +124,16 @@ export function CompanyDetailsSettings({
|
||||
establishments: details?.establishments ?? [],
|
||||
version,
|
||||
reason: '',
|
||||
});
|
||||
};
|
||||
const form = useForm(identity, initial);
|
||||
const dirty = hasPreparationChanges(initial, form.data);
|
||||
const ref = usePreparationDirty(dirty);
|
||||
const clearPreparation = () => {
|
||||
form.setData(initial);
|
||||
form.clearErrors();
|
||||
router.remember(initial, `${identity}:data`);
|
||||
router.remember({}, `${identity}:errors`);
|
||||
};
|
||||
return (
|
||||
<Panel
|
||||
title="Dados da empresa"
|
||||
@@ -122,13 +146,18 @@ export function CompanyDetailsSettings({
|
||||
</p>
|
||||
) : (
|
||||
<form
|
||||
ref={ref}
|
||||
data-preparation-dirty={dirty ? 'true' : 'false'}
|
||||
className="space-y-6"
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault();
|
||||
form.transform((values) => ({ ...values, version }));
|
||||
form.post(
|
||||
'/administracao/parametrizacao/company_details',
|
||||
{ preserveScroll: true },
|
||||
{
|
||||
preserveScroll: true,
|
||||
onSuccess: clearPreparation,
|
||||
},
|
||||
);
|
||||
}}
|
||||
>
|
||||
|
||||
Reference in New Issue
Block a user