@@ -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';
|
||||
|
||||
@@ -131,26 +133,52 @@ function Fields({
|
||||
);
|
||||
}
|
||||
function ResponseCard({
|
||||
scope,
|
||||
...props
|
||||
}: Omit<Parameters<typeof ResponseFields>[0], 'identity'> & { scope: string }) {
|
||||
const r = props.record;
|
||||
const identity = `${scope}:record:${r.id}:${r.version}:${r.status}:${r.canEdit}:${r.canReview}`;
|
||||
return <ResponseFields key={identity} {...props} identity={identity} />;
|
||||
}
|
||||
function ResponseFields({
|
||||
record: r,
|
||||
base,
|
||||
names,
|
||||
identity,
|
||||
}: {
|
||||
identity: string;
|
||||
record: Record;
|
||||
base: string;
|
||||
names: { [key: number]: string };
|
||||
}) {
|
||||
const edit = useForm({ version: r.version, payload: r.payload });
|
||||
const submit = useForm({ version: r.version });
|
||||
const review = useForm({
|
||||
const initialEdit = { version: r.version, payload: r.payload };
|
||||
const edit = useForm(`${identity}:edit`, initialEdit);
|
||||
const submit = useForm(`${identity}:submit`, { version: r.version });
|
||||
const initialReview = {
|
||||
version: r.version,
|
||||
outcome: '',
|
||||
reason: '',
|
||||
follow_up: '',
|
||||
response_reference: '',
|
||||
});
|
||||
};
|
||||
const review = useForm(`${identity}:review`, initialReview);
|
||||
const clearEdit = () => {
|
||||
edit.setData(initialEdit);
|
||||
edit.clearErrors();
|
||||
router.remember(initialEdit, `${identity}:edit:data`);
|
||||
router.remember({}, `${identity}:edit:errors`);
|
||||
};
|
||||
const clearReview = () => {
|
||||
review.setData(initialReview);
|
||||
review.clearErrors();
|
||||
router.remember(initialReview, `${identity}:review:data`);
|
||||
router.remember({}, `${identity}:review:errors`);
|
||||
};
|
||||
const url = `${base}/${r.id}`;
|
||||
const dirty =
|
||||
JSON.stringify(edit.data.payload) !== JSON.stringify(r.payload);
|
||||
const dirty = hasPreparationChanges(initialEdit, edit.data);
|
||||
const editRef = usePreparationDirty(dirty, r.canEdit);
|
||||
const reviewDirty = hasPreparationChanges(initialReview, review.data);
|
||||
const reviewRef = usePreparationDirty(reviewDirty, r.canReview);
|
||||
return (
|
||||
<article
|
||||
id={`contraditorio-${r.id}`}
|
||||
@@ -165,12 +193,15 @@ function ResponseCard({
|
||||
</p>
|
||||
{r.canEdit ? (
|
||||
<form
|
||||
ref={editRef}
|
||||
data-preparation-dirty={dirty ? 'true' : 'false'}
|
||||
className="space-y-4 print:hidden"
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault();
|
||||
edit.post(url + '/save', {
|
||||
preserveState: 'errors',
|
||||
preserveState: true,
|
||||
preserveScroll: true,
|
||||
onSuccess: clearEdit,
|
||||
});
|
||||
}}
|
||||
>
|
||||
@@ -196,7 +227,7 @@ function ResponseCard({
|
||||
}
|
||||
onClick={() =>
|
||||
submit.post(url + '/submit', {
|
||||
preserveState: 'errors',
|
||||
preserveState: true,
|
||||
preserveScroll: true,
|
||||
})
|
||||
}
|
||||
@@ -253,7 +284,12 @@ function ResponseCard({
|
||||
</div>
|
||||
)}
|
||||
{r.canReview && (
|
||||
<div className="space-y-4 print:hidden">
|
||||
<form
|
||||
ref={reviewRef}
|
||||
data-preparation-dirty={reviewDirty ? 'true' : 'false'}
|
||||
onSubmit={(event) => event.preventDefault()}
|
||||
className="space-y-4 print:hidden"
|
||||
>
|
||||
<Errors errors={review.errors} />
|
||||
<label className="block space-y-1">
|
||||
Conclusão
|
||||
@@ -312,6 +348,7 @@ function ResponseCard({
|
||||
</label>
|
||||
<div className="flex flex-wrap gap-3">
|
||||
<Button
|
||||
type="button"
|
||||
disabled={
|
||||
review.processing ||
|
||||
!review.data.outcome ||
|
||||
@@ -320,14 +357,16 @@ function ResponseCard({
|
||||
onClick={() => {
|
||||
review.transform((values) => values);
|
||||
review.post(url + '/decide', {
|
||||
preserveState: 'errors',
|
||||
preserveState: true,
|
||||
preserveScroll: true,
|
||||
onSuccess: clearReview,
|
||||
});
|
||||
}}
|
||||
>
|
||||
Registar conclusão da análise
|
||||
</Button>
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
disabled={
|
||||
review.processing ||
|
||||
@@ -339,34 +378,63 @@ function ResponseCard({
|
||||
reason: values.reason,
|
||||
}));
|
||||
review.post(url + '/return', {
|
||||
preserveState: 'errors',
|
||||
preserveState: true,
|
||||
preserveScroll: true,
|
||||
onSuccess: clearReview,
|
||||
});
|
||||
}}
|
||||
>
|
||||
Devolver para completar
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
)}
|
||||
</article>
|
||||
);
|
||||
}
|
||||
export default function SupplierResponses({
|
||||
export default function SupplierResponses(
|
||||
props: Omit<Parameters<typeof ResponseContent>[0], 'identity'>,
|
||||
) {
|
||||
const identity = useProcurementPreparationIdentity(
|
||||
`supplier-responses:${props.evaluationId}:${props.data.ready}:${props.data.canCreate}:${props.reportOnly}`,
|
||||
);
|
||||
return <ResponseContent key={identity} {...props} identity={identity} />;
|
||||
}
|
||||
function ResponseContent({
|
||||
data,
|
||||
evaluationId,
|
||||
names,
|
||||
reportOnly,
|
||||
generatedAt,
|
||||
identity,
|
||||
}: {
|
||||
identity: string;
|
||||
data: ResponseData;
|
||||
evaluationId: number;
|
||||
names: { [key: number]: string };
|
||||
reportOnly: boolean;
|
||||
generatedAt: string;
|
||||
}) {
|
||||
const create = useForm({ payload: empty });
|
||||
const operation = useRef<string | null>(null);
|
||||
const initialCreate = { payload: empty };
|
||||
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}/contraditorios`;
|
||||
return (
|
||||
<Panel title="Contraditório do fornecedor">
|
||||
@@ -392,6 +460,7 @@ export default function SupplierResponses({
|
||||
)}
|
||||
{data.records.map((r) => (
|
||||
<ResponseCard
|
||||
scope={identity}
|
||||
key={`${r.id}-${r.version}`}
|
||||
record={r}
|
||||
base={base}
|
||||
@@ -404,18 +473,21 @@ export default function SupplierResponses({
|
||||
Registar documento recebido
|
||||
</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