feat(branches): add branches.

This commit is contained in:
elforjani13
2022-01-23 23:23:40 +02:00
committed by a.bouhuolia
parent b5ebcca12d
commit 769d8fa548
21 changed files with 643 additions and 7 deletions

View File

@@ -0,0 +1,45 @@
import React from 'react';
import { Formik } from 'formik';
import { AppToaster } from 'components';
import { CreateBranchFormSchema } from './BranchForm.schema';
import BranchFormContent from './BranchFormContent';
import { useBranchFormContext } from './BranchFormProvider';
import withDialogActions from 'containers/Dialog/withDialogActions';
import { compose } from 'utils';
const defaultInitialValues = {
branch_name: '',
branch_address_1: '',
branch_address_2: '',
phone_number: '',
email: '',
website: '',
branch_address_city: '',
branch_address_country: '',
};
function BranchForm({
// #withDialogActions
closeDialog,
}) {
// Initial form values.
const initialValues = {
...defaultInitialValues,
};
// Handles the form submit.
const handleFormSubmit = (values, { setSubmitting, setErrors }) => {};
return (
<Formik
validationSchema={CreateBranchFormSchema}
initialValues={initialValues}
onSubmit={handleFormSubmit}
component={BranchFormContent}
/>
);
}
export default compose(withDialogActions)(BranchForm);

View File

@@ -0,0 +1,15 @@
import * as Yup from 'yup';
import intl from 'react-intl-universal';
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(),
website: Yup.string().url().nullable(),
phone_number: Yup.number(),
email: Yup.string().email().nullable(),
});
export const CreateBranchFormSchema = Schema;

View File

@@ -0,0 +1,17 @@
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>
);
}

View File

@@ -0,0 +1,20 @@
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,
}) {
return (
<BranchFormProvider dialogName={dialogName}>
<BranchForm />
</BranchFormProvider>
);
}

View File

@@ -0,0 +1,157 @@
import React from 'react';
import intl from 'react-intl-universal';
import { FastField, ErrorMessage, Field } from 'formik';
import styled from 'styled-components';
import {
Classes,
FormGroup,
InputGroup,
ControlGroup,
Position,
} from '@blueprintjs/core';
import { inputIntent } from 'utils';
import { FieldRequiredHint, Col, Row, FormattedMessage as T } from 'components';
import { useBranchFormContext } from './BranchFormProvider';
/**
* Branch form dialog fields.
*/
function BranchFormFields() {
return (
<div className={Classes.DIALOG_BODY}>
{/*------------ Branch Name -----------*/}
<FastField name={'branch_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 Address 1 -----------*/}
<FastField name={'branch_address_1'}>
{({ 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" />}
className={'form-group--branch_address_1'}
>
<InputGroup
intent={inputIntent({ error, touched })}
placeholder={intl.get('branch.dialog.label.address_1')}
{...field}
/>
</FormGroup>
)}
</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_2'}
>
<InputGroup
intent={inputIntent({ error, touched })}
placeholder={intl.get('branch.dialog.label.address_2')}
{...field}
/>
</FormGroup>
)}
</FastField>
{/*------------ Branch Address City & Country-----------*/}
<FormGroup
inline={true}
className={'form-group--branch_address_city'}
helperText={<ErrorMessage name="branch_address_2" />}
>
<ControlGroup>
<FastField name={'branch_address_city'}>
{({ field, meta: { error, touched } }) => (
<InputGroup
intent={inputIntent({ error, touched })}
placeholder={intl.get('branch.dialog.label.city')}
{...field}
/>
)}
</FastField>
<FastField name={'branch_address_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 intent={inputIntent({ error, touched })} {...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;
`;

View File

@@ -0,0 +1,46 @@
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);

View File

@@ -0,0 +1,25 @@
import React from 'react';
import { DialogContent } from 'components';
// import {} from 'hooks/query';
const BranchFormContext = React.createContext();
/**
* Branch form dialog provider.
*/
function BranchFormProvider({ dialogName, ...props }) {
// State provider.
const provider = {
dialogName,
};
return (
<DialogContent
// isLoading={}
>
<BranchFormContext.Provider value={provider} {...props} />
</DialogContent>
);
}
const useBranchFormContext = () => React.useContext(BranchFormContext);
export { BranchFormProvider, useBranchFormContext };

View File

@@ -0,0 +1,31 @@
import React from 'react';
import { FormattedMessage as T } from 'components';
import { Dialog, DialogSuspense } 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 = {}, isOpen }) {
return (
<Dialog
name={dialogName}
title={<T id={'branch.dialog.label'} />}
isOpen={isOpen}
canEscapeJeyClose={true}
autoFocus={true}
className={'dialog--branch-form'}
>
<DialogSuspense>
<BranchFormDialogContent dialogName={dialogName} />
</DialogSuspense>
</Dialog>
);
}
export default compose(withDialogRedux())(BranchFormDialog);