@@ -1,5 +1,8 @@
|
||||
import { Head, Link, router, useForm } from '@inertiajs/react';
|
||||
import { Head, Link, router, useForm, usePage } from '@inertiajs/react';
|
||||
import { useEffect, useState } from 'react';
|
||||
import { usePreparationDirty } from '@/hooks/use-preparation-dirty';
|
||||
import { hasPreparationChanges } from '@/lib/requisition-history';
|
||||
import type { WorkContextState } from '@/types/work-context';
|
||||
import {
|
||||
FormErrors,
|
||||
PageTitle,
|
||||
@@ -244,14 +247,34 @@ function PreservedData({ record }: { record: SupplyRecord }) {
|
||||
);
|
||||
}
|
||||
|
||||
function Preparation({
|
||||
function useSupplyIdentity(props: Props, record?: SupplyRecord) {
|
||||
const page = usePage().props;
|
||||
const actor = page.auth?.user;
|
||||
const context = page.workContext as WorkContextState | null | undefined;
|
||||
return `supply:${actor?.tenant_id}:${actor?.id}:${props.kind}:${props.view}:${props.linkedContractKey ?? ''}:${props.linkedAgreementKey ?? ''}:${props.ready}:${props.canPrepare}:${props.canReview}:${props.canCustoms}:${props.orderLimitsReady ?? false}:${record?.record_key ?? 'new'}:${record?.version ?? 0}:${record?.state ?? 'new'}${context?.active ? `:context:${context.active}` : ''}`;
|
||||
}
|
||||
|
||||
function Preparation(input: {
|
||||
props: Props;
|
||||
record?: SupplyRecord;
|
||||
copy?: boolean;
|
||||
}) {
|
||||
const historyKey = `${useSupplyIdentity(input.props, input.record)}:${input.copy ? 'copy' : 'prepare'}`;
|
||||
return (
|
||||
<PreparationForm key={historyKey} {...input} historyKey={historyKey} />
|
||||
);
|
||||
}
|
||||
|
||||
function PreparationForm({
|
||||
props,
|
||||
record,
|
||||
copy = false,
|
||||
historyKey,
|
||||
}: {
|
||||
props: Props;
|
||||
record?: SupplyRecord;
|
||||
copy?: boolean;
|
||||
historyKey: string;
|
||||
}) {
|
||||
const defaults: Data =
|
||||
props.kind === 'policy'
|
||||
@@ -350,14 +373,17 @@ function Preparation({
|
||||
}
|
||||
}
|
||||
}
|
||||
const form = useForm({
|
||||
const initial = {
|
||||
operation_key: crypto.randomUUID(),
|
||||
record_key: copy ? undefined : record?.record_key,
|
||||
version: copy ? 0 : (record?.version ?? 0),
|
||||
action: 'save',
|
||||
reason: '',
|
||||
data: defaults,
|
||||
});
|
||||
};
|
||||
const form = useForm(historyKey, initial);
|
||||
const dirty = hasPreparationChanges(initial, form.data);
|
||||
const ref = usePreparationDirty(dirty);
|
||||
const [orderSearch, setOrderSearch] = useState(props.orderSearch ?? '');
|
||||
const [historyOptions, setHistoryOptions] = useState(props.histories);
|
||||
const [periodFrom, setPeriodFrom] = useState('');
|
||||
@@ -517,6 +543,8 @@ function Preparation({
|
||||
);
|
||||
return (
|
||||
<form
|
||||
ref={ref}
|
||||
data-preparation-dirty={dirty ? 'true' : 'false'}
|
||||
className="space-y-4"
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault();
|
||||
@@ -557,8 +585,16 @@ function Preparation({
|
||||
});
|
||||
form.post(`/compras/fornecimento/${props.kind}`, {
|
||||
preserveScroll: true,
|
||||
onSuccess: () =>
|
||||
form.setData('operation_key', crypto.randomUUID()),
|
||||
onSuccess: () => {
|
||||
const clean = {
|
||||
...initial,
|
||||
operation_key: crypto.randomUUID(),
|
||||
};
|
||||
form.setData(clean);
|
||||
form.clearErrors();
|
||||
router.remember(clean, `${historyKey}:data`);
|
||||
router.remember({}, `${historyKey}:errors`);
|
||||
},
|
||||
});
|
||||
}}
|
||||
>
|
||||
@@ -1119,22 +1155,38 @@ function Preparation({
|
||||
);
|
||||
}
|
||||
|
||||
function RecordActions({
|
||||
function RecordActions(input: { record: SupplyRecord; props: Props }) {
|
||||
const historyKey = `${useSupplyIdentity(input.props, input.record)}:action:${input.record.canApprove}:${input.record.canReturn}:${input.record.canSuspend}`;
|
||||
return (
|
||||
<RecordActionsForm
|
||||
key={historyKey}
|
||||
{...input}
|
||||
historyKey={historyKey}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
function RecordActionsForm({
|
||||
record,
|
||||
props,
|
||||
historyKey,
|
||||
}: {
|
||||
record: SupplyRecord;
|
||||
props: Props;
|
||||
historyKey: string;
|
||||
}) {
|
||||
const [action, setAction] = useState('');
|
||||
const form = useForm({
|
||||
const initial = {
|
||||
operation_key: crypto.randomUUID(),
|
||||
record_key: record.record_key,
|
||||
version: record.version,
|
||||
action: '',
|
||||
reason: '',
|
||||
data: { evidence: '', event_date: '' },
|
||||
});
|
||||
};
|
||||
const form = useForm(historyKey, initial);
|
||||
const action = form.data.action;
|
||||
const dirty = hasPreparationChanges(initial, form.data);
|
||||
const ref = usePreparationDirty(dirty, Boolean(action));
|
||||
const imports = props.kind === 'import';
|
||||
const choices: [string, string][] = imports
|
||||
? [
|
||||
@@ -1198,7 +1250,6 @@ function RecordActions({
|
||||
className={controlClass}
|
||||
value={action}
|
||||
onChange={(e) => {
|
||||
setAction(e.target.value);
|
||||
form.setData('action', e.target.value);
|
||||
}}
|
||||
>
|
||||
@@ -1214,11 +1265,23 @@ function RecordActions({
|
||||
)}
|
||||
{action && (
|
||||
<form
|
||||
ref={ref}
|
||||
data-preparation-dirty={dirty ? 'true' : 'false'}
|
||||
className="space-y-3"
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault();
|
||||
form.post(`/compras/fornecimento/${props.kind}`, {
|
||||
preserveScroll: true,
|
||||
onSuccess: () => {
|
||||
const clean = {
|
||||
...initial,
|
||||
operation_key: crypto.randomUUID(),
|
||||
};
|
||||
form.setData(clean);
|
||||
form.clearErrors();
|
||||
router.remember(clean, `${historyKey}:data`);
|
||||
router.remember({}, `${historyKey}:errors`);
|
||||
},
|
||||
});
|
||||
}}
|
||||
>
|
||||
|
||||
Reference in New Issue
Block a user