@@ -1,5 +1,6 @@
|
||||
import { Head, Link, useForm, usePage } from '@inertiajs/react';
|
||||
import { useEffect, useRef } from 'react';
|
||||
import { usePreparationDirty } from '@/hooks/use-preparation-dirty';
|
||||
import { hasPreparationChanges } from '@/lib/requisition-history';
|
||||
import { FormErrors, PageTitle, Panel } from '@/components/procurement';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Input } from '@/components/ui/input';
|
||||
@@ -18,7 +19,7 @@ type Source = {
|
||||
}[];
|
||||
};
|
||||
|
||||
export default function Amendment({
|
||||
function AmendmentContent({
|
||||
order,
|
||||
sources,
|
||||
history,
|
||||
@@ -41,41 +42,28 @@ export default function Amendment({
|
||||
}[];
|
||||
}) {
|
||||
const actor = usePage().props.auth?.user;
|
||||
const initial = {
|
||||
operation_key: crypto.randomUUID(),
|
||||
reason: '',
|
||||
sources: sources.map((source) => ({
|
||||
origin: source.origin,
|
||||
version: source.version,
|
||||
plan_hash: source.plan_hash,
|
||||
allocations: source.allocations.map(
|
||||
({ allocation_key, subtotal, total }) => ({
|
||||
allocation_key,
|
||||
subtotal,
|
||||
total,
|
||||
}),
|
||||
),
|
||||
})),
|
||||
};
|
||||
const form = useForm(
|
||||
`order-parts-amendment:${actor?.tenant_id}:${actor?.id}:${order.id}:${sources.map((s) => `${s.origin}:${s.version}:${s.plan_hash}`).join(',')}`,
|
||||
{
|
||||
operation_key: crypto.randomUUID(),
|
||||
reason: '',
|
||||
sources: sources.map((source) => ({
|
||||
origin: source.origin,
|
||||
version: source.version,
|
||||
plan_hash: source.plan_hash,
|
||||
allocations: source.allocations.map(
|
||||
({ allocation_key, subtotal, total }) => ({
|
||||
allocation_key,
|
||||
subtotal,
|
||||
total,
|
||||
}),
|
||||
),
|
||||
})),
|
||||
},
|
||||
initial,
|
||||
);
|
||||
const ref = useRef<HTMLFormElement>(null);
|
||||
useEffect(() => {
|
||||
const current = ref.current;
|
||||
window.dispatchEvent(
|
||||
new CustomEvent('sgc:context-dirty', {
|
||||
detail: { form: current, dirty: form.isDirty },
|
||||
}),
|
||||
);
|
||||
return () => {
|
||||
window.dispatchEvent(
|
||||
new CustomEvent('sgc:context-dirty', {
|
||||
detail: { form: current, dirty: false },
|
||||
}),
|
||||
);
|
||||
};
|
||||
}, [form.isDirty]);
|
||||
const dirty = hasPreparationChanges(initial, form.data);
|
||||
const ref = usePreparationDirty(dirty);
|
||||
const update = (
|
||||
origin: number,
|
||||
allocation: number,
|
||||
@@ -115,6 +103,7 @@ export default function Amendment({
|
||||
</p>
|
||||
<form
|
||||
ref={ref}
|
||||
data-preparation-dirty={dirty ? 'true' : 'false'}
|
||||
className="grid gap-4"
|
||||
onSubmit={(event) => {
|
||||
event.preventDefault();
|
||||
@@ -261,3 +250,11 @@ export default function Amendment({
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
export default function Amendment(
|
||||
props: Parameters<typeof AmendmentContent>[0],
|
||||
) {
|
||||
const actor = usePage().props.auth?.user;
|
||||
const identity = `order-parts-amendment:${actor?.tenant_id}:${actor?.id}:${props.order.id}:${props.sources.map((source) => `${source.origin}:${source.version}:${source.plan_hash}`).join(',')}`;
|
||||
return <AmendmentContent key={identity} {...props} />;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user