Files
freeCodeCamp/common/app/redux/entities-reducer.js
2017-05-03 19:22:05 -05:00

99 lines
2.0 KiB
JavaScript

import { handleActions } from 'redux-actions';
import types from './types';
const initialState = {
superBlock: {},
block: {},
challenge: {},
user: {}
};
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
};
}
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;
}