@@ -1,4 +1,7 @@
|
||||
import { Head, useForm, usePage } from '@inertiajs/react';
|
||||
import { usePreparationDirty } from '@/hooks/use-preparation-dirty';
|
||||
import { hasPreparationChanges } from '@/lib/requisition-history';
|
||||
import type { WorkContextState } from '@/types/work-context';
|
||||
import { Head, router, useForm, usePage } from '@inertiajs/react';
|
||||
import {
|
||||
FormErrors,
|
||||
Panel,
|
||||
@@ -30,8 +33,31 @@ type Review = {
|
||||
decision: string;
|
||||
reason: string;
|
||||
};
|
||||
function useInvestmentIdentity() {
|
||||
const { auth, workContext, canManage, organizationAccess } =
|
||||
usePage().props;
|
||||
const context = workContext as WorkContextState | null | undefined;
|
||||
const access = organizationAccess as { permissions?: string[] } | null;
|
||||
return `investment:${auth.user?.tenant_id}:${auth.user?.id}:${context?.active ?? 'legacy'}:${canManage === true}:${[...(access?.permissions ?? [])].sort().join(',')}`;
|
||||
}
|
||||
function Proposal({ previousId }: { previousId: number }) {
|
||||
const form = useForm({
|
||||
const identity = `${useInvestmentIdentity()}:proposal:${previousId}`;
|
||||
return (
|
||||
<ProposalFields
|
||||
key={identity}
|
||||
previousId={previousId}
|
||||
identity={identity}
|
||||
/>
|
||||
);
|
||||
}
|
||||
function ProposalFields({
|
||||
previousId,
|
||||
identity,
|
||||
}: {
|
||||
previousId: number;
|
||||
identity: string;
|
||||
}) {
|
||||
const initial = {
|
||||
previous_id: previousId,
|
||||
mode: '',
|
||||
scope: '',
|
||||
@@ -41,13 +67,18 @@ function Proposal({ previousId }: { previousId: number }) {
|
||||
valid_from: '',
|
||||
valid_until: '',
|
||||
reason: '',
|
||||
});
|
||||
};
|
||||
const form = useForm(identity, initial);
|
||||
const dirty = hasPreparationChanges(initial, form.data);
|
||||
const ref = usePreparationDirty(dirty);
|
||||
return (
|
||||
<Panel
|
||||
title="Propor nova versão"
|
||||
description="São necessárias duas aprovações de administradores distintos do proponente. Valores e vigência são decisões da empresa."
|
||||
>
|
||||
<form
|
||||
ref={ref}
|
||||
data-preparation-dirty={dirty ? 'true' : 'false'}
|
||||
className="grid gap-4"
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault();
|
||||
@@ -55,7 +86,14 @@ function Proposal({ previousId }: { previousId: number }) {
|
||||
...d,
|
||||
includes_tax: d.includes_tax === '1',
|
||||
}));
|
||||
form.post('/regras-investimento');
|
||||
form.post('/regras-investimento', {
|
||||
onSuccess: () => {
|
||||
form.setData(initial);
|
||||
form.clearErrors();
|
||||
router.remember(initial, `${identity}:data`);
|
||||
router.remember({}, `${identity}:errors`);
|
||||
},
|
||||
});
|
||||
}}
|
||||
>
|
||||
<FormErrors errors={form.errors} />
|
||||
@@ -172,13 +210,29 @@ function Proposal({ previousId }: { previousId: number }) {
|
||||
);
|
||||
}
|
||||
function Decision({ id }: { id: number }) {
|
||||
const form = useForm({ decision: 'approved', reason: '' });
|
||||
const identity = `${useInvestmentIdentity()}:review:${id}`;
|
||||
return <DecisionFields key={identity} id={id} identity={identity} />;
|
||||
}
|
||||
function DecisionFields({ id, identity }: { id: number; identity: string }) {
|
||||
const initial = { decision: 'approved', reason: '' };
|
||||
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-4 grid gap-3"
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault();
|
||||
form.post(`/regras-investimento/${id}/decidir`);
|
||||
form.post(`/regras-investimento/${id}/decidir`, {
|
||||
onSuccess: () => {
|
||||
form.setData(initial);
|
||||
form.clearErrors();
|
||||
router.remember(initial, `${identity}:data`);
|
||||
router.remember({}, `${identity}:errors`);
|
||||
},
|
||||
});
|
||||
}}
|
||||
>
|
||||
<FormErrors errors={form.errors} />
|
||||
|
||||
Reference in New Issue
Block a user