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,18 @@
|
||||
// @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('warehouse_name')),
|
||||
code: Yup.string().trim().min(0).max(DATATYPES_LENGTH.STRING),
|
||||
address: Yup.string().trim(),
|
||||
warehouse_address_2: Yup.string().trim(),
|
||||
city: Yup.string().trim(),
|
||||
country: Yup.string().trim(),
|
||||
phone_number: Yup.number(),
|
||||
website: Yup.string().url().nullable(),
|
||||
email: Yup.string().email().nullable(),
|
||||
});
|
||||
|
||||
export const CreateWarehouseFormSchema = Schema;
|
||||
@@ -0,0 +1,93 @@
|
||||
// @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 { CreateWarehouseFormSchema } from './WarehouseForm.schema';
|
||||
import { useWarehouseFormContext } from './WarehouseFormProvider';
|
||||
import WarehouseFormContent from './WarehouseFormContent';
|
||||
import { transformErrors } from './utils';
|
||||
|
||||
import withDialogActions from '@/containers/Dialog/withDialogActions';
|
||||
import { compose, transformToForm } from '@/utils';
|
||||
|
||||
const defaultInitialValues = {
|
||||
name: '',
|
||||
code: '',
|
||||
address: '',
|
||||
city: '',
|
||||
country: '',
|
||||
phone_number: '',
|
||||
website: '',
|
||||
email: '',
|
||||
};
|
||||
|
||||
/**
|
||||
* Warehouse form.
|
||||
* @returns
|
||||
*/
|
||||
function WarehouseForm({
|
||||
// #withDialogActions
|
||||
closeDialog,
|
||||
}) {
|
||||
const {
|
||||
dialogName,
|
||||
warehouse,
|
||||
warehouseId,
|
||||
createWarehouseMutate,
|
||||
editWarehouseMutate,
|
||||
} = useWarehouseFormContext();
|
||||
|
||||
// Initial form values.
|
||||
const initialValues = {
|
||||
...defaultInitialValues,
|
||||
...transformToForm(warehouse, 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('warehouse.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 (warehouseId) {
|
||||
editWarehouseMutate([warehouseId, form]).then(onSuccess).catch(onError);
|
||||
} else {
|
||||
createWarehouseMutate(form).then(onSuccess).catch(onError);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<Formik
|
||||
validationSchema={CreateWarehouseFormSchema}
|
||||
initialValues={initialValues}
|
||||
onSubmit={handleFormSubmit}
|
||||
component={WarehouseFormContent}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
export default compose(withDialogActions)(WarehouseForm);
|
||||
@@ -0,0 +1,19 @@
|
||||
// @ts-nocheck
|
||||
import React from 'react';
|
||||
import { Form } from 'formik';
|
||||
|
||||
import WarehouseFormFields from './WarehouseFormFields';
|
||||
import WarehouseFormFloatingActions from './WarehouseFormFloatingActions';
|
||||
|
||||
/**
|
||||
* Warehouse form content.
|
||||
* @returns
|
||||
*/
|
||||
export default function WarehouseFormContent() {
|
||||
return (
|
||||
<Form>
|
||||
<WarehouseFormFields />
|
||||
<WarehouseFormFloatingActions />
|
||||
</Form>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
// @ts-nocheck
|
||||
import React from 'react';
|
||||
|
||||
import '@/style/pages/Warehouses/warehouseFormDialog.scss';
|
||||
import { WarehouseFormProvider } from './WarehouseFormProvider';
|
||||
import WarehouseForm from './WarehouseForm';
|
||||
|
||||
/**
|
||||
* Warehouse form dialog content.
|
||||
*/
|
||||
export default function WarehouseFormDialogContent({
|
||||
// #ownProps
|
||||
dialogName,
|
||||
warehouseId,
|
||||
}) {
|
||||
return (
|
||||
<WarehouseFormProvider warehouseId={warehouseId} dialogName={dialogName}>
|
||||
<WarehouseForm />
|
||||
</WarehouseFormProvider>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,154 @@
|
||||
// @ts-nocheck
|
||||
import React from 'react';
|
||||
import intl from 'react-intl-universal';
|
||||
import styled from 'styled-components';
|
||||
import { FastField, ErrorMessage } from 'formik';
|
||||
import {
|
||||
Classes,
|
||||
FormGroup,
|
||||
InputGroup,
|
||||
ControlGroup,
|
||||
} from '@blueprintjs/core';
|
||||
import { inputIntent } from '@/utils';
|
||||
|
||||
import { FieldRequiredHint, FormattedMessage as T } from '@/components';
|
||||
|
||||
/**
|
||||
* Warehouse form fields.
|
||||
* @returns
|
||||
*/
|
||||
export default function WarehouseFormFields() {
|
||||
return (
|
||||
<div className={Classes.DIALOG_BODY}>
|
||||
{/*------------ Warehouse Name -----------*/}
|
||||
<FastField name={'name'}>
|
||||
{({ form, field, meta: { error, touched } }) => (
|
||||
<FormGroup
|
||||
label={<T id={'warehouse.dialog.label.warehouse_name'} />}
|
||||
labelInfo={<FieldRequiredHint />}
|
||||
intent={inputIntent({ error, touched })}
|
||||
inline={true}
|
||||
helperText={<ErrorMessage name="name" />}
|
||||
className={'form-group--warehouse_name'}
|
||||
>
|
||||
<InputGroup intent={inputIntent({ error, touched })} {...field} />
|
||||
</FormGroup>
|
||||
)}
|
||||
</FastField>
|
||||
|
||||
{/*------------ Warehouse Code -----------*/}
|
||||
<FastField name={'code'}>
|
||||
{({ form, field, meta: { error, touched } }) => (
|
||||
<FormGroup
|
||||
label={<T id={'warehouse.dialog.label.code'} />}
|
||||
intent={inputIntent({ error, touched })}
|
||||
inline={true}
|
||||
helperText={<ErrorMessage name="code" />}
|
||||
className={'form-group--warehouse_name'}
|
||||
>
|
||||
<InputGroup intent={inputIntent({ error, touched })} {...field} />
|
||||
</FormGroup>
|
||||
)}
|
||||
</FastField>
|
||||
|
||||
{/*------------ Warehouse Address -----------*/}
|
||||
<FastField name={'address'}>
|
||||
{({ form, field, meta: { error, touched } }) => (
|
||||
<FormGroup
|
||||
label={intl.get('warehouse.dialog.label.warehouse_address')}
|
||||
intent={inputIntent({ error, touched })}
|
||||
inline={true}
|
||||
helperText={<ErrorMessage name="address" />}
|
||||
className={'form-group--warehouse_address_1'}
|
||||
>
|
||||
<InputGroup
|
||||
intent={inputIntent({ error, touched })}
|
||||
placeholder={intl.get(
|
||||
'warehouse.dialog.label.warehouse_address_1',
|
||||
)}
|
||||
{...field}
|
||||
/>
|
||||
</FormGroup>
|
||||
)}
|
||||
</FastField>
|
||||
<WarehouseAddressWrap>
|
||||
{/*------------ Warehouse Address City & Country-----------*/}
|
||||
<FormGroup
|
||||
inline={true}
|
||||
className={'form-group--warehouse_address_city'}
|
||||
helperText={<ErrorMessage name="warehouse_address_city" />}
|
||||
>
|
||||
<ControlGroup>
|
||||
<FastField name={'city'}>
|
||||
{({ field, meta: { error, touched } }) => (
|
||||
<InputGroup
|
||||
intent={inputIntent({ error, touched })}
|
||||
placeholder={intl.get('warehouse.dialog.label.city')}
|
||||
{...field}
|
||||
/>
|
||||
)}
|
||||
</FastField>
|
||||
<FastField name={'country'}>
|
||||
{({ field, meta: { error, touched } }) => (
|
||||
<InputGroup
|
||||
intent={inputIntent({ error, touched })}
|
||||
placeholder={intl.get('warehouse.dialog.label.country')}
|
||||
{...field}
|
||||
/>
|
||||
)}
|
||||
</FastField>
|
||||
</ControlGroup>
|
||||
</FormGroup>
|
||||
</WarehouseAddressWrap>
|
||||
|
||||
{/*------------ Phone Number -----------*/}
|
||||
<FastField name={'phone_number'}>
|
||||
{({ form, field, meta: { error, touched } }) => (
|
||||
<FormGroup
|
||||
label={intl.get('warehouse.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('warehouse.dialog.label.email')}
|
||||
intent={inputIntent({ error, touched })}
|
||||
inline={true}
|
||||
helperText={<ErrorMessage name="email" />}
|
||||
className={'form-group--warehouse_name'}
|
||||
>
|
||||
<InputGroup intent={inputIntent({ error, touched })} {...field} />
|
||||
</FormGroup>
|
||||
)}
|
||||
</FastField>
|
||||
|
||||
{/*------------ Website -----------*/}
|
||||
<FastField name={'website'}>
|
||||
{({ form, field, meta: { error, touched } }) => (
|
||||
<FormGroup
|
||||
label={intl.get('warehouse.dialog.label.website')}
|
||||
intent={inputIntent({ error, touched })}
|
||||
inline={true}
|
||||
helperText={<ErrorMessage name="email" />}
|
||||
className={'form-group--warehouse_name'}
|
||||
>
|
||||
<InputGroup placeholder={'https://'} {...field} />
|
||||
</FormGroup>
|
||||
)}
|
||||
</FastField>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
const WarehouseAddressWrap = styled.div`
|
||||
padding-left: 150px;
|
||||
`;
|
||||
@@ -0,0 +1,49 @@
|
||||
// @ts-nocheck
|
||||
import React from 'react';
|
||||
|
||||
import { Intent, Button, Classes } from '@blueprintjs/core';
|
||||
import { useFormikContext } from 'formik';
|
||||
import { FormattedMessage as T } from '@/components';
|
||||
|
||||
import { useWarehouseFormContext } from './WarehouseFormProvider';
|
||||
import withDialogActions from '@/containers/Dialog/withDialogActions';
|
||||
import { compose } from '@/utils';
|
||||
|
||||
/**
|
||||
* Warehouse form floating actions.
|
||||
* @returns
|
||||
*/
|
||||
function WarehouseFormFloatingActions({
|
||||
// #withDialogActions
|
||||
closeDialog,
|
||||
}) {
|
||||
// Formik context.
|
||||
const { isSubmitting } = useFormikContext();
|
||||
|
||||
const { dialogName } = useWarehouseFormContext();
|
||||
|
||||
// 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)(WarehouseFormFloatingActions);
|
||||
@@ -0,0 +1,46 @@
|
||||
// @ts-nocheck
|
||||
import React from 'react';
|
||||
import { DialogContent } from '@/components';
|
||||
import {
|
||||
useCreateWarehouse,
|
||||
useEditWarehouse,
|
||||
useWarehouse,
|
||||
} from '@/hooks/query';
|
||||
|
||||
const WarehouseFormContext = React.createContext();
|
||||
|
||||
/**
|
||||
* Warehouse form provider.
|
||||
*/
|
||||
function WarehouseFormProvider({ dialogName, warehouseId, ...props }) {
|
||||
// Create and edit warehouse mutations.
|
||||
const { mutateAsync: createWarehouseMutate } = useCreateWarehouse();
|
||||
const { mutateAsync: editWarehouseMutate } = useEditWarehouse();
|
||||
|
||||
// Handle fetch invoice detail.
|
||||
const { data: warehouse, isLoading: isWarehouseLoading } = useWarehouse(
|
||||
warehouseId,
|
||||
{
|
||||
enabled: !!warehouseId,
|
||||
},
|
||||
);
|
||||
|
||||
// State provider.
|
||||
const provider = {
|
||||
dialogName,
|
||||
warehouse,
|
||||
warehouseId,
|
||||
createWarehouseMutate,
|
||||
editWarehouseMutate,
|
||||
};
|
||||
|
||||
return (
|
||||
<DialogContent isLoading={isWarehouseLoading}>
|
||||
<WarehouseFormContext.Provider value={provider} {...props} />
|
||||
</DialogContent>
|
||||
);
|
||||
}
|
||||
|
||||
const useWarehouseFormContext = () => React.useContext(WarehouseFormContext);
|
||||
|
||||
export { WarehouseFormProvider, useWarehouseFormContext };
|
||||
@@ -0,0 +1,44 @@
|
||||
// @ts-nocheck
|
||||
import React from 'react';
|
||||
import { Dialog, DialogSuspense, FormattedMessage as T } from '@/components';
|
||||
import withDialogRedux from '@/components/DialogReduxConnect';
|
||||
|
||||
import { compose } from '@/utils';
|
||||
|
||||
const WarehouseFormDialogContent = React.lazy(
|
||||
() => import('./WarehouseFormDialogContent'),
|
||||
);
|
||||
|
||||
/**
|
||||
* Warehouse form form dialog.
|
||||
*/
|
||||
function WarehouseFormDialog({
|
||||
dialogName,
|
||||
payload: { warehouseId = null, action },
|
||||
isOpen,
|
||||
}) {
|
||||
return (
|
||||
<Dialog
|
||||
name={dialogName}
|
||||
title={
|
||||
action == 'edit' ? (
|
||||
<T id={'warehouse.dialog.label.edit_warehouse'} />
|
||||
) : (
|
||||
<T id={'warehouse.dialog.label.new_warehouse'} />
|
||||
)
|
||||
}
|
||||
isOpen={isOpen}
|
||||
canEscapeJeyClose={true}
|
||||
autoFocus={true}
|
||||
className={'dialog--warehouse-form'}
|
||||
>
|
||||
<DialogSuspense>
|
||||
<WarehouseFormDialogContent
|
||||
dialogName={dialogName}
|
||||
warehouseId={warehouseId}
|
||||
/>
|
||||
</DialogSuspense>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
export default compose(withDialogRedux())(WarehouseFormDialog);
|
||||
@@ -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 === 'WAREHOUSE_CODE_NOT_UNIQUE')) {
|
||||
setErrors({
|
||||
code: intl.get('warehouse.error.warehouse_code_not_unique'),
|
||||
});
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user