@@ -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 {
|
||||
FormErrors,
|
||||
PageTitle,
|
||||
@@ -107,7 +110,34 @@ const qty = (milli: number | string, mean = false) =>
|
||||
});
|
||||
|
||||
function Prepare({ props, old }: { props: Props; old?: HistoryRecord }) {
|
||||
const form = useForm({
|
||||
const identity = `${useHistoryIdentity(props, old)}:prepare`;
|
||||
return (
|
||||
<PrepareForm
|
||||
key={identity}
|
||||
props={props}
|
||||
old={old}
|
||||
identity={identity}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
function useHistoryIdentity(props: Props, record?: HistoryRecord) {
|
||||
const page = usePage().props;
|
||||
const actor = page.auth?.user;
|
||||
const context = page.workContext as WorkContextState | null | undefined;
|
||||
return `supply-history:${actor?.tenant_id}:${props.actorId ?? actor?.id}:${props.ready}:${props.canPrepare}:${props.canReview}:${record?.record_key ?? 'new'}:${record?.version ?? 0}:${record?.state ?? 'new'}${context?.active ? `:context:${context.active}` : ''}`;
|
||||
}
|
||||
|
||||
function PrepareForm({
|
||||
props,
|
||||
old,
|
||||
identity,
|
||||
}: {
|
||||
props: Props;
|
||||
old?: HistoryRecord;
|
||||
identity: string;
|
||||
}) {
|
||||
const initial = {
|
||||
operation_key: crypto.randomUUID(),
|
||||
record_key: old?.record_key,
|
||||
version: old?.version ?? 0,
|
||||
@@ -122,7 +152,10 @@ function Prepare({ props, old }: { props: Props; old?: HistoryRecord }) {
|
||||
quality: old?.data.quality ?? 'unknown',
|
||||
pattern: old?.data.pattern ?? 'unknown',
|
||||
},
|
||||
});
|
||||
};
|
||||
const form = useForm(identity, initial);
|
||||
const dirty = hasPreparationChanges(initial, form.data);
|
||||
const ref = usePreparationDirty(dirty);
|
||||
const select = (
|
||||
key: keyof typeof form.data.data,
|
||||
label: string,
|
||||
@@ -153,11 +186,23 @@ function Prepare({ props, old }: { props: Props; old?: HistoryRecord }) {
|
||||
);
|
||||
return (
|
||||
<form
|
||||
ref={ref}
|
||||
data-preparation-dirty={dirty ? 'true' : 'false'}
|
||||
className="mt-4 space-y-4"
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault();
|
||||
form.post('/compras/fornecimento/history', {
|
||||
preserveScroll: true,
|
||||
onSuccess: () => {
|
||||
const clean = {
|
||||
...initial,
|
||||
operation_key: crypto.randomUUID(),
|
||||
};
|
||||
form.setData(clean);
|
||||
form.clearErrors();
|
||||
router.remember(clean, `${identity}:data`);
|
||||
router.remember({}, `${identity}:errors`);
|
||||
},
|
||||
});
|
||||
}}
|
||||
>
|
||||
@@ -393,21 +438,32 @@ function Facts({
|
||||
);
|
||||
}
|
||||
|
||||
function Decision({
|
||||
function Decision(input: { props: Props; record: HistoryRecord }) {
|
||||
const r = input.record;
|
||||
const identity = `${useHistoryIdentity(input.props, r)}:decision:${r.canApprove}:${r.canReturn}:${r.canSuspend}`;
|
||||
return <DecisionForm key={identity} {...input} identity={identity} />;
|
||||
}
|
||||
|
||||
function DecisionForm({
|
||||
props,
|
||||
record: r,
|
||||
identity,
|
||||
}: {
|
||||
props: Props;
|
||||
record: HistoryRecord;
|
||||
identity: string;
|
||||
}) {
|
||||
const form = useForm({
|
||||
const initial = {
|
||||
operation_key: crypto.randomUUID(),
|
||||
record_key: r.record_key,
|
||||
version: r.version,
|
||||
action: '',
|
||||
reason: '',
|
||||
data: { confirm_scope: false },
|
||||
});
|
||||
};
|
||||
const form = useForm(identity, initial);
|
||||
const dirty = hasPreparationChanges(initial, form.data);
|
||||
const ref = usePreparationDirty(dirty, Boolean(form.data.action));
|
||||
return (
|
||||
<div className="mt-4 space-y-4">
|
||||
{props.canPrepare && ['draft', 'returned'].includes(r.state) && (
|
||||
@@ -449,11 +505,29 @@ function Decision({
|
||||
</label>
|
||||
{form.data.action && (
|
||||
<form
|
||||
ref={ref}
|
||||
data-preparation-dirty={dirty ? 'true' : 'false'}
|
||||
className="space-y-3"
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault();
|
||||
form.post('/compras/fornecimento/history', {
|
||||
preserveScroll: true,
|
||||
onSuccess: () => {
|
||||
const clean = {
|
||||
...initial,
|
||||
operation_key: crypto.randomUUID(),
|
||||
};
|
||||
form.setData(clean);
|
||||
form.clearErrors();
|
||||
router.remember(
|
||||
clean,
|
||||
`${identity}:data`,
|
||||
);
|
||||
router.remember(
|
||||
{},
|
||||
`${identity}:errors`,
|
||||
);
|
||||
},
|
||||
});
|
||||
}}
|
||||
>
|
||||
|
||||
Reference in New Issue
Block a user