Commit ceddb1c3 authored by Viacheslav Pavlov's avatar Viacheslav Pavlov

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) => {
export const deleteCrop = (crop: Crop) => (dispatch) => {
return CropService.deleteCrop(crop.shortName)
.then(() => {
dispatch(loadCrops());
dispatch(loadCrops(true));
dispatch(navigateTo('/c'));
});
};
import update from 'immutability-helper';
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 = {
crop: null,
cropNames: {},
};
export default function crop(state = INITIAL_STATE, action: IReducerAction = {type: ''}) {
......@@ -15,6 +16,15 @@ export default function crop(state = INITIAL_STATE, action: IReducerAction = {ty
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:
return state;
}
......
......@@ -19,9 +19,20 @@ export default function crop(state = INITIAL_STATE, action: IReducerAction = {ty
});
}
case ADMIN_RECEIVE_CROP: {
return update(state, {
details: {$set: null},
});
const updateIndex = state.list ? state.list.findIndex((crop) => crop.shortName === action.payload.shortName) : -1;
return updateIndex === -1 ?
update(state, {
details: {$set: null},
})
:
update(state, {
details: {$set: null},
list: {
[updateIndex]: {$set: action.payload},
},
});
}
case RECEIVE_CROP_DETAILS: {
return update(state, {
......
......@@ -17,6 +17,7 @@ import PageLayout, {PageContents} from 'ui/layout/PageLayout';
interface ICropEditPageProps extends React.ClassAttributes<any> {
classes: any;
shortName?: string;
cropNames: any;
loadCrop: (shortName: string) => void;
saveCrop: (crop: Crop) => Promise<any>;
deleteCrop: (crop: Crop) => void;
......@@ -34,6 +35,7 @@ class CropEditPage extends React.Component<ICropEditPageProps, any> {
saveCrop(updatedCrop)
.then(() => navigateTo(`/c/${updatedCrop.shortName}/edit`));
}
private onDelete = () => {
const {crop, deleteCrop} = this.props;
......@@ -57,7 +59,7 @@ class CropEditPage extends React.Component<ICropEditPageProps, any> {
}
public render() {
const {shortName} = this.props;
const {shortName, cropNames} = this.props;
let {crop} = this.props;
if (!crop && !shortName) {
......@@ -74,7 +76,7 @@ class CropEditPage extends React.Component<ICropEditPageProps, any> {
<PageContents>
<Grid item xs={ 12 }>
<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>
</Grid>
</PageContents>
......@@ -86,6 +88,7 @@ class CropEditPage extends React.Component<ICropEditPageProps, any> {
const mapStateToProps = (state, ownProps) => ({
shortName: ownProps.match.params.shortName,
cropNames: state.crop.admin.cropNames,
crop: state.crop.admin.crop,
});
......
......@@ -10,9 +10,6 @@ import Validators from 'utilities/Validators';
import ItemsEditor from 'ui/common/ItemsEditor';
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 = () => {
log('Adding new string item');
......@@ -23,11 +20,17 @@ const onRemoveString = (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 (
<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 required name="name" label="Crop title" component={ TextField } validate={ [Validators.required] }/>
......@@ -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({
enableReinitialize: true,
destroyOnUnmount: false,
validate,
form: CROP_FORM,
})(CropForm);
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