mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 12:50:38 +00:00
re-structure to monorepo.
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
// @ts-nocheck
|
||||
import * as Yup from 'yup';
|
||||
import intl from 'react-intl-universal';
|
||||
import { DATATYPES_LENGTH } from '@/constants/dataTypes';
|
||||
|
||||
const Schema = Yup.object().shape({
|
||||
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(),
|
||||
});
|
||||
|
||||
export const CreateBranchFormSchema = Schema;
|
||||
@@ -0,0 +1,83 @@
|
||||
// @ts-nocheck
|
||||
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';
|
||||
import { transformErrors } from './utils';
|
||||
|
||||
import BranchFormContent from './BranchFormContent';
|
||||
import { useBranchFormContext } from './BranchFormProvider';
|
||||
|
||||
import withDialogActions from '@/containers/Dialog/withDialogActions';
|
||||
import { compose, transformToForm } from '@/utils';
|
||||
|
||||
const defaultInitialValues = {
|
||||
name: '',
|
||||
code: '',
|
||||
address: '',
|
||||
phone_number: '',
|
||||
email: '',
|
||||
website: '',
|
||||
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 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) {
|
||||
}
|
||||
transformErrors(errors, { setErrors });
|
||||
setSubmitting(false);
|
||||
};
|
||||
|
||||
if (branchId) {
|
||||
editBranchMutate([branchId, form]).then(onSuccess).catch(onError);
|
||||
} else {
|
||||
createBranchMutate(form).then(onSuccess).catch(onError);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<Formik
|
||||
validationSchema={CreateBranchFormSchema}
|
||||
initialValues={initialValues}
|
||||
onSubmit={handleFormSubmit}
|
||||
component={BranchFormContent}
|
||||
/>
|
||||
);
|
||||
}
|
||||
export default compose(withDialogActions)(BranchForm);
|
||||
@@ -0,0 +1,18 @@
|
||||
// @ts-nocheck
|
||||
import React from 'react';
|
||||
import { Form } from 'formik';
|
||||
|
||||
import BranchFormFields from './BranchFormFields';
|
||||
import BranchFormFloatingActions from './BranchFormFloatingActions';
|
||||
|
||||
/**
|
||||
* Branch form content.
|
||||
*/
|
||||
export default function BranchFormContent() {
|
||||
return (
|
||||
<Form>
|
||||
<BranchFormFields />
|
||||
<BranchFormFloatingActions />
|
||||
</Form>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
// @ts-nocheck
|
||||
import React from 'react';
|
||||
|
||||
import '@/style/pages/Branches/BranchFormDialog.scss';
|
||||
|
||||
import { BranchFormProvider } from './BranchFormProvider';
|
||||
import BranchForm from './BranchForm';
|
||||
|
||||
/**
|
||||
* Branch form dialog content.
|
||||
*/
|
||||
export default function BranchFormDialogContent({
|
||||
// #ownProps
|
||||
dialogName,
|
||||
branchId,
|
||||
}) {
|
||||
return (
|
||||
<BranchFormProvider branchId={branchId} dialogName={dialogName}>
|
||||
<BranchForm />
|
||||
</BranchFormProvider>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,152 @@
|
||||
// @ts-nocheck
|
||||
import React from 'react';
|
||||
import intl from 'react-intl-universal';
|
||||
import styled from 'styled-components';
|
||||
import { FastField, ErrorMessage, Field } from 'formik';
|
||||
import {
|
||||
Classes,
|
||||
FormGroup,
|
||||
InputGroup,
|
||||
ControlGroup,
|
||||
} from '@blueprintjs/core';
|
||||
import { inputIntent } from '@/utils';
|
||||
import { FieldRequiredHint, FormattedMessage as T } from '@/components';
|
||||
|
||||
/**
|
||||
* Branch form dialog fields.
|
||||
*/
|
||||
function BranchFormFields() {
|
||||
return (
|
||||
<div className={Classes.DIALOG_BODY}>
|
||||
{/*------------ Branch Name -----------*/}
|
||||
<FastField name={'name'}>
|
||||
{({ form, field, meta: { error, touched } }) => (
|
||||
<FormGroup
|
||||
label={<T id={'branch.dialog.label.branch_name'} />}
|
||||
labelInfo={<FieldRequiredHint />}
|
||||
intent={inputIntent({ error, touched })}
|
||||
inline={true}
|
||||
helperText={<ErrorMessage name="branch_name" />}
|
||||
className={'form-group--branch_name'}
|
||||
>
|
||||
<InputGroup intent={inputIntent({ error, touched })} {...field} />
|
||||
</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 -----------*/}
|
||||
<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="address" />}
|
||||
className={'form-group--branch_address'}
|
||||
>
|
||||
<InputGroup
|
||||
intent={inputIntent({ error, touched })}
|
||||
placeholder={intl.get('branch.dialog.label.address_1')}
|
||||
{...field}
|
||||
/>
|
||||
</FormGroup>
|
||||
)}
|
||||
</FastField>
|
||||
<BranchAddressWrap>
|
||||
{/*------------ Branch Address City & Country-----------*/}
|
||||
<FormGroup
|
||||
inline={true}
|
||||
className={'form-group--branch_address'}
|
||||
helperText={<ErrorMessage name="branch_address_2" />}
|
||||
>
|
||||
<ControlGroup>
|
||||
<FastField name={'city'}>
|
||||
{({ field, meta: { error, touched } }) => (
|
||||
<InputGroup
|
||||
intent={inputIntent({ error, touched })}
|
||||
placeholder={intl.get('branch.dialog.label.city')}
|
||||
{...field}
|
||||
/>
|
||||
)}
|
||||
</FastField>
|
||||
|
||||
<FastField name={'country'}>
|
||||
{({ field, meta: { error, touched } }) => (
|
||||
<InputGroup
|
||||
intent={inputIntent({ error, touched })}
|
||||
placeholder={intl.get('branch.dialog.label.country')}
|
||||
{...field}
|
||||
/>
|
||||
)}
|
||||
</FastField>
|
||||
</ControlGroup>
|
||||
</FormGroup>
|
||||
</BranchAddressWrap>
|
||||
|
||||
{/*------------ Phone Number -----------*/}
|
||||
<FastField name={'phone_number'}>
|
||||
{({ form, field, meta: { error, touched } }) => (
|
||||
<FormGroup
|
||||
label={intl.get('branch.dialog.label.phone_number')}
|
||||
intent={inputIntent({ error, touched })}
|
||||
inline={true}
|
||||
helperText={<ErrorMessage name="phone_number" />}
|
||||
className={'form-group--phone_number'}
|
||||
>
|
||||
<InputGroup placeholder={'https://'} {...field} />
|
||||
</FormGroup>
|
||||
)}
|
||||
</FastField>
|
||||
|
||||
{/*------------ Email -----------*/}
|
||||
<FastField name={'email'}>
|
||||
{({ form, field, meta: { error, touched } }) => (
|
||||
<FormGroup
|
||||
label={intl.get('branch.dialog.label.email')}
|
||||
intent={inputIntent({ error, touched })}
|
||||
inline={true}
|
||||
helperText={<ErrorMessage name="email" />}
|
||||
className={'form-group--email'}
|
||||
>
|
||||
<InputGroup intent={inputIntent({ error, touched })} {...field} />
|
||||
</FormGroup>
|
||||
)}
|
||||
</FastField>
|
||||
|
||||
{/*------------ Website -----------*/}
|
||||
<FastField name={'website'}>
|
||||
{({ form, field, meta: { error, touched } }) => (
|
||||
<FormGroup
|
||||
label={intl.get('branch.dialog.label.website')}
|
||||
intent={inputIntent({ error, touched })}
|
||||
inline={true}
|
||||
helperText={<ErrorMessage name="email" />}
|
||||
className={'form-group--website'}
|
||||
>
|
||||
<InputGroup intent={inputIntent({ error, touched })} {...field} />
|
||||
</FormGroup>
|
||||
)}
|
||||
</FastField>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default BranchFormFields;
|
||||
|
||||
const BranchAddressWrap = styled.div`
|
||||
margin-left: 160px;
|
||||
`;
|
||||
@@ -0,0 +1,47 @@
|
||||
// @ts-nocheck
|
||||
import React from 'react';
|
||||
|
||||
import { Intent, Button, Classes } from '@blueprintjs/core';
|
||||
import { useFormikContext } from 'formik';
|
||||
import { FormattedMessage as T } from '@/components';
|
||||
|
||||
import { useBranchFormContext } from './BranchFormProvider';
|
||||
import withDialogActions from '@/containers/Dialog/withDialogActions';
|
||||
import { compose } from '@/utils';
|
||||
|
||||
/**
|
||||
* Branch form floating actions.
|
||||
*/
|
||||
function BranchFormFloatingActions({
|
||||
// #withDialogActions
|
||||
closeDialog,
|
||||
}) {
|
||||
// Formik context.
|
||||
const { isSubmitting } = useFormikContext();
|
||||
|
||||
const { dialogName } = useBranchFormContext();
|
||||
|
||||
// Handle close button click.
|
||||
const handleCancelBtnClick = () => {
|
||||
closeDialog(dialogName);
|
||||
};
|
||||
|
||||
return (
|
||||
<div className={Classes.DIALOG_FOOTER}>
|
||||
<div className={Classes.DIALOG_FOOTER_ACTIONS}>
|
||||
<Button onClick={handleCancelBtnClick} style={{ minWidth: '85px' }}>
|
||||
<T id={'cancel'} />
|
||||
</Button>
|
||||
<Button
|
||||
intent={Intent.PRIMARY}
|
||||
loading={isSubmitting}
|
||||
style={{ minWidth: '95px' }}
|
||||
type="submit"
|
||||
>
|
||||
{<T id={'save'} />}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
export default compose(withDialogActions)(BranchFormFloatingActions);
|
||||
@@ -0,0 +1,38 @@
|
||||
// @ts-nocheck
|
||||
import React from 'react';
|
||||
import { DialogContent } from '@/components';
|
||||
import { useCreateBranch, useEditBranch, useBranch } from '@/hooks/query';
|
||||
|
||||
const BranchFormContext = React.createContext();
|
||||
|
||||
/**
|
||||
* Branch form dialog provider.
|
||||
*/
|
||||
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={isBranchLoading}>
|
||||
<BranchFormContext.Provider value={provider} {...props} />
|
||||
</DialogContent>
|
||||
);
|
||||
}
|
||||
const useBranchFormContext = () => React.useContext(BranchFormContext);
|
||||
|
||||
export { BranchFormProvider, useBranchFormContext };
|
||||
@@ -0,0 +1,41 @@
|
||||
// @ts-nocheck
|
||||
import React from 'react';
|
||||
import { Dialog, DialogSuspense, FormattedMessage as T } from '@/components';
|
||||
import withDialogRedux from '@/components/DialogReduxConnect';
|
||||
|
||||
import { compose } from '@/utils';
|
||||
|
||||
const BranchFormDialogContent = React.lazy(
|
||||
() => import('./BranchFormDialogContent'),
|
||||
);
|
||||
|
||||
/**
|
||||
* Branch form form dialog.
|
||||
*/
|
||||
function BranchFormDialog({
|
||||
dialogName,
|
||||
payload: { branchId, action },
|
||||
isOpen,
|
||||
}) {
|
||||
return (
|
||||
<Dialog
|
||||
name={dialogName}
|
||||
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} branchId={branchId} />
|
||||
</DialogSuspense>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
export default compose(withDialogRedux())(BranchFormDialog);
|
||||
@@ -0,0 +1,13 @@
|
||||
// @ts-nocheck
|
||||
import intl from 'react-intl-universal';
|
||||
|
||||
/**
|
||||
* Transformes the response errors types.
|
||||
*/
|
||||
export const transformErrors = (errors, { setErrors }) => {
|
||||
if (errors.find((error) => error.type === 'BRANCH_CODE_NOT_UNIQUE')) {
|
||||
setErrors({
|
||||
code: intl.get('branche.error.warehouse_code_not_unique'),
|
||||
});
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user