feat(warehouse): add credit & edit & delete warehouse.

This commit is contained in:
elforjani13
2022-02-01 21:05:54 +02:00
committed by a.bouhuolia
parent 8be3f23d65
commit 28a1cdfa3e
19 changed files with 524 additions and 203 deletions

View File

@@ -1,20 +1,26 @@
import React from 'react';
import { Formik } from 'formik';
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 withDialogActions from 'containers/Dialog/withDialogActions';
import { compose } from 'utils';
import { compose, transformToForm } from 'utils';
const defaultInitialValues = {
warehouse_name: '',
warehouse_address_1: '',
warehouse_address_2: '',
warehouse_address_city: '',
warehouse_address_country: '',
name: '',
code: '',
address: '',
city: '',
country: '',
phone_number: '',
website: '',
email: '',
};
/**
@@ -25,13 +31,50 @@ 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 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) {
}
setSubmitting(false);
};
if (warehouseId) {
editWarehouseMutate([warehouseId, form]).then(onSuccess).catch(onError);
} else {
createWarehouseMutate(form).then(onSuccess).catch(onError);
}
};
return (
<Formik

View File

@@ -1,13 +1,17 @@
import * as Yup from 'yup';
import intl from 'react-intl-universal';
import { DATATYPES_LENGTH } from 'common/dataTypes';
const Schema = Yup.object().shape({
warehouse_name: Yup.string().required().label(intl.get('warehouse_name')),
warehouse_address_1: Yup.string().trim(),
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(),
warehouse_address_city: Yup.string().trim(),
warehouse_address_country: 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;

View File

@@ -10,9 +10,10 @@ import WarehouseForm from './WarehouseForm';
export default function WarehouseFormDialogContent({
// #ownProps
dialogName,
warehouseId,
}) {
return (
<WarehouseFormProvider dialogName={dialogName}>
<WarehouseFormProvider warehouseId={warehouseId} dialogName={dialogName}>
<WarehouseForm />
</WarehouseFormProvider>
);

View File

@@ -22,28 +22,44 @@ export default function WarehouseFormFields() {
return (
<div className={Classes.DIALOG_BODY}>
{/*------------ Warehouse Name -----------*/}
<FastField name={'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="warehouse_name" />}
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={'warehouse_address_1'}>
<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="warehouse_address_1" />}
helperText={<ErrorMessage name="address" />}
className={'form-group--warehouse_address_1'}
>
<InputGroup
@@ -57,24 +73,6 @@ export default function WarehouseFormFields() {
)}
</FastField>
<WarehouseAddressWrap>
<FastField name={'warehouse_address_2'}>
{({ form, field, meta: { error, touched } }) => (
<FormGroup
intent={inputIntent({ error, touched })}
inline={true}
helperText={<ErrorMessage name="warehouse_address_2" />}
className={'form-group--warehouse_address_2'}
>
<InputGroup
intent={inputIntent({ error, touched })}
placeholder={intl.get(
'warehouse.dialog.label.warehouse_address_2',
)}
{...field}
/>
</FormGroup>
)}
</FastField>
{/*------------ Warehouse Address City & Country-----------*/}
<FormGroup
inline={true}
@@ -82,7 +80,7 @@ export default function WarehouseFormFields() {
helperText={<ErrorMessage name="warehouse_address_city" />}
>
<ControlGroup>
<FastField name={'warehouse_address_city'}>
<FastField name={'city'}>
{({ field, meta: { error, touched } }) => (
<InputGroup
intent={inputIntent({ error, touched })}
@@ -91,7 +89,7 @@ export default function WarehouseFormFields() {
/>
)}
</FastField>
<FastField name={'warehouse_address_country'}>
<FastField name={'country'}>
{({ field, meta: { error, touched } }) => (
<InputGroup
intent={inputIntent({ error, touched })}
@@ -103,6 +101,7 @@ export default function WarehouseFormFields() {
</ControlGroup>
</FormGroup>
</WarehouseAddressWrap>
{/*------------ Phone Number -----------*/}
<FastField name={'phone_number'}>
{({ form, field, meta: { error, touched } }) => (
@@ -117,6 +116,36 @@ export default function WarehouseFormFields() {
</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>
);
}

View File

@@ -1,22 +1,44 @@
import React from 'react';
import { DialogContent } from 'components';
// import {} from 'hooks/query';
import {
useCreateWarehouse,
useEditWarehouse,
useWarehouse,
} from 'hooks/query';
import { useLocation } from 'react-router-dom';
const WarehouseFormContext = React.createContext();
/**
* Warehouse form provider.
*/
function WarehouseFormProvider({ dialogName, ...props }) {
function WarehouseFormProvider({ dialogName, warehouseId, ...props }) {
const { state } = useLocation();
console.log(state, 'XXX');
// 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={}
>
<DialogContent isLoading={isWarehouseLoading}>
<WarehouseFormContext.Provider value={provider} {...props} />
</DialogContent>
);

View File

@@ -12,18 +12,31 @@ const WarehouseFormDialogContent = React.lazy(() =>
/**
* Warehouse form form dialog.
*/
function WarehouseFormDialog({ dialogName, isOpen }) {
function WarehouseFormDialog({
dialogName,
payload: { warehouseId = null, action },
isOpen,
}) {
return (
<Dialog
name={dialogName}
title={<T id={'warehouse.dialog.label'} />}
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} />
<WarehouseFormDialogContent
dialogName={dialogName}
warehouseId={warehouseId}
/>
</DialogSuspense>
</Dialog>
);