mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-18 22:00:31 +00:00
WIP
This commit is contained in:
273
client/src/containers/Dashboard/Dialogs/AccountFormDialog.js
Normal file
273
client/src/containers/Dashboard/Dialogs/AccountFormDialog.js
Normal file
@@ -0,0 +1,273 @@
|
||||
import React, { useState} from 'react';
|
||||
import {
|
||||
Button,
|
||||
Classes,
|
||||
FormGroup,
|
||||
InputGroup,
|
||||
Intent,
|
||||
TextArea,
|
||||
MenuItem,
|
||||
Checkbox,
|
||||
} from "@blueprintjs/core";
|
||||
import {Select} from '@blueprintjs/select';
|
||||
import * as Yup from 'yup';
|
||||
import { useFormik } from 'formik';
|
||||
import { useIntl } from 'react-intl';
|
||||
import { omit } from 'lodash';
|
||||
import { compose } from 'utils';
|
||||
import useAsync from 'hooks/async';
|
||||
import Dialog from 'components/Dialog';
|
||||
import AppToaster from 'components/AppToaster';
|
||||
import DialogConnect from 'connectors/Dialog.connector';
|
||||
import DialogReduxConnect from 'components/DialogReduxConnect';
|
||||
import AccountFormDialogConnect from 'connectors/AccountFormDialog.connector';
|
||||
|
||||
function AccountFormDialog ({
|
||||
name,
|
||||
payload,
|
||||
isOpen,
|
||||
accountsTypes,
|
||||
accounts,
|
||||
fetchAccounts,
|
||||
fetchAccountTypes,
|
||||
closeDialog,
|
||||
submitAccount,
|
||||
fetchAccount,
|
||||
editAccount
|
||||
}) {
|
||||
const intl = useIntl();
|
||||
const accountFormValidationSchema = Yup.object().shape({
|
||||
name: Yup
|
||||
.string()
|
||||
.required(intl.formatMessage({ 'id': 'required' })),
|
||||
code: Yup
|
||||
.number(intl.formatMessage({ id: 'field_name_must_be_number' })),
|
||||
account_type_id: Yup
|
||||
.string()
|
||||
.nullable()
|
||||
.required(intl.formatMessage({ 'id': 'required' })),
|
||||
description: Yup.string().trim(),
|
||||
});
|
||||
|
||||
// Formik
|
||||
const formik = useFormik({
|
||||
enableReinitialize: true,
|
||||
initialValues: {
|
||||
...payload.action === 'edit' && editAccount,
|
||||
},
|
||||
validationSchema: accountFormValidationSchema,
|
||||
onSubmit: (values) => {
|
||||
const exclude = ['subaccount'];
|
||||
|
||||
if (payload.action === 'edit') {
|
||||
editAccount({
|
||||
payload: payload.id,
|
||||
form: { ...omit(values, exclude) }
|
||||
}).then((response) => {
|
||||
closeDialog(name);
|
||||
AppToaster.show({
|
||||
message: 'the_account_has_been_edited',
|
||||
});
|
||||
});
|
||||
} else {
|
||||
submitAccount({ form: { ...omit(values, exclude) } }).then(response => {
|
||||
closeDialog(name);
|
||||
AppToaster.show({
|
||||
message: 'the_account_has_been_submit',
|
||||
});
|
||||
});
|
||||
}
|
||||
},
|
||||
});
|
||||
const [state, setState] = useState({
|
||||
loading: true,
|
||||
dialogActive: true,
|
||||
selectedAccountType: null,
|
||||
selectedSubaccount: null,
|
||||
});
|
||||
|
||||
// Filters accounts types items.
|
||||
const filterAccountTypeItems = (query, accountType, _index, exactMatch) => {
|
||||
const normalizedTitle = accountType.name.toLowerCase();
|
||||
const normalizedQuery = query.toLowerCase();
|
||||
|
||||
if (exactMatch) {
|
||||
return normalizedTitle === normalizedQuery;
|
||||
} else {
|
||||
return normalizedTitle.indexOf(normalizedQuery) >= 0;
|
||||
}
|
||||
};
|
||||
|
||||
// Account type item of select filed.
|
||||
const accountTypeItem = (item, { handleClick, modifiers, query }) => {
|
||||
return (<MenuItem text={item.name} key={item.id} onClick={handleClick} />);
|
||||
};
|
||||
|
||||
// Account item of select accounts field.
|
||||
const accountItem = (item, { handleClick, modifiers, query }) => {
|
||||
return (<MenuItem text={item.name} label={item.code} key={item.id} onClick={handleClick} />)
|
||||
};
|
||||
|
||||
// Filters accounts items.
|
||||
const filterAccountsPredicater = (query, account, _index, exactMatch) => {
|
||||
const normalizedTitle = account.name.toLowerCase();
|
||||
const normalizedQuery = query.toLowerCase();
|
||||
|
||||
if (exactMatch) {
|
||||
return normalizedTitle === normalizedQuery;
|
||||
} else {
|
||||
return `${account.code} ${normalizedTitle}`.indexOf(normalizedQuery) >= 0;
|
||||
}
|
||||
};
|
||||
|
||||
const handleClose = () => { closeDialog(name); };
|
||||
|
||||
const fetchHook = useAsync(async () => {
|
||||
await Promise.all([
|
||||
fetchAccounts(),
|
||||
fetchAccountTypes(),
|
||||
|
||||
// Fetch the target in case edit mode.
|
||||
...(payload.action === 'edit') ? [
|
||||
fetchAccount(payload.id),
|
||||
] : [],
|
||||
]);
|
||||
}, false);
|
||||
|
||||
const onDialogOpening = async () => { fetchHook.execute(); }
|
||||
|
||||
const onChangeAccountType = (accountType) => {
|
||||
setState({ ...state, selectedAccountType: accountType.name });
|
||||
formik.setFieldValue('account_type_id', accountType.id);
|
||||
};
|
||||
const onChangeSubaccount = (account) => {
|
||||
setState({ ...state, selectedSubaccount: account });
|
||||
formik.setFieldValue('parent_account_id', account.id);
|
||||
};
|
||||
|
||||
const onDialogClosed = () => {
|
||||
formik.resetForm();
|
||||
setState({
|
||||
...state,
|
||||
selectedSubaccount: null,
|
||||
selectedAccountType: null,
|
||||
});
|
||||
};
|
||||
return (
|
||||
<Dialog
|
||||
name={name}
|
||||
title={payload.action === 'edit' ? 'Edit Account' : 'New Account'}
|
||||
className={{'dialog--loading': state.isLoading, 'dialog--account-form': true }}
|
||||
onClosed={onDialogClosed}
|
||||
onOpening={onDialogOpening}
|
||||
isOpen={isOpen}
|
||||
isLoading={fetchHook.pending}
|
||||
>
|
||||
<form onSubmit={formik.handleSubmit}>
|
||||
<div className={Classes.DIALOG_BODY}>
|
||||
<FormGroup
|
||||
label={'Account Type'}
|
||||
className="{'form-group--account-type'}"
|
||||
inline={true}
|
||||
helperText={formik.errors.account_type_id && formik.errors.account_type_id}
|
||||
intent={formik.errors.account_type_id && Intent.DANGER}>
|
||||
|
||||
<Select
|
||||
items={accountsTypes}
|
||||
noResults={<MenuItem disabled={true} text="No results." />}
|
||||
itemRenderer={accountTypeItem}
|
||||
itemPredicate={filterAccountTypeItems}
|
||||
popoverProps={{ minimal: true }}
|
||||
onItemSelect={onChangeAccountType}>
|
||||
<Button
|
||||
rightIcon="caret-down"
|
||||
text={state.selectedAccountType || 'Select account type'} />
|
||||
</Select>
|
||||
</FormGroup>
|
||||
|
||||
<FormGroup
|
||||
label={'Account Name'}
|
||||
className={'form-group--account-name'}
|
||||
intent={formik.errors.name && Intent.DANGER}
|
||||
helperText={formik.errors.name && formik.errors.name}
|
||||
inline={true}>
|
||||
|
||||
<InputGroup
|
||||
medium={true}
|
||||
intent={formik.errors.name && Intent.DANGER}
|
||||
{...formik.getFieldProps('name')} />
|
||||
</FormGroup>
|
||||
|
||||
<FormGroup
|
||||
label={'Account Code'}
|
||||
className={'form-group--account-code'}
|
||||
intent={formik.errors.code && Intent.DANGER}
|
||||
helperText={formik.errors.code && formik.errors.code}
|
||||
inline={true}>
|
||||
|
||||
<InputGroup
|
||||
medium={true}
|
||||
intent={formik.errors.code && Intent.DANGER}
|
||||
{...formik.getFieldProps('code')} />
|
||||
</FormGroup>
|
||||
|
||||
<FormGroup
|
||||
label={' '}
|
||||
className={'form-group--subaccount'}
|
||||
inline={true}>
|
||||
|
||||
<Checkbox
|
||||
inline={true}
|
||||
label={'Sub account?'}
|
||||
{...formik.getFieldProps('subaccount')} />
|
||||
</FormGroup>
|
||||
|
||||
{ (formik.values.subaccount) &&
|
||||
<FormGroup
|
||||
label={'Sub Account'}
|
||||
className="{'form-group--sub-account'}"
|
||||
inline={true}>
|
||||
<Select
|
||||
items={accounts}
|
||||
noResults={<MenuItem disabled={true} text="No results." />}
|
||||
itemRenderer={accountItem}
|
||||
itemPredicate={filterAccountsPredicater}
|
||||
popoverProps={{ minimal: true }}
|
||||
onItemSelect={onChangeSubaccount}
|
||||
{...formik.getFieldProps('parent_account_id')}>
|
||||
<Button
|
||||
rightIcon="caret-down"
|
||||
text={state.selectedSubaccount ? state.selectedSubaccount.name : "Select Parent Account"}
|
||||
/>
|
||||
</Select>
|
||||
</FormGroup> }
|
||||
|
||||
<FormGroup
|
||||
label={'Description'}
|
||||
className={'form-group--description'}
|
||||
intent={formik.errors.description && Intent.DANGER}
|
||||
helperText={formik.errors.description && formik.errors.credential}
|
||||
inline={true}>
|
||||
|
||||
<TextArea growVertically={true} large={true} {...formik.getFieldProps('description')} />
|
||||
</FormGroup>
|
||||
</div>
|
||||
|
||||
<div className={Classes.DIALOG_FOOTER}>
|
||||
<div className={Classes.DIALOG_FOOTER_ACTIONS}>
|
||||
<Button onClick={handleClose}>Close</Button>
|
||||
<Button intent={Intent.PRIMARY} type="submit">
|
||||
{ payload.action === 'edit' ? 'Edit' : 'Submit' }
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
|
||||
export default compose(
|
||||
AccountFormDialogConnect,
|
||||
DialogReduxConnect,
|
||||
DialogConnect,
|
||||
)(AccountFormDialog);
|
||||
201
client/src/containers/Dashboard/Dialogs/UserFormDialog.js
Normal file
201
client/src/containers/Dashboard/Dialogs/UserFormDialog.js
Normal file
@@ -0,0 +1,201 @@
|
||||
import React from 'react';
|
||||
import { useIntl } from "react-intl"
|
||||
import {useFormik} from 'formik';
|
||||
import * as Yup from 'yup';
|
||||
import {
|
||||
Dialog,
|
||||
Button,
|
||||
FormGroup,
|
||||
InputGroup,
|
||||
Intent,
|
||||
TextArea,
|
||||
MenuItem,
|
||||
Checkbox,
|
||||
Classes,
|
||||
HTMLSelect,
|
||||
} from '@blueprintjs/core';
|
||||
import UserFormDialogConnect from 'connectors/UserFormDialog.connector';
|
||||
import DialogReduxConnect from 'components/DialogReduxConnect';
|
||||
import AppToaster from 'components/AppToaster';
|
||||
import useAsync from 'hooks/async';
|
||||
import {objectKeysTransform} from 'utils';
|
||||
import {pick, snakeCase} from 'lodash';
|
||||
|
||||
function UserFormDialog({
|
||||
fetchUser,
|
||||
submitUser,
|
||||
editUser,
|
||||
name,
|
||||
payload,
|
||||
isOpen,
|
||||
userDetails,
|
||||
closeDialog,
|
||||
}) {
|
||||
const intl = useIntl();
|
||||
const fetchHook = useAsync(async () => {
|
||||
await Promise.all([
|
||||
...(payload.action === 'edit') ? [
|
||||
fetchUser(payload.user.id),
|
||||
] : [],
|
||||
]);
|
||||
}, false);
|
||||
|
||||
const validationSchema = Yup.object().shape({
|
||||
first_name: Yup.string().required(),
|
||||
last_name: Yup.string().required(),
|
||||
email: Yup.string().email().required(),
|
||||
phone_number: Yup.string().required(),
|
||||
password: Yup.string().min(5).required(),
|
||||
});
|
||||
|
||||
const initialValues = {
|
||||
status: 1,
|
||||
...payload.action === 'edit' &&
|
||||
pick(
|
||||
objectKeysTransform(payload.user, snakeCase),
|
||||
Object.keys(validationSchema.fields)
|
||||
),
|
||||
password: '',
|
||||
};
|
||||
|
||||
const formik = useFormik({
|
||||
enableReinitialize: true,
|
||||
initialValues,
|
||||
validationSchema,
|
||||
onSubmit: (values) => {
|
||||
const form = {
|
||||
...values,
|
||||
confirm_password: values.password,
|
||||
};
|
||||
if (payload.action === 'edit') {
|
||||
editUser(payload.user.id, form).then((response) => {
|
||||
AppToaster.show({
|
||||
message: 'the_user_details_has_been_updated',
|
||||
});
|
||||
closeDialog(name);
|
||||
});
|
||||
} else {
|
||||
submitUser(form).then((response) => {
|
||||
AppToaster.show({
|
||||
message: 'the_user_has_been_invited',
|
||||
});
|
||||
closeDialog(name);
|
||||
});
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
const statusOptions = [
|
||||
{value: 1, label: 'Active'},
|
||||
{value: 2, label: 'Inactive'},
|
||||
];
|
||||
|
||||
const onDialogOpening = () => { fetchHook.execute(); };
|
||||
|
||||
const onDialogClosed = () => {
|
||||
formik.resetForm();
|
||||
|
||||
};
|
||||
|
||||
const handleClose = () => { closeDialog(name); };
|
||||
|
||||
return (
|
||||
<Dialog
|
||||
isOpen={isOpen}
|
||||
name={name}
|
||||
title={payload.action === 'edit' ? 'Edit User' : 'New User'}
|
||||
isLoading={fetchHook.pending}
|
||||
onClosed={onDialogClosed}
|
||||
onOpening={onDialogOpening}>
|
||||
|
||||
<form onSubmit={formik.handleSubmit}>
|
||||
<div className={Classes.DIALOG_BODY}>
|
||||
<FormGroup
|
||||
label={'First Name'}
|
||||
className={'form-group--first-name'}
|
||||
intent={formik.errors.first_name && Intent.DANGER}
|
||||
helperText={formik.errors.first_name && formik.errors.first_name}
|
||||
inline={true}>
|
||||
|
||||
<InputGroup
|
||||
intent={formik.errors.first_name && Intent.DANGER}
|
||||
{...formik.getFieldProps('first_name')} />
|
||||
</FormGroup>
|
||||
|
||||
<FormGroup
|
||||
label={'Last Name'}
|
||||
className={'form-group--last-name'}
|
||||
intent={formik.errors.last_name && Intent.DANGER}
|
||||
helperText={formik.errors.last_name && formik.errors.last_name}
|
||||
inline={true}>
|
||||
|
||||
<InputGroup
|
||||
intent={formik.errors.last_name && Intent.DANGER}
|
||||
{...formik.getFieldProps('last_name')} />
|
||||
</FormGroup>
|
||||
|
||||
<FormGroup
|
||||
label={'Email'}
|
||||
className={'form-group--email'}
|
||||
intent={formik.errors.email && Intent.DANGER}
|
||||
helperText={formik.errors.email && formik.errors.email}
|
||||
inline={true}>
|
||||
|
||||
<InputGroup
|
||||
intent={formik.errors.email && Intent.DANGER}
|
||||
{...formik.getFieldProps('email')} />
|
||||
</FormGroup>
|
||||
|
||||
<FormGroup
|
||||
label={'Phone Number'}
|
||||
className={'form-group--phone-number'}
|
||||
intent={formik.errors.phone_number && Intent.DANGER}
|
||||
helperText={formik.errors.phone_number && formik.errors.phone_number}
|
||||
inline={true}>
|
||||
|
||||
<InputGroup
|
||||
intent={formik.errors.phone_number && Intent.DANGER}
|
||||
{...formik.getFieldProps('phone_number')} />
|
||||
</FormGroup>
|
||||
|
||||
<FormGroup
|
||||
label={'Password'}
|
||||
className={'form-group--password'}
|
||||
intent={formik.errors.password && Intent.DANGER}
|
||||
helperText={formik.errors.password && formik.errors.password}
|
||||
inline={true}>
|
||||
|
||||
<InputGroup
|
||||
intent={formik.errors.password && Intent.DANGER}
|
||||
className={Classes.FILL}
|
||||
{...formik.getFieldProps('password')} />
|
||||
</FormGroup>
|
||||
|
||||
<FormGroup
|
||||
label={'Status'}
|
||||
className={'form-group--status'}
|
||||
intent={formik.errors.status && Intent.DANGER}
|
||||
helperText={formik.errors.status && formik.errors.status}
|
||||
inline={true}>
|
||||
|
||||
<HTMLSelect
|
||||
options={statusOptions}
|
||||
className={Classes.FILL}
|
||||
{...formik.getFieldProps(`status`)} />
|
||||
</FormGroup>
|
||||
|
||||
<div className={Classes.DIALOG_FOOTER}>
|
||||
<div className={Classes.DIALOG_FOOTER_ACTIONS}>
|
||||
<Button onClick={handleClose}>Close</Button>
|
||||
<Button intent={Intent.PRIMARY} type="submit">
|
||||
{ payload.action === 'edit' ? 'Edit' : 'Submit' }
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
|
||||
export default UserFormDialogConnect(DialogReduxConnect(UserFormDialog));
|
||||
Reference in New Issue
Block a user