Commit 1d6f90c8 authored by Matija Obreza's avatar Matija Obreza
Browse files

Merge branch '90-crop-details-edit-pages' into 'master'

Resolve "Crop details/edit pages"

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