@@ -1,5 +1,7 @@
|
||||
import { useState } from 'react';
|
||||
import { useForm } from '@inertiajs/react';
|
||||
import { router, useForm, useRemember } from '@inertiajs/react';
|
||||
import { usePreparationDirty } from '@/hooks/use-preparation-dirty';
|
||||
import { hasPreparationChanges } from '@/lib/requisition-history';
|
||||
import { useInventoryPreparationIdentity } from '@/hooks/use-inventory-preparation-identity';
|
||||
import { FormErrors, controlClass } from '@/components/procurement';
|
||||
import { Button } from '@/components/ui/button';
|
||||
|
||||
@@ -12,8 +14,16 @@ type Props = {
|
||||
data?: Record<string, string | number | boolean | null>;
|
||||
};
|
||||
|
||||
function Confirm({ props, choice }: { props: Props; choice: Choice }) {
|
||||
const form = useForm({
|
||||
function Confirm({
|
||||
props,
|
||||
choice,
|
||||
identity,
|
||||
}: {
|
||||
props: Props;
|
||||
choice: Choice;
|
||||
identity: string;
|
||||
}) {
|
||||
const initial = {
|
||||
operation_key: crypto.randomUUID(),
|
||||
record_key: props.record.record_key,
|
||||
version: props.record.version,
|
||||
@@ -25,13 +35,29 @@ function Confirm({ props, choice }: { props: Props; choice: Choice }) {
|
||||
? { event_on: props.today ?? '', evidence: '' }
|
||||
: {}),
|
||||
},
|
||||
});
|
||||
};
|
||||
const form = useForm(identity, initial);
|
||||
const dirty = hasPreparationChanges(initial, form.data);
|
||||
const ref = usePreparationDirty(dirty);
|
||||
return (
|
||||
<form
|
||||
ref={ref}
|
||||
data-preparation-dirty={dirty ? 'true' : 'false'}
|
||||
className="mt-3 grid gap-3 md:grid-cols-2"
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault();
|
||||
form.post(props.route);
|
||||
form.post(props.route, {
|
||||
onSuccess: () => {
|
||||
const next = {
|
||||
...initial,
|
||||
operation_key: crypto.randomUUID(),
|
||||
};
|
||||
form.setData(next);
|
||||
form.clearErrors();
|
||||
router.remember(next, `${identity}:data`);
|
||||
router.remember({}, `${identity}:errors`);
|
||||
},
|
||||
});
|
||||
}}
|
||||
>
|
||||
<FormErrors errors={form.errors} />
|
||||
@@ -88,10 +114,23 @@ function Confirm({ props, choice }: { props: Props; choice: Choice }) {
|
||||
}
|
||||
|
||||
export function InventoryEventActions(props: Props) {
|
||||
const [selected, setSelected] = useState(
|
||||
props.choices.length === 1 ? props.choices[0].value : '',
|
||||
const data = JSON.stringify(
|
||||
Object.entries(props.data ?? {}).sort(([a], [b]) => a.localeCompare(b)),
|
||||
);
|
||||
const identity = useInventoryPreparationIdentity(
|
||||
`${props.route}:${props.record.record_key}:${props.record.version}:${props.choices
|
||||
.map((choice) => choice.value)
|
||||
.sort()
|
||||
.join(',')}:${data}`,
|
||||
);
|
||||
if (props.choices.length === 0) return null;
|
||||
return <EventFields key={identity} props={props} identity={identity} />;
|
||||
}
|
||||
function EventFields({ props, identity }: { props: Props; identity: string }) {
|
||||
const [selected, setSelected] = useRemember(
|
||||
props.choices.length === 1 ? props.choices[0].value : '',
|
||||
`${identity}:action`,
|
||||
);
|
||||
const choice = props.choices.find((option) => option.value === selected);
|
||||
return (
|
||||
<details className="mt-4">
|
||||
@@ -118,7 +157,12 @@ export function InventoryEventActions(props: Props) {
|
||||
</label>
|
||||
)}
|
||||
{choice && (
|
||||
<Confirm key={choice.value} props={props} choice={choice} />
|
||||
<Confirm
|
||||
key={choice.value}
|
||||
props={props}
|
||||
choice={choice}
|
||||
identity={`${identity}:${choice.value}`}
|
||||
/>
|
||||
)}
|
||||
</details>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user