Files
freeCodeCamp/common/app/redux/entities-reducer.js
Berkeley Martinez 0d3dd75f41 Feature(settings): add user flag logic
This also moves a couple of settings to their own controller
2016-07-28 23:40:01 -07:00

61 lines
1.3 KiB
JavaScript

import types from './types';
const { updateUserPoints, updateCompletedChallenges } = types;
const initialState = {
superBlock: {},
block: {},
challenge: {},
user: {}
};
export default function entities(state = initialState, action) {
const { type, payload: { username, points, flag } = {} } = action;
if (type === updateCompletedChallenges) {
const username = action.payload;
const completedChallengeMap = state.user[username].challengeMap || {};
return {
...state,
challenge: Object.keys(state.challenge)
.reduce((map, key) => {
const challenge = state.challenge[key];
map[key] = {
...challenge,
isCompleted: !!completedChallengeMap[challenge.id]
};
return map;
}, {})
};
}
if (type === updateUserPoints) {
return {
...state,
user: {
...state.user,
[username]: {
...state.user[username],
points
}
}
};
}
if (action.meta && action.meta.entities) {
return {
...state,
...action.meta.entities
};
}
if (action.type === types.updateUserFlag) {
return {
...state,
user: {
...state.user,
[username]: {
...state.user[username],
[flag]: !state.user[username][flag]
}
}
};
}
return state;
}