Files
compras/resources/js/components/inventory-event-actions.tsx
T
nala eb32c74c56
tests / ci (push) Waiting to run
commit compras1 2811
2026-11-28 07:11:35 +02:00

170 lines
6.3 KiB
TypeScript

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';
type Choice = { value: string; label: string };
type Props = {
route: string;
record: { record_key: string; version: number };
choices: Choice[];
today?: string;
data?: Record<string, string | number | boolean | null>;
};
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,
action: choice.value,
reason: '',
data: {
...props.data,
...(choice.value === 'execute'
? { 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, {
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} />
<label className="grid gap-1 text-sm">
Motivo e evidência da conferência
<textarea
required
minLength={10}
maxLength={4000}
className={controlClass}
value={form.data.reason}
onChange={(e) => form.setData('reason', e.target.value)}
/>
</label>
{choice.value === 'execute' && (
<>
<label className="grid gap-1 text-sm">
Data efectiva do evento
<input
type="date"
required
max={props.today}
className={controlClass}
value={String(form.data.data.event_on ?? '')}
onChange={(e) =>
form.setData('data', {
...form.data.data,
event_on: e.target.value,
})
}
/>
</label>
<label className="grid gap-1 text-sm">
Comprovativo da execução física
<input
required
minLength={10}
maxLength={3000}
className={controlClass}
value={String(form.data.data.evidence ?? '')}
onChange={(e) =>
form.setData('data', {
...form.data.data,
evidence: e.target.value,
})
}
/>
</label>
</>
)}
<Button disabled={form.processing}>{choice.label}</Button>
</form>
);
}
export function InventoryEventActions(props: Props) {
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">
<summary className="cursor-pointer text-sm font-medium">
{props.choices.length === 1
? props.choices[0].label
: 'Acções da minha responsabilidade'}
</summary>
{props.choices.length > 1 && (
<label className="mt-3 grid gap-1 text-sm">
Acção
<select
className={controlClass}
value={selected}
onChange={(e) => setSelected(e.target.value)}
>
<option value="">Seleccione a decisão</option>
{props.choices.map((option) => (
<option key={option.value} value={option.value}>
{option.label}
</option>
))}
</select>
</label>
)}
{choice && (
<Confirm
key={choice.value}
props={props}
choice={choice}
identity={`${identity}:${choice.value}`}
/>
)}
</details>
);
}