From f29545ef6cdf429e49462ba03cbd792b44507d68 Mon Sep 17 00:00:00 2001 From: Berkeley Martinez Date: Thu, 10 Mar 2016 17:21:46 -0800 Subject: [PATCH] Move to localize non ui data --- common/app/create-reducer.js | 2 ++ common/app/redux/actions.js | 6 ++++++ common/app/redux/entities-reducer.js | 15 +++++++++++++++ common/app/redux/types.js | 7 ++++++- common/app/routes/Hikes/components/Hikes.jsx | 6 +++--- common/app/routes/Hikes/components/Lecture.jsx | 2 +- common/app/routes/Hikes/redux/actions.js | 3 ++- .../app/routes/Hikes/redux/fetch-hikes-saga.js | 9 ++------- common/app/routes/Hikes/redux/reducer.js | 6 +----- common/app/routes/Hikes/redux/selectors.js | 2 +- common/app/routes/Hikes/redux/utils.js | 17 ++++++++--------- common/app/routes/Jobs/components/Jobs.jsx | 11 +++++------ common/app/routes/Jobs/components/Show.jsx | 6 +++--- common/app/routes/Jobs/redux/actions.js | 3 ++- common/app/routes/Jobs/redux/fetch-jobs-saga.js | 6 ++---- common/app/routes/Jobs/redux/reducer.js | 10 ++-------- 16 files changed, 61 insertions(+), 50 deletions(-) create mode 100644 common/app/redux/entities-reducer.js diff --git a/common/app/create-reducer.js b/common/app/create-reducer.js index 5a11edd93f9..e18719e8515 100644 --- a/common/app/create-reducer.js +++ b/common/app/create-reducer.js @@ -2,6 +2,7 @@ import { combineReducers } from 'redux'; import { reducer as formReducer } from 'redux-form'; import { reducer as app } from './redux'; +import entitieReducer from './redux/entities-reducer'; import { reducer as hikesApp } from './routes/Hikes/redux'; import { reducer as challengesApp } from './routes/challenges/redux'; import { @@ -12,6 +13,7 @@ import { export default function createReducer(sideReducers = {}) { return combineReducers({ ...sideReducers, + entities: entitieReducer, app, hikesApp, jobsApp, diff --git a/common/app/redux/actions.js b/common/app/redux/actions.js index a0958094618..f1af64754f7 100644 --- a/common/app/redux/actions.js +++ b/common/app/redux/actions.js @@ -33,3 +33,9 @@ export const hardGoTo = createAction(types.hardGoTo); export const updateWindowHeight = createAction(types.updateWindowHeight); export const updateNavHeight = createAction(types.updateNavHeight); + + +// data +export const updateChallengesData = createAction(types.updateChallengesData); +export const updateJobsData = createAction(types.updateJobsData); +export const updateHikesData = createAction(types.updateHikesData); diff --git a/common/app/redux/entities-reducer.js b/common/app/redux/entities-reducer.js new file mode 100644 index 00000000000..121eb7f047a --- /dev/null +++ b/common/app/redux/entities-reducer.js @@ -0,0 +1,15 @@ +const initialState = { + hike: {}, + challenge: {}, + job: {} +}; + +export default function dataReducer(state = initialState, action) { + if (action.meta && action.meta.entities) { + return { + ...state, + ...action.meta.entities + }; + } + return state; +} diff --git a/common/app/redux/types.js b/common/app/redux/types.js index 8c1349c6584..9860e941bc1 100644 --- a/common/app/redux/types.js +++ b/common/app/redux/types.js @@ -13,5 +13,10 @@ export default createTypes([ 'hardGoTo', 'updateWindowHeight', - 'updateNavHeight' + 'updateNavHeight', + + // data handling + 'updateChallengesData', + 'updateJobsData', + 'updateHikesData' ], 'app'); diff --git a/common/app/routes/Hikes/components/Hikes.jsx b/common/app/routes/Hikes/components/Hikes.jsx index 94e185e2b08..05f11669d3c 100644 --- a/common/app/routes/Hikes/components/Hikes.jsx +++ b/common/app/routes/Hikes/components/Hikes.jsx @@ -13,9 +13,9 @@ import contain from '../../../utils/professor-x'; // const log = debug('fcc:hikes'); const mapStateToProps = createSelector( - state => state.hikesApp.hikes.entities, - state => state.hikesApp.hikes.results, - (hikesMap, hikesByDashedName)=> { + state => state.entities.hike, + state => state.hikesApp.hikes, + (hikesMap, hikesByDashedName) => { if (!hikesMap || !hikesByDashedName) { return { hikes: [] }; } diff --git a/common/app/routes/Hikes/components/Lecture.jsx b/common/app/routes/Hikes/components/Lecture.jsx index ca7601ba635..e6d85643eda 100644 --- a/common/app/routes/Hikes/components/Lecture.jsx +++ b/common/app/routes/Hikes/components/Lecture.jsx @@ -42,7 +42,7 @@ export class Lecture extends React.Component { componentWillMount() { if (!this.props.id) { - this.props.hardGoTo('/map'); + // this.props.hardGoTo('/map'); } } diff --git a/common/app/routes/Hikes/redux/actions.js b/common/app/routes/Hikes/redux/actions.js index 9560ce43671..10d434f0687 100644 --- a/common/app/routes/Hikes/redux/actions.js +++ b/common/app/routes/Hikes/redux/actions.js @@ -13,7 +13,8 @@ export const fetchHikes = createAction(types.fetchHikes); // called within fetchHikesSaga export const fetchHikesCompleted = createAction( types.fetchHikesCompleted, - (hikes, currentHike) => ({ hikes, currentHike }) + (entities, hikes, currentHike) => ({ hikes, currentHike }), + entities => ({ entities }) ); export const resetHike = createAction(types.resetHike); diff --git a/common/app/routes/Hikes/redux/fetch-hikes-saga.js b/common/app/routes/Hikes/redux/fetch-hikes-saga.js index 2c926fe8813..e8da7de051e 100644 --- a/common/app/routes/Hikes/redux/fetch-hikes-saga.js +++ b/common/app/routes/Hikes/redux/fetch-hikes-saga.js @@ -25,14 +25,9 @@ export default ({ services }) => ({ dispatch }) => next => { { hikes: arrayOf(hike) } ); - hikes = { - entities: entities.hike, - results: result.hikes - }; + const currentHike = findCurrentHike(result.hikes, dashedName); - const currentHike = findCurrentHike(hikes, dashedName); - - return fetchHikesCompleted(hikes, currentHike); + return fetchHikesCompleted(entities, result.hikes, currentHike); }) .catch(error => { return Observable.just({ diff --git a/common/app/routes/Hikes/redux/reducer.js b/common/app/routes/Hikes/redux/reducer.js index c48094ce753..5a850643c62 100644 --- a/common/app/routes/Hikes/redux/reducer.js +++ b/common/app/routes/Hikes/redux/reducer.js @@ -3,10 +3,7 @@ import types from './types'; import { findNextHikeName } from './utils'; const initialState = { - hikes: { - results: [], - entities: {} - }, + hikes: [], // ui // hike dashedName currentHike: '', @@ -91,7 +88,6 @@ export default handleActions( [types.fetchHikesCompleted]: (state, { payload }) => { const { hikes, currentHike } = payload; - return { ...state, hikes, diff --git a/common/app/routes/Hikes/redux/selectors.js b/common/app/routes/Hikes/redux/selectors.js index f89651eadab..507a90e1c22 100644 --- a/common/app/routes/Hikes/redux/selectors.js +++ b/common/app/routes/Hikes/redux/selectors.js @@ -2,7 +2,7 @@ import { createSelector } from 'reselect'; export const getCurrentHike = createSelector( - state => state.hikesApp.hikes.entities, + state => state.entities.hike, state => state.hikesApp.currentHike, (hikesMap, currentHikeDashedName) => (hikesMap[currentHikeDashedName] || {}) ); diff --git a/common/app/routes/Hikes/redux/utils.js b/common/app/routes/Hikes/redux/utils.js index 2a50e2fb1e0..6b3f6f0fbe6 100644 --- a/common/app/routes/Hikes/redux/utils.js +++ b/common/app/routes/Hikes/redux/utils.js @@ -4,7 +4,7 @@ import _ from 'lodash'; const log = debug('fcc:hikes:utils'); function getFirstHike(hikes) { - return hikes.results[0]; + return hikes[0]; } // interface Hikes { @@ -26,7 +26,6 @@ export function findCurrentHike(hikes, dashedName) { const filterRegex = new RegExp(dashedName, 'i'); return hikes - .results .filter(dashedName => { return filterRegex.test(dashedName); }) @@ -43,23 +42,23 @@ export function getCurrentHike(hikes = {}, dashedName) { } // findNextHikeName( -// hikes: { results: String[] }, +// hikes: String[], // dashedName: String // ) => String -export function findNextHikeName({ results }, dashedName) { +export function findNextHikeName(hikes, dashedName) { if (!dashedName) { - log('find next hike no id provided'); - return results[0]; + log('find next hike no dashedName provided'); + return hikes[0]; } const currentIndex = _.findIndex( - results, + hikes, _dashedName => _dashedName === dashedName ); - if (currentIndex >= results.length) { + if (currentIndex >= hikes.length) { return ''; } - return results[currentIndex + 1]; + return hikes[currentIndex + 1]; } diff --git a/common/app/routes/Jobs/components/Jobs.jsx b/common/app/routes/Jobs/components/Jobs.jsx index a8480a3f3bf..3b65c5905bd 100644 --- a/common/app/routes/Jobs/components/Jobs.jsx +++ b/common/app/routes/Jobs/components/Jobs.jsx @@ -16,12 +16,11 @@ import { } from '../redux/actions'; const mapStateToProps = createSelector( - state => state.jobsApp.jobs.entities, - state => state.jobsApp.jobs.results, - state => state.jobsApp, - (jobsMap, jobsById) => { - return { jobs: jobsById.map(id => jobsMap[id]) }; - } + state => state.entities.job, + state => state.jobsApp.jobs, + (jobsMap, jobsById) => ({ + jobs: jobsById.map(id => jobsMap[id]) + }) ); const bindableActions = { diff --git a/common/app/routes/Jobs/components/Show.jsx b/common/app/routes/Jobs/components/Show.jsx index 410c7cfcf67..429e4765b13 100644 --- a/common/app/routes/Jobs/components/Show.jsx +++ b/common/app/routes/Jobs/components/Show.jsx @@ -60,12 +60,12 @@ function generateMessage( const mapStateToProps = createSelector( state => state.app, state => state.jobsApp.currentJob, - state => state.jobsApp.jobs.entities, - ({ username, isFrontEndCert, isBackEndCert }, currentJob, jobs) => ({ + state => state.entities.job, + ({ username, isFrontEndCert, isBackEndCert }, currentJob, jobMap) => ({ username, isFrontEndCert, isBackEndCert, - job: jobs[currentJob] || {} + job: jobMap[currentJob] || {} }) ); diff --git a/common/app/routes/Jobs/redux/actions.js b/common/app/routes/Jobs/redux/actions.js index e3a6f7a6de7..40a328f7574 100644 --- a/common/app/routes/Jobs/redux/actions.js +++ b/common/app/routes/Jobs/redux/actions.js @@ -5,7 +5,8 @@ import types from './types'; export const fetchJobs = createAction(types.fetchJobs); export const fetchJobsCompleted = createAction( types.fetchJobsCompleted, - (currentJob, jobs) => ({ currentJob, jobs }) + (_, currentJob, jobs) => ({ currentJob, jobs }), + entities => ({ entities }) ); export const findJob = createAction(types.findJob); diff --git a/common/app/routes/Jobs/redux/fetch-jobs-saga.js b/common/app/routes/Jobs/redux/fetch-jobs-saga.js index a959605fbfc..d843b18be98 100644 --- a/common/app/routes/Jobs/redux/fetch-jobs-saga.js +++ b/common/app/routes/Jobs/redux/fetch-jobs-saga.js @@ -32,11 +32,9 @@ export default ({ services }) => ({ dispatch }) => next => { return fetchJobsCompleted( + entities, result.jobs[0], - { - entities: entities.job, - results: result.jobs - } + result.jobs ); }) .catch(error => { diff --git a/common/app/routes/Jobs/redux/reducer.js b/common/app/routes/Jobs/redux/reducer.js index e227f9c2545..2a270b777b5 100644 --- a/common/app/routes/Jobs/redux/reducer.js +++ b/common/app/routes/Jobs/redux/reducer.js @@ -13,21 +13,15 @@ const initialState = { initialValues: {}, currentJob: '', newJob: {}, - jobs: { - entities: {}, - results: [] - } + jobs: [] }; export default handleActions( { [types.findJob]: (state, { payload: id }) => { - const currentJob = state.jobs.entities[id]; return { ...state, - currentJob: currentJob && currentJob.id ? - currentJob.id : - state.currentJob + currentJob: id }; }, [types.fetchJobsCompleted]: (state, { payload: { jobs, currentJob } }) => ({