Commit ceddb1c3 authored by Viacheslav Pavlov's avatar Viacheslav Pavlov
Browse files

fixed onCropDelete, fixed adding crop otherNames (now form validating if otherName already exist)

parent 6dac9e2a
...@@ -24,7 +24,7 @@ export const saveCrop = (crop: Crop) => (dispatch) => { ...@@ -24,7 +24,7 @@ export const saveCrop = (crop: Crop) => (dispatch) => {
export const deleteCrop = (crop: Crop) => (dispatch) => { export const deleteCrop = (crop: Crop) => (dispatch) => {
return CropService.deleteCrop(crop.shortName) return CropService.deleteCrop(crop.shortName)
.then(() => { .then(() => {
dispatch(loadCrops()); dispatch(loadCrops(true));
dispatch(navigateTo('/c')); dispatch(navigateTo('/c'));
}); });
}; };
import update from 'immutability-helper'; import update from 'immutability-helper';
import { IReducerAction } from 'model/common.model'; import { IReducerAction } from 'model/common.model';
import {ADMIN_RECEIVE_CROP} from 'crop/constants'; import {ADMIN_RECEIVE_CROP, RECEIVE_CROPS} from 'crop/constants';
const INITIAL_STATE = { const INITIAL_STATE = {
crop: null, crop: null,
cropNames: {},
}; };
export default function crop(state = INITIAL_STATE, action: IReducerAction = {type: ''}) { export default function crop(state = INITIAL_STATE, action: IReducerAction = {type: ''}) {
...@@ -15,6 +16,15 @@ export default function crop(state = INITIAL_STATE, action: IReducerAction = {ty ...@@ -15,6 +16,15 @@ export default function crop(state = INITIAL_STATE, action: IReducerAction = {ty
crop: {$set: action.payload}, crop: {$set: action.payload},
}); });
} }
case RECEIVE_CROPS: {
const cropNames = {};
const crops = action.payload;
crops.map((crop) => cropNames[crop.shortName] = crop.otherNames);
return update(state, {
cropNames: {$set: cropNames},
});
}
default: default:
return state; return state;
} }
......
...@@ -19,9 +19,20 @@ export default function crop(state = INITIAL_STATE, action: IReducerAction = {ty ...@@ -19,9 +19,20 @@ export default function crop(state = INITIAL_STATE, action: IReducerAction = {ty
}); });
} }
case ADMIN_RECEIVE_CROP: { case ADMIN_RECEIVE_CROP: {
return update(state, { const updateIndex = state.list ? state.list.findIndex((crop) => crop.shortName === action.payload.shortName) : -1;
return updateIndex === -1 ?
update(state, {
details: {$set: null}, details: {$set: null},
})
:
update(state, {
details: {$set: null},
list: {
[updateIndex]: {$set: action.payload},
},
}); });
} }
case RECEIVE_CROP_DETAILS: { case RECEIVE_CROP_DETAILS: {
return update(state, { return update(state, {
......
...@@ -17,6 +17,7 @@ import PageLayout, {PageContents} from 'ui/layout/PageLayout'; ...@@ -17,6 +17,7 @@ import PageLayout, {PageContents} from 'ui/layout/PageLayout';
interface ICropEditPageProps extends React.ClassAttributes<any> { interface ICropEditPageProps extends React.ClassAttributes<any> {
classes: any; classes: any;
shortName?: string; shortName?: string;
cropNames: any;
loadCrop: (shortName: string) => void; loadCrop: (shortName: string) => void;
saveCrop: (crop: Crop) => Promise<any>; saveCrop: (crop: Crop) => Promise<any>;
deleteCrop: (crop: Crop) => void; deleteCrop: (crop: Crop) => void;
...@@ -34,6 +35,7 @@ class CropEditPage extends React.Component<ICropEditPageProps, any> { ...@@ -34,6 +35,7 @@ class CropEditPage extends React.Component<ICropEditPageProps, any> {
saveCrop(updatedCrop) saveCrop(updatedCrop)
.then(() => navigateTo(`/c/${updatedCrop.shortName}/edit`)); .then(() => navigateTo(`/c/${updatedCrop.shortName}/edit`));
} }
private onDelete = () => { private onDelete = () => {
const {crop, deleteCrop} = this.props; const {crop, deleteCrop} = this.props;
...@@ -57,7 +59,7 @@ class CropEditPage extends React.Component<ICropEditPageProps, any> { ...@@ -57,7 +59,7 @@ class CropEditPage extends React.Component<ICropEditPageProps, any> {
} }
public render() { public render() {
const {shortName} = this.props; const {shortName, cropNames} = this.props;
let {crop} = this.props; let {crop} = this.props;
if (!crop && !shortName) { if (!crop && !shortName) {
...@@ -74,7 +76,7 @@ class CropEditPage extends React.Component<ICropEditPageProps, any> { ...@@ -74,7 +76,7 @@ class CropEditPage extends React.Component<ICropEditPageProps, any> {
<PageContents> <PageContents>
<Grid item xs={ 12 }> <Grid item xs={ 12 }>
<Paper className="p-20 mb-10"> <Paper className="p-20 mb-10">
<CropForm initialValues={ crop } onSubmit={ this.onSave } onDelete={ this.onDelete }/> <CropForm initialValues={ crop } onSubmit={ this.onSave } onDelete={ this.onDelete } cropNames={ cropNames }/>
</Paper> </Paper>
</Grid> </Grid>
</PageContents> </PageContents>
...@@ -86,6 +88,7 @@ class CropEditPage extends React.Component<ICropEditPageProps, any> { ...@@ -86,6 +88,7 @@ class CropEditPage extends React.Component<ICropEditPageProps, any> {
const mapStateToProps = (state, ownProps) => ({ const mapStateToProps = (state, ownProps) => ({
shortName: ownProps.match.params.shortName, shortName: ownProps.match.params.shortName,
cropNames: state.crop.admin.cropNames,
crop: state.crop.admin.crop, crop: state.crop.admin.crop,
}); });
......
...@@ -10,9 +10,6 @@ import Validators from 'utilities/Validators'; ...@@ -10,9 +10,6 @@ import Validators from 'utilities/Validators';
import ItemsEditor from 'ui/common/ItemsEditor'; import ItemsEditor from 'ui/common/ItemsEditor';
import Divider from '@material-ui/core/Divider'; import Divider from '@material-ui/core/Divider';
const stringField = (member, index, fields, itemLabel) => (
<Field required name={ `${member}` } type="text" component={ TextField } label={ itemLabel } validate={ [Validators.required] }/>
);
const onAddString = () => { const onAddString = () => {
log('Adding new string item'); log('Adding new string item');
...@@ -23,11 +20,17 @@ const onRemoveString = (item) => { ...@@ -23,11 +20,17 @@ const onRemoveString = (item) => {
log('Removing string', item); log('Removing string', item);
}; };
const CropForm = ({error, handleSubmit, initialValues, onDelete}) => { const CropForm = ({error, handleSubmit, initialValues, onDelete, cropNames}) => {
const notInUse = (value) => !value || cropNames && Object.keys(cropNames).filter((cropShortName) => cropNames[cropShortName].indexOf(value) !== -1 && cropShortName !== initialValues.shortName).length === 0 ? undefined : 'Already in use';
const stringField = (otherName, index, fields, itemLabel) => (
<Field required name={ `${otherName}` } type="text" component={ TextField } label={ itemLabel } validate={ [Validators.required, notInUse] }/>
);
return ( return (
<form onSubmit={ handleSubmit }> <form onSubmit={ handleSubmit }>
{ initialValues && initialValues.version && <div>Version: { initialValues.version || 0 }</div> } { initialValues.version >= 0 ? <div>Version: { initialValues.version }</div> : null }
<Field disabled={ initialValues.shortName } required name="shortName" label="Crop code" component={ TextField } validate={ [Validators.required, Validators.maxLength20] }/> <Field disabled={ initialValues.shortName } required name="shortName" label="Crop code" component={ TextField } validate={ [Validators.required, Validators.maxLength20] }/>
<Field required name="name" label="Crop title" component={ TextField } validate={ [Validators.required] }/> <Field required name="name" label="Crop title" component={ TextField } validate={ [Validators.required] }/>
...@@ -48,8 +51,22 @@ const CropForm = ({error, handleSubmit, initialValues, onDelete}) => { ...@@ -48,8 +51,22 @@ const CropForm = ({error, handleSubmit, initialValues, onDelete}) => {
); );
}; };
const validate = (values) => {
if (values.otherNames) {
const otherNamesError = [];
values.otherNames.map((otherName, index, array) => {
if (array.indexOf(otherName) !== index) {
otherNamesError[index] = 'Duplicate';
}
});
return { otherNames : otherNamesError };
}
return {};
};
export default reduxForm({ export default reduxForm({
enableReinitialize: true, enableReinitialize: true,
destroyOnUnmount: false, destroyOnUnmount: false,
validate,
form: CROP_FORM, form: CROP_FORM,
})(CropForm); })(CropForm);
Supports Markdown
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