Commit 370e1b82 authored by Viacheslav Pavlov's avatar Viacheslav Pavlov
Browse files

added create crop button

parent 51d5b1ed
......@@ -21,6 +21,11 @@ export const saveCrop = (crop: Crop) => (dispatch) => {
.then((crop) => dispatch(receiveCrop(crop)));
};
export const createCrop = () => (dispatch) => {
dispatch(receiveCrop(null));
return dispatch(navigateTo('/c/edit'));
};
export const deleteCrop = (crop: Crop) => (dispatch) => {
return CropService.deleteCrop(crop.shortName)
.then(() => {
......
import update from 'immutability-helper';
import { IReducerAction } from 'model/common.model';
import {ADMIN_RECEIVE_CROP, RECEIVE_CROPS} from 'crop/constants';
import {ADMIN_RECEIVE_CROP, RECEIVE_CROPS, DELETE_CROP} from 'crop/constants';
const INITIAL_STATE = {
crop: null,
......@@ -16,6 +16,11 @@ export default function crop(state = INITIAL_STATE, action: IReducerAction = {ty
crop: {$set: action.payload},
});
}
case DELETE_CROP: {
return update(state, {
crop: {$set: null},
});
}
case RECEIVE_CROPS: {
const cropNames = {};
const crops = action.payload;
......
......@@ -19,11 +19,12 @@ export default function crop(state = INITIAL_STATE, action: IReducerAction = {ty
});
}
case ADMIN_RECEIVE_CROP: {
const updateIndex = state.list ? state.list.findIndex((crop) => crop.shortName === action.payload.shortName) : -1;
const updateIndex = state.list && action.payload ? state.list.findIndex((crop) => crop.shortName === action.payload.shortName) : -1;
return updateIndex === -1 ?
update(state, {
details: {$set: null},
list: {$push: action.payload ? [action.payload] : []},
})
:
update(state, {
......
......@@ -2,6 +2,9 @@ import * as React from 'react';
import {connect} from 'react-redux';
import {bindActionCreators} from 'redux';
// Actions
import {createCrop} from 'crop/actions/admin';
// Models
import CropDetails from 'model/genesys/CropDetails';
......@@ -10,19 +13,31 @@ import PageLayout, { PageContents } from 'ui/layout/PageLayout';
import ContentHeader from 'ui/common/heading/ContentHeader';
import CropCard from 'crop/ui/c/CropCard';
import GridLayout from 'ui/layout/GridLayout';
import ContentHeaderWithButton from 'ui/common/heading/ContentHeaderWithButton';
import ActionButton from 'ui/common/buttons/ActionButton';
import Authorize from 'ui/common/authorized/Authorize';
interface IBrowsePageProps extends React.ClassAttributes<any> {
crops: CropDetails[];
createCrop: any;
}
class BrowsePage extends React.Component<IBrowsePageProps> {
private addNewCropHandle = () => {
const {createCrop} = this.props;
createCrop();
}
public render() {
const {crops} = this.props;
return (
<PageLayout>
<ContentHeader title="Crop list" subTitle="Genesys crops directory"/>
<Authorize role="ROLE_ADMINISTRATOR">
<ContentHeaderWithButton buttons={ <ActionButton title="Create crop" action={ this.addNewCropHandle } style={ {margin: '4px', padding: '0'} }/> }/>
</Authorize>
<PageContents>
<GridLayout>
{ crops && crops.map((crop) => <CropCard key={ crop.shortName } crop={ crop } compact/>) }
......@@ -38,7 +53,9 @@ const mapStateToProps = (state, ownProps) => ({
filterCode: ownProps.match.params.filterCode,
});
const mapDispatchToProps = (dispatch) => bindActionCreators({}, dispatch);
const mapDispatchToProps = (dispatch) => bindActionCreators({
createCrop,
}, dispatch);
export default connect(mapStateToProps, mapDispatchToProps)(BrowsePage);
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