mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-17 13:20:31 +00:00
feat(branche): add crud branches.
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
import React from 'react';
|
||||
import intl from 'react-intl-universal';
|
||||
|
||||
import { Formik } from 'formik';
|
||||
import { Intent } from '@blueprintjs/core';
|
||||
|
||||
import { AppToaster } from 'components';
|
||||
import { CreateBranchFormSchema } from './BranchForm.schema';
|
||||
@@ -8,30 +11,67 @@ import BranchFormContent from './BranchFormContent';
|
||||
import { useBranchFormContext } from './BranchFormProvider';
|
||||
|
||||
import withDialogActions from 'containers/Dialog/withDialogActions';
|
||||
import { compose } from 'utils';
|
||||
import { compose, transformToForm } from 'utils';
|
||||
|
||||
const defaultInitialValues = {
|
||||
branch_name: '',
|
||||
branch_address_1: '',
|
||||
branch_address_2: '',
|
||||
name: '',
|
||||
code: '',
|
||||
address: '',
|
||||
phone_number: '',
|
||||
email: '',
|
||||
website: '',
|
||||
branch_address_city: '',
|
||||
branch_address_country: '',
|
||||
city: '',
|
||||
country: '',
|
||||
};
|
||||
|
||||
function BranchForm({
|
||||
// #withDialogActions
|
||||
closeDialog,
|
||||
}) {
|
||||
const {
|
||||
dialogName,
|
||||
branch,
|
||||
branchId,
|
||||
createBranchMutate,
|
||||
editBranchMutate,
|
||||
} = useBranchFormContext();
|
||||
|
||||
// Initial form values.
|
||||
const initialValues = {
|
||||
...defaultInitialValues,
|
||||
...transformToForm(branch, defaultInitialValues),
|
||||
};
|
||||
|
||||
// Handles the form submit.
|
||||
const handleFormSubmit = (values, { setSubmitting, setErrors }) => {};
|
||||
const handleFormSubmit = (values, { setSubmitting, setErrors }) => {
|
||||
const form = { ...values };
|
||||
|
||||
// Handle request response success.
|
||||
const onSuccess = (response) => {
|
||||
AppToaster.show({
|
||||
message: intl.get('branch.dialog.success_message'),
|
||||
intent: Intent.SUCCESS,
|
||||
});
|
||||
closeDialog(dialogName);
|
||||
};
|
||||
|
||||
// Handle request response errors.
|
||||
const onError = ({
|
||||
response: {
|
||||
data: { errors },
|
||||
},
|
||||
}) => {
|
||||
if (errors) {
|
||||
}
|
||||
setSubmitting(false);
|
||||
};
|
||||
|
||||
if (branchId) {
|
||||
editBranchMutate([branchId, form]).then(onSuccess).catch(onError);
|
||||
} else {
|
||||
createBranchMutate(form).then(onSuccess).catch(onError);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<Formik
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
import * as Yup from 'yup';
|
||||
import intl from 'react-intl-universal';
|
||||
import { DATATYPES_LENGTH } from 'common/dataTypes';
|
||||
|
||||
const Schema = Yup.object().shape({
|
||||
branch_name: Yup.string().required().label(intl.get('branch_name')),
|
||||
branch_address_1: Yup.string().trim(),
|
||||
branch_address_2: Yup.string().trim(),
|
||||
branch_address_city: Yup.string().trim(),
|
||||
branch_address_country: Yup.string().trim(),
|
||||
name: Yup.string().required().label(intl.get('branch_name')),
|
||||
code: Yup.string().trim().min(0).max(DATATYPES_LENGTH.STRING),
|
||||
address: Yup.string().trim(),
|
||||
city: Yup.string().trim(),
|
||||
country: Yup.string().trim(),
|
||||
website: Yup.string().url().nullable(),
|
||||
phone_number: Yup.number(),
|
||||
email: Yup.string().email().nullable(),
|
||||
|
||||
@@ -11,9 +11,10 @@ import BranchForm from './BranchForm';
|
||||
export default function BranchFormDialogContent({
|
||||
// #ownProps
|
||||
dialogName,
|
||||
branchId,
|
||||
}) {
|
||||
return (
|
||||
<BranchFormProvider dialogName={dialogName}>
|
||||
<BranchFormProvider branchId={branchId} dialogName={dialogName}>
|
||||
<BranchForm />
|
||||
</BranchFormProvider>
|
||||
);
|
||||
|
||||
@@ -20,7 +20,7 @@ function BranchFormFields() {
|
||||
return (
|
||||
<div className={Classes.DIALOG_BODY}>
|
||||
{/*------------ Branch Name -----------*/}
|
||||
<FastField name={'branch_name'}>
|
||||
<FastField name={'name'}>
|
||||
{({ form, field, meta: { error, touched } }) => (
|
||||
<FormGroup
|
||||
label={<T id={'branch.dialog.label.branch_name'} />}
|
||||
@@ -34,15 +34,29 @@ function BranchFormFields() {
|
||||
</FormGroup>
|
||||
)}
|
||||
</FastField>
|
||||
{/*------------ Branch Code -----------*/}
|
||||
<FastField name={'code'}>
|
||||
{({ form, field, meta: { error, touched } }) => (
|
||||
<FormGroup
|
||||
label={<T id={'branch.dialog.label.branch_code'} />}
|
||||
intent={inputIntent({ error, touched })}
|
||||
inline={true}
|
||||
helperText={<ErrorMessage name="code" />}
|
||||
className={'form-group--branch_name'}
|
||||
>
|
||||
<InputGroup intent={inputIntent({ error, touched })} {...field} />
|
||||
</FormGroup>
|
||||
)}
|
||||
</FastField>
|
||||
|
||||
{/*------------ Branch Address 1 -----------*/}
|
||||
<FastField name={'branch_address_1'}>
|
||||
{/*------------ Branch Address -----------*/}
|
||||
<FastField name={'address'}>
|
||||
{({ form, field, meta: { error, touched } }) => (
|
||||
<FormGroup
|
||||
label={intl.get('branch.dialog.label.branch_address')}
|
||||
intent={inputIntent({ error, touched })}
|
||||
inline={true}
|
||||
helperText={<ErrorMessage name="branch_address_1" />}
|
||||
helperText={<ErrorMessage name="address" />}
|
||||
className={'form-group--branch_address'}
|
||||
>
|
||||
<InputGroup
|
||||
@@ -54,24 +68,6 @@ function BranchFormFields() {
|
||||
)}
|
||||
</FastField>
|
||||
<BranchAddressWrap>
|
||||
{/*------------ Branch Address 2 -----------*/}
|
||||
<FastField name={'branch_address_2'}>
|
||||
{({ form, field, meta: { error, touched } }) => (
|
||||
<FormGroup
|
||||
intent={inputIntent({ error, touched })}
|
||||
inline={true}
|
||||
helperText={<ErrorMessage name="branch_address_2" />}
|
||||
className={'form-group--branch_address'}
|
||||
>
|
||||
<InputGroup
|
||||
intent={inputIntent({ error, touched })}
|
||||
placeholder={intl.get('branch.dialog.label.address_2')}
|
||||
{...field}
|
||||
/>
|
||||
</FormGroup>
|
||||
)}
|
||||
</FastField>
|
||||
|
||||
{/*------------ Branch Address City & Country-----------*/}
|
||||
<FormGroup
|
||||
inline={true}
|
||||
@@ -79,7 +75,7 @@ function BranchFormFields() {
|
||||
helperText={<ErrorMessage name="branch_address_2" />}
|
||||
>
|
||||
<ControlGroup>
|
||||
<FastField name={'branch_address_city'}>
|
||||
<FastField name={'city'}>
|
||||
{({ field, meta: { error, touched } }) => (
|
||||
<InputGroup
|
||||
intent={inputIntent({ error, touched })}
|
||||
@@ -89,7 +85,7 @@ function BranchFormFields() {
|
||||
)}
|
||||
</FastField>
|
||||
|
||||
<FastField name={'branch_address_country'}>
|
||||
<FastField name={'country'}>
|
||||
{({ field, meta: { error, touched } }) => (
|
||||
<InputGroup
|
||||
intent={inputIntent({ error, touched })}
|
||||
@@ -112,7 +108,7 @@ function BranchFormFields() {
|
||||
helperText={<ErrorMessage name="phone_number" />}
|
||||
className={'form-group--phone_number'}
|
||||
>
|
||||
<InputGroup intent={inputIntent({ error, touched })} {...field} />
|
||||
<InputGroup placeholder={'https://'} {...field} />
|
||||
</FormGroup>
|
||||
)}
|
||||
</FastField>
|
||||
|
||||
@@ -1,21 +1,33 @@
|
||||
import React from 'react';
|
||||
import { DialogContent } from 'components';
|
||||
// import {} from 'hooks/query';
|
||||
import { useCreateBranch, useEditBranch, useBranch } from 'hooks/query';
|
||||
|
||||
const BranchFormContext = React.createContext();
|
||||
|
||||
/**
|
||||
* Branch form dialog provider.
|
||||
*/
|
||||
function BranchFormProvider({ dialogName, ...props }) {
|
||||
function BranchFormProvider({ dialogName, branchId, ...props }) {
|
||||
// Create and edit warehouse mutations.
|
||||
const { mutateAsync: createBranchMutate } = useCreateBranch();
|
||||
const { mutateAsync: editBranchMutate } = useEditBranch();
|
||||
|
||||
// Handle fetch branch detail.
|
||||
const { data: branch, isLoading: isBranchLoading } = useBranch(branchId, {
|
||||
enabled: !!branchId,
|
||||
});
|
||||
|
||||
// State provider.
|
||||
const provider = {
|
||||
dialogName,
|
||||
branch,
|
||||
branchId,
|
||||
createBranchMutate,
|
||||
editBranchMutate,
|
||||
};
|
||||
|
||||
return (
|
||||
<DialogContent
|
||||
// isLoading={}
|
||||
>
|
||||
<DialogContent isLoading={isBranchLoading}>
|
||||
<BranchFormContext.Provider value={provider} {...props} />
|
||||
</DialogContent>
|
||||
);
|
||||
|
||||
@@ -12,18 +12,28 @@ const BranchFormDialogContent = React.lazy(() =>
|
||||
/**
|
||||
* Branch form form dialog.
|
||||
*/
|
||||
function BranchFormDialog({ dialogName, payload = {}, isOpen }) {
|
||||
function BranchFormDialog({
|
||||
dialogName,
|
||||
payload: { branchId, action },
|
||||
isOpen,
|
||||
}) {
|
||||
return (
|
||||
<Dialog
|
||||
name={dialogName}
|
||||
title={<T id={'branch.dialog.label'} />}
|
||||
title={
|
||||
action === 'edit' ? (
|
||||
<T id={'branch.dialog.label_edit_branch'} />
|
||||
) : (
|
||||
<T id={'branch.dialog.label_new_branch'} />
|
||||
)
|
||||
}
|
||||
isOpen={isOpen}
|
||||
canEscapeJeyClose={true}
|
||||
autoFocus={true}
|
||||
className={'dialog--branch-form'}
|
||||
>
|
||||
<DialogSuspense>
|
||||
<BranchFormDialogContent dialogName={dialogName} />
|
||||
<BranchFormDialogContent dialogName={dialogName} branchId={branchId} />
|
||||
</DialogSuspense>
|
||||
</Dialog>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user