@@ -1,5 +1,13 @@
|
||||
import { Head, Link, useForm } from '@inertiajs/react';
|
||||
import { useState } from 'react';
|
||||
import {
|
||||
Head,
|
||||
Link,
|
||||
router,
|
||||
useForm,
|
||||
usePage,
|
||||
useRemember,
|
||||
} from '@inertiajs/react';
|
||||
import { usePreparationDirty } from '@/hooks/use-preparation-dirty';
|
||||
import { hasPreparationChanges } from '@/lib/requisition-history';
|
||||
import {
|
||||
FormErrors,
|
||||
Panel,
|
||||
@@ -51,9 +59,10 @@ type Props = {
|
||||
canReview: boolean;
|
||||
};
|
||||
export default function BankPage(props: Props) {
|
||||
const actor = usePage().props.auth?.user;
|
||||
return (
|
||||
<Bank
|
||||
key={`${props.supplier.id}:${props.accountKey ?? 'legacy'}:${props.actorId ?? 0}:${props.canPropose}:${props.canReview}`}
|
||||
key={`${actor?.tenant_id}:${props.supplier.id}:${props.accountKey ?? 'legacy'}:${props.actorId ?? actor?.id ?? 0}:${props.currentId}:${props.state}:${props.canPropose}:${props.canReview}`}
|
||||
{...props}
|
||||
/>
|
||||
);
|
||||
@@ -71,9 +80,15 @@ function Bank({
|
||||
accountKey = null,
|
||||
accounts = [],
|
||||
multiAccountsAvailable = false,
|
||||
actorId,
|
||||
}: Props) {
|
||||
const [addingAccount, setAddingAccount] = useState(false);
|
||||
const form = useForm({
|
||||
const actor = usePage().props.auth?.user;
|
||||
const identity = `supplier-bank:${actor?.tenant_id}:${actorId ?? actor?.id ?? 0}:${supplier.id}:${accountKey ?? 'legacy'}:${currentId}:${canPropose}:${canReview}`;
|
||||
const [addingAccount, setAddingAccount] = useRemember(
|
||||
false,
|
||||
`${identity}:adding`,
|
||||
);
|
||||
const initial = {
|
||||
previous_id: currentId,
|
||||
account_key: accountKey,
|
||||
bank_name: '',
|
||||
@@ -83,12 +98,29 @@ function Bank({
|
||||
account_identifier: '',
|
||||
evidence_reference: '',
|
||||
reason: '',
|
||||
});
|
||||
const review = useForm({
|
||||
};
|
||||
const form = useForm(`${identity}:proposal`, initial);
|
||||
const initialReview = {
|
||||
decision: 'approved',
|
||||
reason: '',
|
||||
independently_verified: false,
|
||||
});
|
||||
};
|
||||
const review = useForm(`${identity}:review`, initialReview);
|
||||
const dirty = Object.values({
|
||||
bank_name: form.data.bank_name,
|
||||
country: form.data.country,
|
||||
currency: form.data.currency,
|
||||
beneficiary: form.data.beneficiary,
|
||||
account_identifier: form.data.account_identifier,
|
||||
evidence_reference: form.data.evidence_reference,
|
||||
reason: form.data.reason,
|
||||
}).some((value) => value !== '');
|
||||
const proposalRef = usePreparationDirty(dirty, canPropose);
|
||||
const reviewDirty = hasPreparationChanges(initialReview, review.data);
|
||||
const reviewRef = usePreparationDirty(
|
||||
reviewDirty,
|
||||
canReview && current !== null && state === 'pending',
|
||||
);
|
||||
const labels = {
|
||||
bank_name: 'Banco',
|
||||
country: 'Código do país (2 letras)',
|
||||
@@ -189,11 +221,25 @@ function Bank({
|
||||
{canReview && current && state === 'pending' && (
|
||||
<Panel title="Conferência independente">
|
||||
<form
|
||||
ref={reviewRef}
|
||||
data-preparation-dirty={
|
||||
reviewDirty ? 'true' : 'false'
|
||||
}
|
||||
className="grid gap-4"
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault();
|
||||
review.post(
|
||||
`/fornecedores/${supplier.id}/dados-bancarios/${current.id}/validar`,
|
||||
{
|
||||
onSuccess: () => {
|
||||
review.reset();
|
||||
review.clearErrors();
|
||||
router.remember(
|
||||
initialReview,
|
||||
`${identity}:review:data`,
|
||||
);
|
||||
},
|
||||
},
|
||||
);
|
||||
}}
|
||||
>
|
||||
@@ -256,18 +302,26 @@ function Bank({
|
||||
description="Preencha integralmente os dados confirmados. A nova versão exige duas validações novas; a versão anterior permanece no histórico."
|
||||
>
|
||||
<form
|
||||
ref={proposalRef}
|
||||
data-preparation-dirty={dirty ? 'true' : 'false'}
|
||||
className="grid gap-4"
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault();
|
||||
form.post(
|
||||
`/fornecedores/${supplier.id}/dados-bancarios`,
|
||||
{
|
||||
onSuccess: (page) => {
|
||||
onSuccess: () => {
|
||||
form.reset();
|
||||
form.setData(
|
||||
'previous_id',
|
||||
Number(page.props.currentId),
|
||||
form.clearErrors();
|
||||
router.remember(
|
||||
initial,
|
||||
`${identity}:proposal:data`,
|
||||
);
|
||||
router.remember(
|
||||
false,
|
||||
`${identity}:adding`,
|
||||
);
|
||||
setAddingAccount(false);
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user