Move fetchChallenges up a level

This commit is contained in:
Berkeley Martinez
2016-05-06 16:04:23 -07:00
parent 861f89683b
commit 59dcabb588
10 changed files with 46 additions and 44 deletions

View File

@ -45,3 +45,12 @@ export const createErrorObserable = error => Observable.just({
type: types.handleError, type: types.handleError,
error 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 })
);

View File

@ -1,8 +1,5 @@
import { Observable } from 'rx';
import { fetchChallenges } from './types'; import { fetchChallenges } from './types';
import { fetchChallengesCompleted } from './actions'; import { createErrorObserable, fetchChallengesCompleted } from './actions';
import { handleError } from '../../../redux/types';
export default function fetchChallengesSaga(action$, getState, { services }) { export default function fetchChallengesSaga(action$, getState, { services }) {
return action$ return action$
@ -12,6 +9,6 @@ export default function fetchChallengesSaga(action$, getState, { services }) {
.map(({ entities, result } = {}) => { .map(({ entities, result } = {}) => {
return fetchChallengesCompleted(entities, result); return fetchChallengesCompleted(entities, result);
}) })
.catch(error => Observable.just({ type: handleError, error })); .catch(createErrorObserable);
}); });
} }

View File

@ -3,4 +3,5 @@ export { default as actions } from './actions';
export { default as types } from './types'; export { default as types } from './types';
import fetchUserSaga from './fetch-user-saga'; import fetchUserSaga from './fetch-user-saga';
export const sagas = [ fetchUserSaga ]; import fetchChallengesSaga from './fetch-challenges-saga';
export const sagas = [ fetchUserSaga, fetchChallengesSaga ];

View File

@ -30,6 +30,12 @@ export default handleActions(
[types.updateNavHeight]: (state, { payload: navHeight }) => ({ [types.updateNavHeight]: (state, { payload: navHeight }) => ({
...state, ...state,
navHeight navHeight
}),
// challenges
[types.fetchChallengesCompleted]: (state, { payload = [] }) => ({
...state,
superBlocks: payload
}) })
}, },
{ {
@ -40,6 +46,7 @@ export default handleActions(
isSignedIn: false, isSignedIn: false,
csrfToken: '', csrfToken: '',
windowHeight: 0, windowHeight: 0,
navHeight: 0 navHeight: 0,
superBlocks: []
} }
); );

View File

@ -18,5 +18,10 @@ export default createTypes([
// data handling // data handling
'updateChallengesData', 'updateChallengesData',
'updateJobsData', 'updateJobsData',
'updateHikesData' 'updateHikesData',
// challenges
'fetchChallenges',
'fetchChallengesCompleted'
], 'app'); ], 'app');

View File

@ -8,9 +8,9 @@ import { createSelector } from 'reselect';
import Map from './Map.jsx'; import Map from './Map.jsx';
import { import {
clearFilter, clearFilter,
fetchChallenges,
updateFilter updateFilter
} from '../redux/actions'; } from '../redux/actions';
import { fetchChallenges } from '../../../redux/actions';
const bindableActions = { const bindableActions = {
clearFilter, clearFilter,
@ -18,7 +18,7 @@ const bindableActions = {
updateFilter updateFilter
}; };
const superBlocksSelector = createSelector( const superBlocksSelector = createSelector(
state => state.map.superBlocks, state => state.app.superBlocks,
state => state.entities.superBlock, state => state.entities.superBlock,
state => state.entities.block, state => state.entities.block,
state => state.entities.challenge, state => state.entities.challenge,
@ -28,25 +28,27 @@ const superBlocksSelector = createSelector(
superBlocks: [] superBlocks: []
}; };
} }
return superBlocks return {
.map(superBlockName => superBlockMap[superBlockName]) superBlocks: superBlocks
.map(superBlock => ({ .map(superBlockName => superBlockMap[superBlockName])
...superBlock, .map(superBlock => ({
blocks: superBlock.blocks ...superBlock,
.map(blockName => blockMap[blockName]) blocks: superBlock.blocks
.map(block => ({ .map(blockName => blockMap[blockName])
...block, .map(block => ({
challenges: block.challenges ...block,
.map(dashedName => challengeMap[dashedName]) challenges: block.challenges
})) .map(dashedName => challengeMap[dashedName])
})); }))
}))
};
} }
); );
const mapStateToProps = createSelector( const mapStateToProps = createSelector(
superBlocksSelector, superBlocksSelector,
state => state.map.filter, state => state.map.filter,
(superBlocks, filter) => { ({ superBlocks }, filter) => {
return { return {
superBlocks, superBlocks,
filter filter

View File

@ -1,14 +1,6 @@
import { createAction } from 'redux-actions'; import { createAction } from 'redux-actions';
import types from './types'; import types from './types';
export const fetchChallenges = createAction(types.fetchChallenges);
export const fetchChallengesCompleted = createAction(
types.fetchChallengesCompleted,
(_, superBlocks) => superBlocks,
entities => ({ entities })
);
export const updateFilter = createAction( export const updateFilter = createAction(
types.updateFilter, types.updateFilter,
e => e.target.value e => e.target.value

View File

@ -2,5 +2,4 @@ export actions from './actions';
export reducer from './reducer'; export reducer from './reducer';
export types from './types'; export types from './types';
import fetchChallengesSaga from './fetch-challenges-saga'; export const sagas = [];
export const sagas = [ fetchChallengesSaga ];

View File

@ -2,17 +2,9 @@ import { handleActions } from 'redux-actions';
import types from './types'; import types from './types';
const initialState = { const initialState = { filter: '' };
superBlocks: [],
filter: ''
};
export default handleActions( export default handleActions(
{ {
[types.fetchChallengesCompleted]: (state, { payload = [] }) => ({
...state,
superBlocks: payload
}),
[types.updateFilter]: (state, { payload = ''}) => ({ [types.updateFilter]: (state, { payload = ''}) => ({
...state, ...state,
filter: payload filter: payload

View File

@ -1,8 +1,6 @@
import createTypes from '../../../utils/create-types'; import createTypes from '../../../utils/create-types';
export default createTypes([ export default createTypes([
'fetchChallenges',
'fetchChallengesCompleted',
'updateFilter', 'updateFilter',
'clearFilter' 'clearFilter'
], 'map'); ], 'map');