Refactor entities reducer for redux-actions update

This commit is contained in:
Stuart Taylor
2016-11-23 21:53:42 +00:00
parent 5b14f1631b
commit bba4ecc13c
2 changed files with 82 additions and 87 deletions

View File

@ -93,7 +93,7 @@ export const updateUserEmail = createAction(
// updateUserLang(username: String, lang: String) => Action // updateUserLang(username: String, lang: String) => Action
export const updateUserLang = createAction( export const updateUserLang = createAction(
types.updateUserLang, types.updateUserLang,
(username, lang) => ({ username, lang }) (username, lang) => ({ username, languageTag: lang })
); );
// updateUserChallenge( // updateUserChallenge(

View File

@ -1,6 +1,7 @@
import { handleActions } from 'redux-actions';
import types from './types'; import types from './types';
const { updateUserPoints } = types;
const initialState = { const initialState = {
superBlock: {}, superBlock: {},
block: {}, block: {},
@ -8,96 +9,90 @@ const initialState = {
user: {} user: {}
}; };
// future refactor(berks): Several of the actions here are just updating props const userReducer = handleActions(
// on the main user. These can be refactors into one response for all actions {
export default function entities(state = initialState, action) { [types.updateUserPoints]: (state, { payload: { username, points } }) => ({
const { ...state,
type, [username]: {
payload: { email, username, points, flag, languageTag } = {} ...state[username],
} = action; 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) { if (action.meta && action.meta.entities) {
return { return {
...state, ...state,
...action.meta.entities ...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; 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;
}