mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 04:40:32 +00:00
chrone: sperate client and server to different repos.
This commit is contained in:
142
src/containers/Dialogs/AccountDialog/AccountDialogForm.js
Normal file
142
src/containers/Dialogs/AccountDialog/AccountDialogForm.js
Normal file
@@ -0,0 +1,142 @@
|
||||
import React, { useCallback } from 'react';
|
||||
import { Intent } from '@blueprintjs/core';
|
||||
import { Formik } from 'formik';
|
||||
import intl from 'react-intl-universal';
|
||||
import { omit } from 'lodash';
|
||||
import { AppToaster } from 'components';
|
||||
|
||||
import AccountDialogFormContent from './AccountDialogFormContent';
|
||||
|
||||
import withDialogActions from 'containers/Dialog/withDialogActions';
|
||||
import {
|
||||
EditAccountFormSchema,
|
||||
CreateAccountFormSchema,
|
||||
} from './AccountForm.schema';
|
||||
import { compose, transformToForm } from 'utils';
|
||||
import { transformApiErrors, transformAccountToForm } from './utils';
|
||||
|
||||
import 'style/pages/Accounts/AccountFormDialog.scss';
|
||||
import { useAccountDialogContext } from './AccountDialogProvider';
|
||||
|
||||
// Default initial form values.
|
||||
const defaultInitialValues = {
|
||||
account_type: '',
|
||||
parent_account_id: '',
|
||||
name: '',
|
||||
code: '',
|
||||
description: '',
|
||||
subaccount: false,
|
||||
};
|
||||
|
||||
/**
|
||||
* Account form dialog content.
|
||||
*/
|
||||
function AccountFormDialogContent({
|
||||
// #withDialogActions
|
||||
closeDialog,
|
||||
}) {
|
||||
// Account form context.
|
||||
const {
|
||||
editAccountMutate,
|
||||
createAccountMutate,
|
||||
account,
|
||||
|
||||
accountId,
|
||||
action,
|
||||
parentAccountId,
|
||||
accountType,
|
||||
isNewMode,
|
||||
dialogName,
|
||||
} = useAccountDialogContext();
|
||||
|
||||
// Form validation schema in create and edit mode.
|
||||
const validationSchema = isNewMode
|
||||
? CreateAccountFormSchema
|
||||
: EditAccountFormSchema;
|
||||
|
||||
// Callbacks handles form submit.
|
||||
const handleFormSubmit = (values, { setSubmitting, setErrors }) => {
|
||||
const form = omit(values, ['subaccount']);
|
||||
const toastAccountName = values.code
|
||||
? `${values.code} - ${values.name}`
|
||||
: values.name;
|
||||
|
||||
// Handle request success.
|
||||
const handleSuccess = () => {
|
||||
closeDialog(dialogName);
|
||||
|
||||
AppToaster.show({
|
||||
message: intl.get(
|
||||
isNewMode
|
||||
? 'service_has_been_created_successfully'
|
||||
: 'service_has_been_edited_successfully',
|
||||
{
|
||||
name: toastAccountName,
|
||||
service: intl.get('account'),
|
||||
},
|
||||
),
|
||||
intent: Intent.SUCCESS,
|
||||
});
|
||||
};
|
||||
// Handle request error.
|
||||
const handleError = (error) => {
|
||||
const {
|
||||
response: {
|
||||
data: { errors },
|
||||
},
|
||||
} = error;
|
||||
|
||||
const errorsTransformed = transformApiErrors(errors);
|
||||
setErrors({ ...errorsTransformed });
|
||||
setSubmitting(false);
|
||||
};
|
||||
if (accountId) {
|
||||
editAccountMutate([accountId, form])
|
||||
.then(handleSuccess)
|
||||
.catch(handleError);
|
||||
} else {
|
||||
createAccountMutate({ ...form })
|
||||
.then(handleSuccess)
|
||||
.catch(handleError);
|
||||
}
|
||||
};
|
||||
|
||||
// Form initial values in create and edit mode.
|
||||
const initialValues = {
|
||||
...defaultInitialValues,
|
||||
/**
|
||||
* We only care about the fields in the form. Previously unfilled optional
|
||||
* values such as `notes` come back from the API as null, so remove those
|
||||
* as well.
|
||||
*/
|
||||
...transformToForm(
|
||||
transformAccountToForm(account, {
|
||||
action,
|
||||
parentAccountId,
|
||||
accountType,
|
||||
}),
|
||||
defaultInitialValues,
|
||||
),
|
||||
};
|
||||
|
||||
// Handles dialog close.
|
||||
const handleClose = useCallback(() => {
|
||||
closeDialog(dialogName);
|
||||
}, [closeDialog, dialogName]);
|
||||
|
||||
return (
|
||||
<Formik
|
||||
validationSchema={validationSchema}
|
||||
initialValues={initialValues}
|
||||
onSubmit={handleFormSubmit}
|
||||
>
|
||||
<AccountDialogFormContent
|
||||
dialogName={dialogName}
|
||||
action={action}
|
||||
onClose={handleClose}
|
||||
/>
|
||||
</Formik>
|
||||
);
|
||||
}
|
||||
|
||||
export default compose(withDialogActions)(AccountFormDialogContent);
|
||||
Reference in New Issue
Block a user