mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 21:00:31 +00:00
feat(webapp): edit bank rule
This commit is contained in:
@@ -7,6 +7,8 @@ interface RuleFormBootValues {
|
||||
bankRule?: null;
|
||||
bankRuleId?: null;
|
||||
isBankRuleLoading: boolean;
|
||||
isEditMode: boolean;
|
||||
isNewMode: boolean;
|
||||
}
|
||||
|
||||
const RuleFormBootContext = createContext<RuleFormBootValues>(
|
||||
@@ -27,11 +29,17 @@ function RuleFormBoot({ bankRuleId, ...props }: RuleFormBootProps) {
|
||||
);
|
||||
const { data: accounts, isLoading: isAccountsLoading } = useAccounts({}, {});
|
||||
|
||||
const isNewMode = !bankRuleId;
|
||||
const isEditMode = !isNewMode;
|
||||
|
||||
const provider = {
|
||||
bankRuleId,
|
||||
bankRule,
|
||||
accounts,
|
||||
isBankRuleLoading,
|
||||
isAccountsLoading,
|
||||
isEditMode,
|
||||
isNewMode,
|
||||
} as RuleFormBootValues;
|
||||
|
||||
const isLoading = isBankRuleLoading || isAccountsLoading;
|
||||
|
||||
@@ -14,7 +14,7 @@ import {
|
||||
Group,
|
||||
Stack,
|
||||
} from '@/components';
|
||||
import { useCreateBankRule } from '@/hooks/query/bank-rules';
|
||||
import { useCreateBankRule, useEditBankRule } from '@/hooks/query/bank-rules';
|
||||
import {
|
||||
AssignTransactionTypeOptions,
|
||||
FieldCondition,
|
||||
@@ -24,7 +24,11 @@ import {
|
||||
initialValues,
|
||||
} from './_utils';
|
||||
import { useRuleFormDialogBoot } from './RuleFormBoot';
|
||||
import { transfromToSnakeCase } from '@/utils';
|
||||
import {
|
||||
transformToCamelCase,
|
||||
transformToForm,
|
||||
transfromToSnakeCase,
|
||||
} from '@/utils';
|
||||
import withDialogActions from '@/containers/Dialog/withDialogActions';
|
||||
import { DialogsName } from '@/constants/dialogs';
|
||||
|
||||
@@ -33,42 +37,53 @@ function RuleFormContentFormRoot({
|
||||
openDialog,
|
||||
closeDialog,
|
||||
}) {
|
||||
const { accounts } = useRuleFormDialogBoot();
|
||||
const { accounts, bankRule, isEditMode, bankRuleId } =
|
||||
useRuleFormDialogBoot();
|
||||
const { mutateAsync: createBankRule } = useCreateBankRule();
|
||||
const { mutateAsync: editBankRule } = useEditBankRule();
|
||||
|
||||
const validationSchema = CreateRuleFormSchema;
|
||||
|
||||
const _initialValues = {
|
||||
...initialValues,
|
||||
...transformToForm(transformToCamelCase(bankRule), initialValues),
|
||||
};
|
||||
|
||||
// Handles the form submitting.
|
||||
const handleSubmit = (
|
||||
values: RuleFormValues,
|
||||
{ setSubmitting }: FormikHelpers<RuleFormValues>,
|
||||
) => {
|
||||
const _value = transfromToSnakeCase(values);
|
||||
|
||||
const _values = transfromToSnakeCase(values);
|
||||
setSubmitting(true);
|
||||
createBankRule(_value)
|
||||
.then(() => {
|
||||
setSubmitting(false);
|
||||
closeDialog(DialogsName.BankRuleForm);
|
||||
|
||||
AppToaster.show({
|
||||
intent: Intent.SUCCESS,
|
||||
message: 'The bank rule has been created successfully.',
|
||||
});
|
||||
})
|
||||
.catch((error) => {
|
||||
setSubmitting(false);
|
||||
|
||||
AppToaster.show({
|
||||
intent: Intent.DANGER,
|
||||
message: 'Something went wrong!',
|
||||
});
|
||||
const handleSuccess = () => {
|
||||
setSubmitting(false);
|
||||
closeDialog(DialogsName.BankRuleForm);
|
||||
AppToaster.show({
|
||||
intent: Intent.SUCCESS,
|
||||
message: 'The bank rule has been created successfully.',
|
||||
});
|
||||
};
|
||||
const handleError = (error) => {
|
||||
setSubmitting(false);
|
||||
AppToaster.show({
|
||||
intent: Intent.DANGER,
|
||||
message: 'Something went wrong!',
|
||||
});
|
||||
};
|
||||
if (isEditMode) {
|
||||
editBankRule([bankRuleId, _values])
|
||||
.then(handleSuccess)
|
||||
.catch(handleError);
|
||||
} else {
|
||||
createBankRule(_values).then(handleSuccess).catch(handleError);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<Formik<RuleFormValues>
|
||||
initialValues={initialValues}
|
||||
initialValues={_initialValues}
|
||||
validationSchema={validationSchema}
|
||||
onSubmit={handleSubmit}
|
||||
>
|
||||
|
||||
@@ -16,8 +16,8 @@ function RuleFormDialogRoot({
|
||||
}) {
|
||||
return (
|
||||
<Dialog
|
||||
name={dialogName}f
|
||||
title={'New Bank Rule'}
|
||||
name={dialogName}
|
||||
title={bankRuleId ? 'Edit Bank Rule' : 'New Bank Rule'}
|
||||
isOpen={isOpen}
|
||||
canEscapeJeyClose={true}
|
||||
autoFocus={true}
|
||||
|
||||
@@ -16,6 +16,7 @@ import { useBankRulesTableColumns } from './hooks';
|
||||
import { BankRulesTableActionsMenu } from './_components';
|
||||
import { BankRulesLandingEmptyState } from './BankRulesLandingEmptyState';
|
||||
import { useRulesListBoot } from './RulesListBoot';
|
||||
import { DialogsName } from '@/constants/dialogs';
|
||||
|
||||
/**
|
||||
* Invoices datatable.
|
||||
@@ -41,7 +42,9 @@ function RulesTable({
|
||||
};
|
||||
|
||||
// Handle delete bank rule.
|
||||
const handleEditBankRule = () => {};
|
||||
const handleEditBankRule = ({ id }) => {
|
||||
openDialog(DialogsName.BankRuleForm, { bankRuleId: id });
|
||||
};
|
||||
|
||||
const isEmptyState = false;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user