Skip to content

Commit

Permalink
Merge pull request #26 from mkozhukharenko/ormus
Browse files Browse the repository at this point in the history
Added interrupts + reselect
  • Loading branch information
mkozhukharenko committed Mar 30, 2016
2 parents 74a3833 + 6c05f97 commit 3fc5193
Show file tree
Hide file tree
Showing 16 changed files with 134 additions and 62 deletions.
1 change: 1 addition & 0 deletions src/modules/dictionaries/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default function loadDictionaries(listOfDict) {
},
params
},
interrupt: (store) => !!store.getState().dictionaries[dicName].resources.length,

payload: {
collectionName: dicName
Expand Down
18 changes: 15 additions & 3 deletions src/modules/dictionaries/reducer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
import {createDataMap} from './helpers';
import * as types from './constants';

export default function dictionaries(state = {}, action = {}) {
const dictInitialState = {
isLoading: false,
resources: [],
resourcesMap: [],
error: null
};

const disctionariesDefaultState = {
[types.DEPARTMENTS]: dictInitialState,
[types.ENROLMENTS_TYPES]: dictInitialState,
[types.ENROLMENTS_STATUS_TYPES]: dictInitialState,
[types.TIMEPERIODS]: dictInitialState
};

export default function dictionaries(state = disctionariesDefaultState, action = {}) {
switch (action.type) {

case types.DICTIONARY_LOAD_START:
Expand All @@ -13,7 +27,6 @@ export default function dictionaries(state = {}, action = {}) {
return Object.assign({}, state, {
[action.payload.collectionName]: {
isLoading: false,
loaded: true,
resources: action.response.resources,
resourcesMap: createDataMap(action.response.resources)
}
Expand All @@ -23,7 +36,6 @@ export default function dictionaries(state = {}, action = {}) {
return Object.assign({}, state, {
[action.payload.collectionName]: {
isLoading: false,
loaded: false,
error: action.error.message
}
});
Expand Down
4 changes: 3 additions & 1 deletion src/modules/enrolments/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export function loadEnrolments(params) {
},
params,
cache: true
}
},
interrupt: (store) => !!store.getState().enrolments.list.resources.length
};
}

Expand All @@ -30,6 +31,7 @@ export function loadEnrolmentById(id) {
},
cache: true
},
interrupt: (store) => !!store.getState().enrolments.view.mainInfo.data[id],
payload: {
id: id
}
Expand Down
2 changes: 2 additions & 0 deletions src/modules/enrolments/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ export function isDataForOneEnrolmentLoaded(reducerName, params = {}) {
}

export function decodeOneEnrolment(item, dictionaries) {
if (!item) return {};

let {DEPARTMENTS, ENROLMENTS_TYPES, ENROLMENTS_STATUS_TYPES} = dictionaries;
let {
isStateNames,
Expand Down
29 changes: 19 additions & 10 deletions src/modules/enrolments/list/EnrolmentsListPage.jsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import React, {Component, PropTypes} from 'react';
import {connect} from 'react-redux';
import { createSelector } from 'reselect';

import * as dictConst from '../../dictionaries/constants';
import {loadEnrolments} from './../actions';
import loadDictionaries from '../../dictionaries/actions';
import {isDataForEnrolmentLoaded, decodeEnrolments} from '../helpers';
import Table from 'react-bootstrap/lib/Table';
import EnrolmentItem from './EnrolmentItem';
import Loading from '../../commons/Loading';
import Loading from 'loading';
import {ENROLMENT_LIST_REDUCER} from './../constants';

class EnrolmentsListPage extends Component {
Expand All @@ -26,8 +27,7 @@ class EnrolmentsListPage extends Component {
return <Loading/>;
}

let {enrolmentList, dictionaries} = this.props;
let enrolments = decodeEnrolments(enrolmentList, dictionaries).map((item)=> {
let enrolments = this.props.decodedEnrolments.map((item)=> {
return <EnrolmentItem item={item} key={item.id}/>;
});

Expand All @@ -51,11 +51,20 @@ class EnrolmentsListPage extends Component {
}
}

const select = (state)=> {
return {
enrolmentList: state.enrolments.list,
dictionaries: state.dictionaries
};
};
// const select = (state)=> {
// return {
// enrolmentList: state.enrolments.list,
// dictionaries: state.dictionaries
// };
// };

export default connect(select, {loadEnrolments, loadDictionaries})(EnrolmentsListPage);
export const getDecodedEnrolments = createSelector(
[ (state) => state.enrolments.list,
(state) => state.dictionaries],
(enrolmentList, listOfDict) => ({
decodedEnrolments: decodeEnrolments(enrolmentList, listOfDict),
enrolmentList: enrolmentList
})
)

export default connect(getDecodedEnrolments, {loadEnrolments, loadDictionaries})(EnrolmentsListPage);
48 changes: 31 additions & 17 deletions src/modules/enrolments/view/MainInfo.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,26 @@ import Table from 'react-bootstrap/lib/Table';
import {loadEnrolmentById} from './../actions';
import loadDictionaries from '../../dictionaries/actions';
import {ENROLMENT_MAININFO_REDUCER} from './../constants';
import Loading from '../../commons/Loading';
import Loading from 'loading';
import {DEPARTMENTS, ENROLMENTS_TYPES, ENROLMENTS_STATUS_TYPES} from '../../dictionaries/constants';
import { createSelector } from 'reselect';

export default class MainInfo extends Component {
static propTypes = {
decodedEnrolment: PropTypes.arrayOf(React.PropTypes.object).isRequired,
id: PropTypes.string.isRequired
};

componentDidMount() {
this.props.dispatch(loadDictionaries([DEPARTMENTS, ENROLMENTS_TYPES, ENROLMENTS_STATUS_TYPES]));
this.props.dispatch(loadEnrolmentById(this.props.params.id));
}

render() {
if (!isDataForOneEnrolmentLoaded(ENROLMENT_MAININFO_REDUCER, {'enrolId': this.props.params.id})) {
let { isDataLoaded, decodedEnrolment } = this.props;
if (!isDataLoaded) {
return <Loading/>;
} else {
let item = decodeOneEnrolment(this.props.mainInfo.data[this.props.params.id], this.props.dictionaries);

return (
<Table striped bordered condensed hover>
<thead>
Expand All @@ -32,27 +36,27 @@ export default class MainInfo extends Component {
<tbody>
<tr>
<td>id</td>
<td>{item.id}</td>
<td>{decodedEnrolment.id}</td>
</tr>
<tr>
<td>docSeries</td>
<td>{item.docSeries}</td>
<td>{decodedEnrolment.docSeries}</td>
</tr>
<tr>
<td>isInterview</td>
<td>{item.isInterview}</td>
<td>{decodedEnrolment.isInterview}</td>
</tr>
<tr>
<td>isState</td>
<td>{item.isState}</td>
<td>{decodedEnrolment.isState}</td>
</tr>
<tr>
<td>departmentId</td>
<td>{item.departmentId}</td>
<td>{decodedEnrolment.departmentId}</td>
</tr>
<tr>
<td>enrolmentTypeId</td>
<td>{item.enrolmentTypeId}</td>
<td>{decodedEnrolment.enrolmentTypeId}</td>
</tr>
</tbody>
</Table>
Expand All @@ -65,11 +69,21 @@ MainInfo.propTypes = {
dispatch: PropTypes.func.isRequired
};

const select = (state)=> {
return {
mainInfo: state.enrolments.view.mainInfo,
dictionaries: state.dictionaries
};
};
// const select = (state)=> {
// return {
// mainInfo: state.enrolments.view.mainInfo,
// dictionaries: state.dictionaries
// };
// };

export const getOneDecodedEnrolment = createSelector(
[ (state) => state.enrolments.view.mainInfo,
(state) => state.dictionaries,
(state, ownProps) => ownProps.params.id],
(mainInfo, listOfDict, enrolId) => ({
decodedEnrolment: decodeOneEnrolment(mainInfo.data[enrolId], listOfDict),
isDataLoaded: isDataForOneEnrolmentLoaded(ENROLMENT_MAININFO_REDUCER, {'enrolId': enrolId})
})
)

export default connect(select)(MainInfo);
export default connect(getOneDecodedEnrolment)(MainInfo);
3 changes: 0 additions & 3 deletions src/modules/rating/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,5 @@ export const LOAD_RATING_START = 'LOAD_RATING_START';
export const LOAD_RATING_SUCCESS= 'LOAD_RATING_SUCCESS';
export const LOAD_RATING_FAIL = 'LOAD_RATING_FAIL';

export const DEPARTMENT_PICKED = 'DEPARTMENT_PICKED';
export const SPECOFFER_PICKED = 'SPECOFFER_PICKED';

// reducer name
export const RATING = 'rating';
3 changes: 2 additions & 1 deletion src/modules/rating/container/SpecofferChooser.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import { createSelector } from 'reselect';

class SpecofferChooser extends Component {
static propTypes = {
specofferChooserData: PropTypes.arrayOf(React.PropTypes.object).isRequired,
departments: PropTypes.arrayOf(React.PropTypes.object).isRequired,
specoffers: PropTypes.object.isRequired,
departmentId: PropTypes.string,
specofferId: PropTypes.string
};
Expand Down
10 changes: 10 additions & 0 deletions src/modules/rating/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import {combineReducers} from 'redux';
import {LOCATION_CHANGE} from 'react-router-redux';
import { ignoreActions } from 'redux-ignore';

import { TIMEPERIODID_CHANGED } from '../settings/widget';

/**
* lost of departments and specoffers to choose
*/
Expand Down Expand Up @@ -31,6 +33,14 @@ export function specofferChooser(state = specofferChooserInitialState, action =
case types.LOAD_SPECOFFER_CHOOSER_FAIL:
return Object.assign({}, state, {isLoading: false, error: action.error.message});

case TIMEPERIODID_CHANGED:
return Object.assign({}, state,
{
isLoading: false,
resources: []
}
);

case LOCATION_CHANGE: // listen to query parameters changes
//if (action.payload.pathname !== '/rating') return state;
console.log('LOCATION_CHANGE', action);
Expand Down
33 changes: 21 additions & 12 deletions src/modules/settings/containers/timePeriodId.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
import React, {Component, PropTypes} from 'react';
import {connect} from 'react-redux';
import Input from 'react-bootstrap/lib/Input';
import { createSelector } from 'reselect';

import {TIMEPERIODS} from './../../dictionaries/constants';
import {isDictLoaded} from './../../dictionaries/helpers';
import loadDictionaries from './../../dictionaries/actions';
import {changeTimePeriodId} from './../widget';


class TimePeriodId extends Component {

constructor(props) {
Expand All @@ -25,13 +26,12 @@ class TimePeriodId extends Component {

render() {

if (!isDictLoaded([TIMEPERIODS], this.props.dictionaries)) {
let { isDictLoadedProp, timeperiods, timePeriodId } = this.props;

if (!isDictLoadedProp) {
return <div>loading...</div>;
}

let {dictionaries, timePeriodId} = this.props;
let timeperiods = dictionaries[TIMEPERIODS].resourcesMap;

const optionList = timeperiods.map((item, i) => {
return <option value={i} key={i}>{item}</option>;
});
Expand All @@ -52,13 +52,22 @@ TimePeriodId.propTypes = {
children: PropTypes.any
};

const mapStateToSettings = (state) => {
console.log('state', state);
return {
dictionaries: state.dictionaries,
timePeriodId: state.settings.timePeriodId
};
};
// const mapStateToSettings = (state) => {
// return {
// dictionaries: state.dictionaries,
// timePeriodId: state.settings.timePeriodId
// };
// };

export const mapStateToSettings = createSelector(
[ (state) => state.dictionaries,
(state) => state.settings.timePeriodId],
(dictionaries, timePeriodId) => ({
isDictLoadedProp: isDictLoaded([TIMEPERIODS], dictionaries),
timeperiods: dictionaries[TIMEPERIODS].resourcesMap,
timePeriodId: timePeriodId
})
)

const mapDispatchToSettings = (dispatch) => {
return {
Expand Down
3 changes: 2 additions & 1 deletion src/modules/statistics/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export function loadStatistics(url, collectionName) {
},
payload: {
collectionName
}
},
interrupt: (store) => !!store.getState().statistics[collectionName].data.length
};
}
1 change: 1 addition & 0 deletions src/modules/statistics/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export const STATISTICS_MAP = {
[ALLOCATIONS_ADMINUNITS_MAP]: {
callApi: {
url: '/stats/{{timePeriodId}}/entrants/allocations/adminunits',
collectionName: ALLOCATIONS_ADMINUNITS,
params: {},
cache: true,
cacheTime: 300
Expand Down
Loading

0 comments on commit 3fc5193

Please sign in to comment.