mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-20 14:50:32 +00:00
feat(webapp): tax rate form validation errors
This commit is contained in:
@@ -6,17 +6,24 @@ import { AppToaster } from '@/components';
|
|||||||
|
|
||||||
import TaxRateFormDialogFormContent from './TaxRateFormDialogFormContent';
|
import TaxRateFormDialogFormContent from './TaxRateFormDialogFormContent';
|
||||||
|
|
||||||
import withDialogActions from '@/containers/Dialog/withDialogActions';
|
|
||||||
import {
|
import {
|
||||||
CreateTaxRateFormSchema,
|
CreateTaxRateFormSchema,
|
||||||
EditTaxRateFormSchema,
|
EditTaxRateFormSchema,
|
||||||
} from './TaxRateForm.schema';
|
} from './TaxRateForm.schema';
|
||||||
import { transformApiErrors, transformFormToReq, transformTaxRateToForm } from './utils';
|
import {
|
||||||
|
isTaxRateChange,
|
||||||
|
transformApiErrors,
|
||||||
|
transformFormToReq,
|
||||||
|
transformTaxRateToForm,
|
||||||
|
} from './utils';
|
||||||
import { useCreateTaxRate, useEditTaxRate } from '@/hooks/query/taxRates';
|
import { useCreateTaxRate, useEditTaxRate } from '@/hooks/query/taxRates';
|
||||||
import { useTaxRateFormDialogContext } from './TaxRateFormDialogBoot';
|
import { useTaxRateFormDialogContext } from './TaxRateFormDialogBoot';
|
||||||
import { TaxRateFormDialogFormFooter } from './TaxRateFormDialogFormFooter';
|
import { TaxRateFormDialogFormFooter } from './TaxRateFormDialogFormFooter';
|
||||||
import { compose, transformToForm } from '@/utils';
|
import { TaxRateFormDialogFormErrors } from './TaxRateFormDialogFormErrors';
|
||||||
|
import withDrawerActions from '@/containers/Drawer/withDrawerActions';
|
||||||
|
import withDialogActions from '@/containers/Dialog/withDialogActions';
|
||||||
|
import { DRAWERS } from '@/constants/drawers';
|
||||||
|
import { compose } from '@/utils';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tax rate form dialog content.
|
* Tax rate form dialog content.
|
||||||
@@ -24,6 +31,9 @@ import { compose, transformToForm } from '@/utils';
|
|||||||
function TaxRateFormDialogForm({
|
function TaxRateFormDialogForm({
|
||||||
// #withDialogActions
|
// #withDialogActions
|
||||||
closeDialog,
|
closeDialog,
|
||||||
|
|
||||||
|
// #withDrawerActions
|
||||||
|
closeDrawer,
|
||||||
}) {
|
}) {
|
||||||
// Account form context.
|
// Account form context.
|
||||||
const { taxRate, taxRateId, isNewMode, dialogName } =
|
const { taxRate, taxRateId, isNewMode, dialogName } =
|
||||||
@@ -37,14 +47,34 @@ function TaxRateFormDialogForm({
|
|||||||
const { mutateAsync: createTaxRateMutate } = useCreateTaxRate();
|
const { mutateAsync: createTaxRateMutate } = useCreateTaxRate();
|
||||||
const { mutateAsync: editTaxRateMutate } = useEditTaxRate();
|
const { mutateAsync: editTaxRateMutate } = useEditTaxRate();
|
||||||
|
|
||||||
|
// Form initial values in create and edit mode.
|
||||||
|
const initialValues = transformTaxRateToForm(taxRate);
|
||||||
|
|
||||||
// Callbacks handles form submit.
|
// Callbacks handles form submit.
|
||||||
const handleFormSubmit = (values, { setSubmitting, setErrors }) => {
|
const handleFormSubmit = (values, { setSubmitting, setErrors }) => {
|
||||||
|
const isTaxChanged = isTaxRateChange(initialValues, values);
|
||||||
|
|
||||||
|
// Detarmines whether in edit mode and tax rate has been changed
|
||||||
|
// and confirm box is not checked.
|
||||||
|
if (!isNewMode && isTaxChanged && !values.confirm_edit) {
|
||||||
|
setErrors({
|
||||||
|
confirm_edit:
|
||||||
|
'Please review the terms and conditions below before proceeding',
|
||||||
|
});
|
||||||
|
setSubmitting(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
const form = transformFormToReq(values);
|
const form = transformFormToReq(values);
|
||||||
|
|
||||||
|
// Handle request success on edit.
|
||||||
|
const handleSuccessOnEdit = (response) => {
|
||||||
|
if (response?.data?.data?.id !== taxRateId) {
|
||||||
|
closeDrawer(DRAWERS.TAX_RATE_DETAILS);
|
||||||
|
}
|
||||||
|
};
|
||||||
// Handle request success.
|
// Handle request success.
|
||||||
const handleSuccess = () => {
|
const handleSuccess = () => {
|
||||||
closeDialog(dialogName);
|
closeDialog(dialogName);
|
||||||
|
|
||||||
AppToaster.show({
|
AppToaster.show({
|
||||||
message: 'The tax rate has been created successfully.',
|
message: 'The tax rate has been created successfully.',
|
||||||
intent: Intent.SUCCESS,
|
intent: Intent.SUCCESS,
|
||||||
@@ -68,12 +98,11 @@ function TaxRateFormDialogForm({
|
|||||||
.catch(handleError);
|
.catch(handleError);
|
||||||
} else {
|
} else {
|
||||||
editTaxRateMutate([taxRateId, { ...form }])
|
editTaxRateMutate([taxRateId, { ...form }])
|
||||||
|
.then(handleSuccessOnEdit)
|
||||||
.then(handleSuccess)
|
.then(handleSuccess)
|
||||||
.catch(handleError);
|
.catch(handleError);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
// Form initial values in create and edit mode.
|
|
||||||
const initialValues = transformTaxRateToForm(taxRate);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Formik
|
<Formik
|
||||||
@@ -83,6 +112,7 @@ function TaxRateFormDialogForm({
|
|||||||
>
|
>
|
||||||
<Form>
|
<Form>
|
||||||
<div className={Classes.DIALOG_BODY}>
|
<div className={Classes.DIALOG_BODY}>
|
||||||
|
<TaxRateFormDialogFormErrors />
|
||||||
<TaxRateFormDialogFormContent />
|
<TaxRateFormDialogFormContent />
|
||||||
</div>
|
</div>
|
||||||
<TaxRateFormDialogFormFooter />
|
<TaxRateFormDialogFormFooter />
|
||||||
@@ -91,4 +121,7 @@ function TaxRateFormDialogForm({
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export default compose(withDialogActions)(TaxRateFormDialogForm);
|
export default compose(
|
||||||
|
withDialogActions,
|
||||||
|
withDrawerActions,
|
||||||
|
)(TaxRateFormDialogForm);
|
||||||
|
|||||||
@@ -108,7 +108,7 @@ function ConfirmEditingTaxRate() {
|
|||||||
return (
|
return (
|
||||||
<EditWarningWrap>
|
<EditWarningWrap>
|
||||||
<Text color={'#766f58'}>Please Note:</Text>
|
<Text color={'#766f58'}>Please Note:</Text>
|
||||||
<ConfirmEditFormGroup name={'confirm_edit'}>
|
<ConfirmEditFormGroup name={'confirm_edit'} helperText={''}>
|
||||||
<FCheckbox
|
<FCheckbox
|
||||||
name={'confirm_edit'}
|
name={'confirm_edit'}
|
||||||
label={`I understand that updating the
|
label={`I understand that updating the
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
// @ts-nocheck
|
||||||
|
import React from 'react';
|
||||||
|
import { Alert } from '@/components';
|
||||||
|
import { Intent } from '@blueprintjs/core';
|
||||||
|
import { useFormikContext } from 'formik';
|
||||||
|
|
||||||
|
export function TaxRateFormDialogFormErrors() {
|
||||||
|
const { errors } = useFormikContext();
|
||||||
|
|
||||||
|
if (!errors.confirm_edit) return null;
|
||||||
|
|
||||||
|
return <Alert intent={Intent.DANGER}>{errors.confirm_edit}</Alert>;
|
||||||
|
}
|
||||||
@@ -15,18 +15,44 @@ export const defaultInitialValues = {
|
|||||||
confirm_edit: false,
|
confirm_edit: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
export const transformApiErrors = () => {
|
/**
|
||||||
return {};
|
* Transformers response errors to form errors.
|
||||||
|
* @returns {Record<string, string>}
|
||||||
|
*/
|
||||||
|
export const transformApiErrors = (errors) => {
|
||||||
|
const fields = {};
|
||||||
|
|
||||||
|
if (errors.find((e) => e.type === 'TAX_CODE_NOT_UNIQUE')) {
|
||||||
|
fields.code = 'The tax rate is not unique.';
|
||||||
|
}
|
||||||
|
return fields;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tranformes form values to request values.
|
||||||
|
*/
|
||||||
export const transformFormToReq = (form) => {
|
export const transformFormToReq = (form) => {
|
||||||
return omit({ ...form }, ['confirm_edit']);
|
return omit({ ...form }, ['confirm_edit']);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Detarmines whether the tax rate changed.
|
||||||
|
* @param initialValues
|
||||||
|
* @param formValues
|
||||||
|
* @returns {boolean}
|
||||||
|
*/
|
||||||
|
export const isTaxRateChange = (initialValues, formValues) => {
|
||||||
|
return initialValues.rate !== formValues.rate;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Detarmines whether the tax rate changed.
|
||||||
|
* @returns {boolean}
|
||||||
|
*/
|
||||||
export const useIsTaxRateChanged = () => {
|
export const useIsTaxRateChanged = () => {
|
||||||
const { initialValues, values } = useFormikContext();
|
const { initialValues, values } = useFormikContext();
|
||||||
|
|
||||||
return initialValues.rate !== values.rate;
|
return isTaxRateChange(initialValues, values);
|
||||||
};
|
};
|
||||||
|
|
||||||
const convertFormAttrsToBoolean = (form) => {
|
const convertFormAttrsToBoolean = (form) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user