@@ -1,5 +1,7 @@
|
||||
import { Link, useForm } from '@inertiajs/react';
|
||||
import { useRef } from 'react';
|
||||
import { useProcurementPreparationIdentity } from '@/hooks/use-procurement-preparation-identity';
|
||||
import { usePreparationDirty } from '@/hooks/use-preparation-dirty';
|
||||
import { hasPreparationChanges } from '@/lib/requisition-history';
|
||||
import { Link, router, useForm, useRemember } from '@inertiajs/react';
|
||||
import { Panel, controlClass } from '@/components/procurement';
|
||||
import { Button } from '@/components/ui/button';
|
||||
|
||||
@@ -139,32 +141,69 @@ function PlanFields({
|
||||
);
|
||||
}
|
||||
function MeasureCard({
|
||||
scope,
|
||||
...props
|
||||
}: Omit<Parameters<typeof MeasureFields>[0], 'identity'> & { scope: string }) {
|
||||
const m = props.measure;
|
||||
const identity = `${scope}:record:${m.id}:${m.version}:${m.status}:${m.canPlan}:${m.canExecute}:${m.canReview}`;
|
||||
return <MeasureFields key={identity} {...props} identity={identity} />;
|
||||
}
|
||||
function MeasureFields({
|
||||
measure: m,
|
||||
base,
|
||||
people,
|
||||
names,
|
||||
today,
|
||||
identity,
|
||||
}: {
|
||||
identity: string;
|
||||
measure: Measure;
|
||||
base: string;
|
||||
people: Person[];
|
||||
names: Record<number, string>;
|
||||
today: string;
|
||||
}) {
|
||||
const plan = useForm({
|
||||
const initialPlan = {
|
||||
version: m.version,
|
||||
title: m.title,
|
||||
description: m.description,
|
||||
responsible_id: String(m.responsible_id),
|
||||
due_on: m.due_on,
|
||||
reason: '',
|
||||
});
|
||||
const progress = useForm({
|
||||
};
|
||||
const plan = useForm(`${identity}:plan`, initialPlan);
|
||||
const initialProgress = {
|
||||
version: m.version,
|
||||
result: m.result ?? '',
|
||||
evidence: m.evidence ?? '',
|
||||
});
|
||||
const decision = useForm({ version: m.version, reason: '' });
|
||||
};
|
||||
const progress = useForm(`${identity}:progress`, initialProgress);
|
||||
const initialDecision = { version: m.version, reason: '' };
|
||||
const decision = useForm(`${identity}:decision`, initialDecision);
|
||||
const planDirty = hasPreparationChanges(initialPlan, plan.data);
|
||||
const planRef = usePreparationDirty(planDirty, m.canPlan);
|
||||
const progressDirty = hasPreparationChanges(initialProgress, progress.data);
|
||||
const progressRef = usePreparationDirty(progressDirty, m.canExecute);
|
||||
const reviewDirty = hasPreparationChanges(initialDecision, decision.data);
|
||||
const reviewRef = usePreparationDirty(reviewDirty, m.canReview);
|
||||
const clearPlan = () => {
|
||||
plan.setData(initialPlan);
|
||||
plan.clearErrors();
|
||||
router.remember(initialPlan, `${identity}:plan:data`);
|
||||
router.remember({}, `${identity}:plan:errors`);
|
||||
};
|
||||
const clearProgress = () => {
|
||||
progress.setData(initialProgress);
|
||||
progress.clearErrors();
|
||||
router.remember(initialProgress, `${identity}:progress:data`);
|
||||
router.remember({}, `${identity}:progress:errors`);
|
||||
};
|
||||
const clearDecision = () => {
|
||||
decision.setData(initialDecision);
|
||||
decision.clearErrors();
|
||||
router.remember(initialDecision, `${identity}:decision:data`);
|
||||
router.remember({}, `${identity}:decision:errors`);
|
||||
};
|
||||
const url = `${base}/${m.id}`;
|
||||
const overdue = m.status !== 'closed' && m.due_on < today;
|
||||
return (
|
||||
@@ -217,12 +256,15 @@ function MeasureCard({
|
||||
Alterar plano, responsável ou prazo
|
||||
</summary>
|
||||
<form
|
||||
ref={planRef}
|
||||
data-preparation-dirty={planDirty ? 'true' : 'false'}
|
||||
className="mt-4 space-y-4"
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault();
|
||||
plan.post(url + '/plan', {
|
||||
preserveState: 'errors',
|
||||
preserveState: true,
|
||||
preserveScroll: true,
|
||||
onSuccess: clearPlan,
|
||||
});
|
||||
}}
|
||||
>
|
||||
@@ -253,12 +295,15 @@ function MeasureCard({
|
||||
)}
|
||||
{m.canExecute && (
|
||||
<form
|
||||
ref={progressRef}
|
||||
data-preparation-dirty={progressDirty ? 'true' : 'false'}
|
||||
className="space-y-4 print:hidden"
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault();
|
||||
progress.post(url + '/progress', {
|
||||
preserveState: 'errors',
|
||||
preserveState: true,
|
||||
preserveScroll: true,
|
||||
onSuccess: clearProgress,
|
||||
});
|
||||
}}
|
||||
>
|
||||
@@ -301,8 +346,9 @@ function MeasureCard({
|
||||
}
|
||||
onClick={() =>
|
||||
progress.post(url + '/submit', {
|
||||
preserveState: 'errors',
|
||||
preserveState: true,
|
||||
preserveScroll: true,
|
||||
onSuccess: clearProgress,
|
||||
})
|
||||
}
|
||||
>
|
||||
@@ -312,7 +358,12 @@ function MeasureCard({
|
||||
</form>
|
||||
)}
|
||||
{m.canReview && (
|
||||
<div className="space-y-3 print:hidden">
|
||||
<form
|
||||
ref={reviewRef}
|
||||
data-preparation-dirty={reviewDirty ? 'true' : 'false'}
|
||||
onSubmit={(event) => event.preventDefault()}
|
||||
className="space-y-3 print:hidden"
|
||||
>
|
||||
<Errors errors={decision.errors} />
|
||||
<label className="block space-y-1">
|
||||
Fundamentação da conferência
|
||||
@@ -328,20 +379,23 @@ function MeasureCard({
|
||||
</label>
|
||||
<div className="flex flex-wrap gap-3">
|
||||
<Button
|
||||
type="button"
|
||||
disabled={
|
||||
decision.processing ||
|
||||
decision.data.reason.trim().length < 10
|
||||
}
|
||||
onClick={() =>
|
||||
decision.post(url + '/close', {
|
||||
preserveState: 'errors',
|
||||
preserveState: true,
|
||||
preserveScroll: true,
|
||||
onSuccess: clearDecision,
|
||||
})
|
||||
}
|
||||
>
|
||||
Confirmar encerramento
|
||||
</Button>
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
disabled={
|
||||
decision.processing ||
|
||||
@@ -349,34 +403,63 @@ function MeasureCard({
|
||||
}
|
||||
onClick={() =>
|
||||
decision.post(url + '/return', {
|
||||
preserveState: 'errors',
|
||||
preserveState: true,
|
||||
preserveScroll: true,
|
||||
onSuccess: clearDecision,
|
||||
})
|
||||
}
|
||||
>
|
||||
Devolver para correcção
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
)}
|
||||
</article>
|
||||
);
|
||||
}
|
||||
export default function SupplierImprovements({
|
||||
export default function SupplierImprovements(
|
||||
props: Omit<Parameters<typeof ImprovementContent>[0], 'identity'>,
|
||||
) {
|
||||
const identity = useProcurementPreparationIdentity(
|
||||
`supplier-improvements:${props.evaluationId}:${props.data.ready}:${props.data.canCreate}:${props.reportOnly}`,
|
||||
);
|
||||
return <ImprovementContent key={identity} {...props} identity={identity} />;
|
||||
}
|
||||
function ImprovementContent({
|
||||
data,
|
||||
evaluationId,
|
||||
names,
|
||||
reportOnly,
|
||||
generatedAt,
|
||||
identity,
|
||||
}: {
|
||||
identity: string;
|
||||
data: ImprovementData;
|
||||
evaluationId: number;
|
||||
names: Record<number, string>;
|
||||
reportOnly: boolean;
|
||||
generatedAt: string;
|
||||
}) {
|
||||
const create = useForm(freshPlan);
|
||||
const operation = useRef<string | null>(null);
|
||||
const initialCreate = freshPlan;
|
||||
const create = useForm(`${identity}:create`, initialCreate);
|
||||
const [operation, setOperation] = useRemember(
|
||||
crypto.randomUUID(),
|
||||
`${identity}:operation`,
|
||||
);
|
||||
const createDirty = hasPreparationChanges(initialCreate, create.data);
|
||||
const createRef = usePreparationDirty(
|
||||
createDirty,
|
||||
data.canCreate && !reportOnly,
|
||||
);
|
||||
const clearCreate = () => {
|
||||
create.setData(initialCreate);
|
||||
create.clearErrors();
|
||||
router.remember(initialCreate, `${identity}:create:data`);
|
||||
router.remember({}, `${identity}:create:errors`);
|
||||
const nextOperation = crypto.randomUUID();
|
||||
setOperation(nextOperation);
|
||||
router.remember(nextOperation, `${identity}:operation`);
|
||||
};
|
||||
const base = `/fornecedores/avaliacoes/${evaluationId}/medidas`;
|
||||
const overdue = data.records.filter(
|
||||
(m) => m.status !== 'closed' && m.due_on < data.today,
|
||||
@@ -416,6 +499,7 @@ export default function SupplierImprovements({
|
||||
)}
|
||||
{data.records.map((m) => (
|
||||
<MeasureCard
|
||||
scope={identity}
|
||||
key={`${m.id}-${m.version}`}
|
||||
measure={m}
|
||||
base={base}
|
||||
@@ -430,18 +514,21 @@ export default function SupplierImprovements({
|
||||
Adicionar medida de melhoria
|
||||
</summary>
|
||||
<form
|
||||
ref={createRef}
|
||||
data-preparation-dirty={
|
||||
createDirty ? 'true' : 'false'
|
||||
}
|
||||
className="mt-4 space-y-4"
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault();
|
||||
operation.current ??=
|
||||
crypto.randomUUID();
|
||||
create.transform((values) => ({
|
||||
...values,
|
||||
operation_key: operation.current,
|
||||
operation_key: operation,
|
||||
}));
|
||||
create.post(base, {
|
||||
preserveState: 'errors',
|
||||
preserveState: true,
|
||||
preserveScroll: true,
|
||||
onSuccess: clearCreate,
|
||||
});
|
||||
}}
|
||||
>
|
||||
|
||||
Reference in New Issue
Block a user