Commit 62a42ad0 authored by Maksym Tishchenko's avatar Maksym Tishchenko
Browse files

Seed viability

parent f2a6f4d2
......@@ -21,8 +21,8 @@ import Site from '@gringlobal-ce/client/src/model/gringlobal/Site';
import { CodeValueDisplay } from 'common/CodeValue';
import PrettyDate from '@gringlobal-ce/client/ui/common/time/PrettyDate';
import { PrintSpecies } from 'common/Taxonomy';
import OrderForm from 'viability/ui/c/OrderForm';
import { showSnackbar } from '@gringlobal-ce/client/action/snackbar';
import OrderDialog from 'viability/ui/c/OrderDialog';
interface IOrderBrowsePageProps extends React.ClassAttributes<any>, WithTranslation {
showSnackbar: (snack: string) => void;
......@@ -107,8 +107,8 @@ class OrderBrowsePage extends React.Component<IOrderBrowsePageProps> {
}
private rowsToggled = (selectedRows: number[]) => {
const selectedIds = selectedRows.map((rowIndex) => [ this.scheduledInventories[rowIndex], null ])
this.setState({ selected: selectedIds });
const selected = selectedRows.map((rowIndex) => this.scheduledInventories[rowIndex])
this.setState({ selected });
}
......@@ -122,9 +122,10 @@ class OrderBrowsePage extends React.Component<IOrderBrowsePageProps> {
private handleSubmit = (data) => {
const { showSnackbar, t } = this.props
this.resetError();
data.inventories = data.inventories.reduce((result, [inv, rule]) => {
return { ...result, [inv.id]: rule.id }
data.inventories = data.inventories.reduce((result, inv) => {
return { ...result, [inv.id]: data.inventoryViabilityRule.id }
}, {})
data.inventoryViabilityRule = null
InventoryViabilityService.orderViabilityTest(data)
.then(() => {
this.setState({ orderDialogIsOpen: false, selected: [] }); // close dialog and unselect items
......@@ -156,7 +157,6 @@ class OrderBrowsePage extends React.Component<IOrderBrowsePageProps> {
}
this.scheduledInventories = scheduledActions.content.map((action) => action.inventory);
// const initialValues = { inventories: new Map(selected.reduce((arr, selectedInv) => [selectedInv.id, null], [])) }
return (
<>
......@@ -188,16 +188,13 @@ class OrderBrowsePage extends React.Component<IOrderBrowsePageProps> {
onRowsToggled={ this.rowsToggled }
sort={ scheduledActions.sort }
/>
<OrderForm
<OrderDialog
isOpen={ orderDialogIsOpen }
onClose={ this.closeOrderDialog }
onSubmit={ this.handleSubmit }
error={ error }
formId='viability-order-form'
resetError={ this.resetError }
title={ t('viability.public.p.order.label.prepareOrder') }
initialValues={ { inventories: selected } }
submitButtonLabel={ t('viability.public.p.order.label.createOrder') }
inventories={ selected }
/>
</>
);
......
import * as React from 'react';
import { WithTranslation, withTranslation } from 'react-i18next';
import { compose } from 'redux';
// model
import { Inventory } from '@gringlobal-ce/client/model/gringlobal';
import Dialog from '@material-ui/core/Dialog';
import DialogContent from '@material-ui/core/DialogContent';
import DialogTitle from '@material-ui/core/DialogTitle';
import { CooperatorOwnedTableConfiguration as TableConfiguration } from '@gringlobal-ce/client/ui/common/table/TableConfiguration';
import { InventoryTableDefaultConfig } from 'inventory/ui/InventoryBrowsePage';
import { WithStyles, withStyles } from '@material-ui/core/styles';
import { Button, DialogActions } from '@material-ui/core';
import Table from '@gringlobal-ce/client/ui/common/table/Table';
import OrderForm from 'viability/ui/c/OrderForm';
const styles = (theme) => ({
dialogContent: {
display: 'flex' as const,
flexDirection: 'column' as const,
height: '100vh',
},
form: {
height: '50%',
width: '100%',
},
tableWrapper: {
height: '50%',
width: '100%',
overflowY: 'auto' as const,
},
dialogActions: {
padding: '1rem 1.5rem',
display: 'flex' as const,
justifyContent: 'flex-start' as const,
},
});
interface IOrderDialogProps extends React.ClassAttributes<any>, WithTranslation, WithStyles {
onSubmit: (data) => void;
inventories?: Inventory[];
isOpen: boolean;
onClose: () => void;
resetError?: () => void;
error: string
}
const InventoryTableConfig = new TableConfiguration(InventoryTableDefaultConfig);
class OrderDialog extends React.Component <IOrderDialogProps, any> {
public componentDidUpdate(prevProps: Readonly<IOrderDialogProps>) {
const { isOpen, resetError } = this.props;
if (resetError && isOpen === true && prevProps.isOpen === false) {
resetError();
return;
}
}
public componentWillUnmount() {
this.props.onClose();
}
private handleSubmit = (formData) => {
const { onSubmit, inventories } = this.props;
onSubmit({ ...formData, inventories })
}
public render() {
const { isOpen, t, classes, onClose, inventories, error } = this.props;
const inventoryColumns = InventoryTableConfig.getColumns(inventories ? inventories[0] : null);
return (
<div>
{ isOpen &&
<Dialog
open={ isOpen }
onClose={ onClose }
fullWidth
maxWidth={ false }
disableEnforceFocus
>
<DialogTitle>{ t('viability.public.p.order.label.prepareOrder') }</DialogTitle>
<DialogContent className={ classes.dialogContent }>
<div className={ classes.form }>
<OrderForm
onSubmit={ this.handleSubmit }
error={ error }
/>
</div>
<div className={ classes.tableWrapper }>
<Table
tableKey="inventory-list"
type={ 'Inventory' }
columns={ inventoryColumns }
data={ inventories }
tableConfig={ InventoryTableConfig }
total={ inventories.length }
/>
</div>
</DialogContent>
<DialogActions className={ classes.dialogActions }>
<Button
variant="contained"
color="primary"
type="submit"
form="viability-order-form"
>
{ t('viability.public.p.order.label.createOrder') }
</Button>
<Button variant="contained" type="button" onClick={ onClose }>{ t('common:action.cancel') }</Button>
</DialogActions>
</Dialog>
}
</div>
);
}
}
export default compose(
withStyles(styles),
withTranslation(),
)(OrderDialog) as React.ComponentClass<any>;
......@@ -4,12 +4,10 @@ import { withStyles, WithStyles, createStyles } from '@material-ui/core/styles';
import { withTranslation, WithTranslation } from 'react-i18next';
// UI
import { Grid } from '@material-ui/core';
import withDialog from 'ui/common/withDialog';
import { required } from '@gringlobal-ce/client/utilities/validators';
import SiteField from 'common/SiteField';
import { CooperatorAutocomplete } from 'common/Cooperator';
import InventoryViabilityRuleField from 'common/InventoryViabilityRuleField';
import { TextField } from '@gringlobal-ce/client/ui/common/form/TextField';
const styles = (theme) => createStyles({
textField: {
......@@ -24,35 +22,19 @@ const OrderForm = ({ t, onSubmit, initialValues, error }: FormProps & WithTransl
{ (props: FormRenderProps & WithStyles) => (
<form onSubmit={ props.handleSubmit } id="viability-order-form">
<Grid container spacing={ 4 }>
{
initialValues.inventories.map(([inventory], index) =>
<React.Fragment key={ `wrap-${inventory.id}` }>
<Grid item xs={ 12 } sm={ 6 }>
<Field
placeholder={ t('client:model._.inventory') }
name={ `inventories[${index}][0].inventoryNumber` }
type="text"
component={ TextField }
label={ t('client:model._.inventory') }
disabled
/>
</Grid>
<Grid item xs={ 12 } sm={ 6 }>
<Field
placeholder={ t('client:model.InventoryViability.inventoryViabilityRule') }
name={ `inventories[${index}][1]` }
type="text"
component={ InventoryViabilityRuleField }
label={ t('client:model.InventoryViability.inventoryViabilityRule') }
required
validate={ required }
/>
</Grid>
</React.Fragment>
)
}
<Grid item xs={ 12 } sm={ 6 } md={ 4 }>
<Field
placeholder={ t('client:model.InventoryViability.inventoryViabilityRule') }
name="inventoryViabilityRule"
type="text"
component={ InventoryViabilityRuleField }
label={ t('client:model.InventoryViability.inventoryViabilityRule') }
required
validate={ required }
/>
</Grid>
<Grid item xs={ 12 } sm={ 6 }>
<Grid item xs={ 12 } sm={ 6 } md={ 4 }>
<Field
name="site"
label={ t('client:model.ViabilityOrderRequest.site') }
......@@ -63,7 +45,7 @@ const OrderForm = ({ t, onSubmit, initialValues, error }: FormProps & WithTransl
validate={ required }
/>
</Grid>
<Grid item xs={ 12 } sm={ 6 }>
<Grid item xs={ 12 } sm={ 6 } md={ 4 }>
<Field
placeholder={ t('client:model.ViabilityOrderRequest.cooperator') }
name="cooperator"
......@@ -79,4 +61,4 @@ const OrderForm = ({ t, onSubmit, initialValues, error }: FormProps & WithTransl
)}
</Form>
export default withDialog(withStyles(styles)(withTranslation()(OrderForm)));
export default withStyles(styles)(withTranslation()(OrderForm));
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment