diff --git a/common/app/redux/actions.js b/common/app/redux/actions.js index b89a2494af..5e9c5700f7 100644 --- a/common/app/redux/actions.js +++ b/common/app/redux/actions.js @@ -45,3 +45,12 @@ export const createErrorObserable = error => Observable.just({ type: types.handleError, error }); + +// challenges +// these need to be used by more than one route so we put them here +export const fetchChallenges = createAction(types.fetchChallenges); +export const fetchChallengesCompleted = createAction( + types.fetchChallengesCompleted, + (_, superBlocks) => superBlocks, + entities => ({ entities }) +); diff --git a/common/app/routes/map/redux/fetch-challenges-saga.js b/common/app/redux/fetch-challenges-saga.js similarity index 65% rename from common/app/routes/map/redux/fetch-challenges-saga.js rename to common/app/redux/fetch-challenges-saga.js index 7091033c28..450fea2daf 100644 --- a/common/app/routes/map/redux/fetch-challenges-saga.js +++ b/common/app/redux/fetch-challenges-saga.js @@ -1,8 +1,5 @@ -import { Observable } from 'rx'; import { fetchChallenges } from './types'; -import { fetchChallengesCompleted } from './actions'; - -import { handleError } from '../../../redux/types'; +import { createErrorObserable, fetchChallengesCompleted } from './actions'; export default function fetchChallengesSaga(action$, getState, { services }) { return action$ @@ -12,6 +9,6 @@ export default function fetchChallengesSaga(action$, getState, { services }) { .map(({ entities, result } = {}) => { return fetchChallengesCompleted(entities, result); }) - .catch(error => Observable.just({ type: handleError, error })); + .catch(createErrorObserable); }); } diff --git a/common/app/redux/index.js b/common/app/redux/index.js index d0e8381658..9cdbc9653d 100644 --- a/common/app/redux/index.js +++ b/common/app/redux/index.js @@ -3,4 +3,5 @@ export { default as actions } from './actions'; export { default as types } from './types'; import fetchUserSaga from './fetch-user-saga'; -export const sagas = [ fetchUserSaga ]; +import fetchChallengesSaga from './fetch-challenges-saga'; +export const sagas = [ fetchUserSaga, fetchChallengesSaga ]; diff --git a/common/app/redux/reducer.js b/common/app/redux/reducer.js index 6ef681c631..e95901abf5 100644 --- a/common/app/redux/reducer.js +++ b/common/app/redux/reducer.js @@ -30,6 +30,12 @@ export default handleActions( [types.updateNavHeight]: (state, { payload: navHeight }) => ({ ...state, navHeight + }), + + // challenges + [types.fetchChallengesCompleted]: (state, { payload = [] }) => ({ + ...state, + superBlocks: payload }) }, { @@ -40,6 +46,7 @@ export default handleActions( isSignedIn: false, csrfToken: '', windowHeight: 0, - navHeight: 0 + navHeight: 0, + superBlocks: [] } ); diff --git a/common/app/redux/types.js b/common/app/redux/types.js index 9860e941bc..523a5b3dd8 100644 --- a/common/app/redux/types.js +++ b/common/app/redux/types.js @@ -18,5 +18,10 @@ export default createTypes([ // data handling 'updateChallengesData', 'updateJobsData', - 'updateHikesData' + 'updateHikesData', + // challenges + + + 'fetchChallenges', + 'fetchChallengesCompleted' ], 'app'); diff --git a/common/app/routes/map/components/Show.jsx b/common/app/routes/map/components/Show.jsx index 05ba101889..12968aadb2 100644 --- a/common/app/routes/map/components/Show.jsx +++ b/common/app/routes/map/components/Show.jsx @@ -8,9 +8,9 @@ import { createSelector } from 'reselect'; import Map from './Map.jsx'; import { clearFilter, - fetchChallenges, updateFilter } from '../redux/actions'; +import { fetchChallenges } from '../../../redux/actions'; const bindableActions = { clearFilter, @@ -18,7 +18,7 @@ const bindableActions = { updateFilter }; const superBlocksSelector = createSelector( - state => state.map.superBlocks, + state => state.app.superBlocks, state => state.entities.superBlock, state => state.entities.block, state => state.entities.challenge, @@ -28,25 +28,27 @@ const superBlocksSelector = createSelector( superBlocks: [] }; } - return superBlocks - .map(superBlockName => superBlockMap[superBlockName]) - .map(superBlock => ({ - ...superBlock, - blocks: superBlock.blocks - .map(blockName => blockMap[blockName]) - .map(block => ({ - ...block, - challenges: block.challenges - .map(dashedName => challengeMap[dashedName]) - })) - })); + return { + superBlocks: superBlocks + .map(superBlockName => superBlockMap[superBlockName]) + .map(superBlock => ({ + ...superBlock, + blocks: superBlock.blocks + .map(blockName => blockMap[blockName]) + .map(block => ({ + ...block, + challenges: block.challenges + .map(dashedName => challengeMap[dashedName]) + })) + })) + }; } ); const mapStateToProps = createSelector( superBlocksSelector, state => state.map.filter, - (superBlocks, filter) => { + ({ superBlocks }, filter) => { return { superBlocks, filter diff --git a/common/app/routes/map/redux/actions.js b/common/app/routes/map/redux/actions.js index 0868fe4957..afbce1c479 100644 --- a/common/app/routes/map/redux/actions.js +++ b/common/app/routes/map/redux/actions.js @@ -1,14 +1,6 @@ import { createAction } from 'redux-actions'; import types from './types'; - -export const fetchChallenges = createAction(types.fetchChallenges); -export const fetchChallengesCompleted = createAction( - types.fetchChallengesCompleted, - (_, superBlocks) => superBlocks, - entities => ({ entities }) -); - export const updateFilter = createAction( types.updateFilter, e => e.target.value diff --git a/common/app/routes/map/redux/index.js b/common/app/routes/map/redux/index.js index 79b8c842e4..df7a505050 100644 --- a/common/app/routes/map/redux/index.js +++ b/common/app/routes/map/redux/index.js @@ -2,5 +2,4 @@ export actions from './actions'; export reducer from './reducer'; export types from './types'; -import fetchChallengesSaga from './fetch-challenges-saga'; -export const sagas = [ fetchChallengesSaga ]; +export const sagas = []; diff --git a/common/app/routes/map/redux/reducer.js b/common/app/routes/map/redux/reducer.js index c3f4179e4f..76a973e171 100644 --- a/common/app/routes/map/redux/reducer.js +++ b/common/app/routes/map/redux/reducer.js @@ -2,17 +2,9 @@ import { handleActions } from 'redux-actions'; import types from './types'; -const initialState = { - superBlocks: [], - filter: '' -}; - +const initialState = { filter: '' }; export default handleActions( { - [types.fetchChallengesCompleted]: (state, { payload = [] }) => ({ - ...state, - superBlocks: payload - }), [types.updateFilter]: (state, { payload = ''}) => ({ ...state, filter: payload diff --git a/common/app/routes/map/redux/types.js b/common/app/routes/map/redux/types.js index a6447dfe50..86dcb1721b 100644 --- a/common/app/routes/map/redux/types.js +++ b/common/app/routes/map/redux/types.js @@ -1,8 +1,6 @@ import createTypes from '../../../utils/create-types'; export default createTypes([ - 'fetchChallenges', - 'fetchChallengesCompleted', 'updateFilter', 'clearFilter' ], 'map');