From bba4ecc13c5f971be89d8b776fe48163fb9ba4d4 Mon Sep 17 00:00:00 2001 From: Stuart Taylor Date: Wed, 23 Nov 2016 21:53:42 +0000 Subject: [PATCH] Refactor entities reducer for redux-actions update --- common/app/redux/actions.js | 2 +- common/app/redux/entities-reducer.js | 167 +++++++++++++-------------- 2 files changed, 82 insertions(+), 87 deletions(-) diff --git a/common/app/redux/actions.js b/common/app/redux/actions.js index c34b50206a..a4602bc899 100644 --- a/common/app/redux/actions.js +++ b/common/app/redux/actions.js @@ -93,7 +93,7 @@ export const updateUserEmail = createAction( // updateUserLang(username: String, lang: String) => Action export const updateUserLang = createAction( types.updateUserLang, - (username, lang) => ({ username, lang }) + (username, lang) => ({ username, languageTag: lang }) ); // updateUserChallenge( diff --git a/common/app/redux/entities-reducer.js b/common/app/redux/entities-reducer.js index af6d1046b5..9e3df2c6c4 100644 --- a/common/app/redux/entities-reducer.js +++ b/common/app/redux/entities-reducer.js @@ -1,6 +1,7 @@ +import { handleActions } from 'redux-actions'; + import types from './types'; -const { updateUserPoints } = types; const initialState = { superBlock: {}, block: {}, @@ -8,96 +9,90 @@ const initialState = { user: {} }; -// future refactor(berks): Several of the actions here are just updating props -// on the main user. These can be refactors into one response for all actions -export default function entities(state = initialState, action) { - const { - type, - payload: { email, username, points, flag, languageTag } = {} - } = action; +const userReducer = handleActions( + { + [types.updateUserPoints]: (state, { payload: { username, points } }) => ({ + ...state, + [username]: { + ...state[username], + points + } + }), + [types.updateUserFlag]: (state, { payload: { username, flag } }) => ({ + ...state, + [username]: { + ...state[username], + [flag]: !state[username][flag] + } + }), + [types.updateUserEmail]: (state, { payload: { username, email } }) => ({ + ...state, + [username]: { + ...state[username], + email + } + }), + [types.updateUserLang]: + ( + state, + { + payload: { username, languageTag } + } + ) => ({ + ...state, + [username]: { + ...state[username], + languageTag + } + }), + [types.updateMyCurrentChallenge]: + ( + state, + { + payload: { username, currentChallengeId } + } + ) => ({ + ...state, + [username]: { + ...state[username], + currentChallengeId + } + }), + [types.updateUserChallenge]: + ( + state, + { + payload: { username, challengeInfo } + } + ) => ({ + ...state, + [username]: { + ...state[username], + challengeMap: { + ...state[username].challengeMap, + [challengeInfo.id]: challengeInfo + } + } + }) + }, + initialState.user +); + +function metaReducer(state = initialState, action) { if (action.meta && action.meta.entities) { return { ...state, ...action.meta.entities }; } - if (type === updateUserPoints) { - return { - ...state, - user: { - ...state.user, - [username]: { - ...state.user[username], - points - } - } - }; - } - if (action.type === types.updateUserFlag) { - return { - ...state, - user: { - ...state.user, - [username]: { - ...state.user[username], - [flag]: !state.user[username][flag] - } - } - }; - } - if (action.type === types.updateUserEmail) { - return { - ...state, - user: { - ...state.user, - [username]: { - ...state.user[username], - email - } - } - }; - } - if (action.type === types.updateUserLang) { - return { - ...state, - user: { - ...state.user, - [username]: { - ...state.user[username], - languageTag - } - } - }; - } - - if (action.type === types.updateMyCurrentChallenge) { - return { - ...state, - user: { - ...state.user, - [username]: { - ...state.user[username], - currentChallengeId: action.payload.currentChallengeId - } - } - }; - } - - if (action.type === types.updateUserChallenge) { - const { challengeInfo } = action.payload; - return { - ...state, - user: { - ...state.user, - [username]: { - ...state.user[username], - challengeMap: { - ...state.user[username].challengeMap, - [challengeInfo.id]: challengeInfo - } - } - } - }; - } return state; } + +export default function entitiesReducer(state, action) { + const newState = metaReducer(state, action); + const user = userReducer(newState.user, action); + if (newState.user !== user) { + return { ...newState, user }; + } + return newState; +}