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