Feature(redux): Add completed challenge logic to redux

This commit is contained in:
Berkeley Martinez
2016-06-20 13:38:49 -07:00
parent 0c07e961a7
commit 1be1712577
5 changed files with 38 additions and 4 deletions

View File

@ -38,6 +38,11 @@ export const updateUserPoints = createAction(
types.updateUserPoints,
(username, points) => ({ username, points })
);
// updateCompletedChallenges(username: String) => Action
export const updateCompletedChallenges = createAction(
types.updateCompletedChallenges
);
// used when server needs client to redirect
export const delayedRedirect = createAction(types.delayedRedirect);

View File

@ -1,4 +1,7 @@
import { updateUserPoints } from './types';
import {
updateUserPoints,
updateCompletedChallenges
} from './types';
const initialState = {
superBlock: {},
@ -9,6 +12,22 @@ const initialState = {
export default function entities(state = initialState, action) {
const { type, payload: { username, points } = {} } = 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,

View File

@ -3,6 +3,7 @@ import { fetchUser } from './types';
import {
addUser,
updateThisUser,
updateCompletedChallenges,
createErrorObservable,
showSignIn
} from './actions';
@ -18,7 +19,8 @@ export default function getUserSaga(action$, getState, { services }) {
}
return Observable.of(
addUser(entities),
updateThisUser(result)
updateThisUser(result),
updateCompletedChallenges(result)
);
})
.catch(createErrorObservable);

View File

@ -7,6 +7,7 @@ export default createTypes([
'addUser',
'updateThisUser',
'updateUserPoints',
'updateCompletedChallenges',
'showSignIn',
'makeToast',

View File

@ -24,13 +24,20 @@ export class Block extends PureComponent {
return <div>No Challenges Found</div>;
}
return challenges.map(challenge => {
const { title, dashedName, isLocked, isRequired } = challenge;
const {
title,
dashedName,
isLocked,
isRequired,
isCompleted
} = challenge;
const challengeClassName = classnames({
'text-primary': true,
'padded-ionic-icon': true,
'negative-15': true,
'challenge-title': true,
'ion-checkmark-circled': !isLocked,
'ion-checkmark-circled faded': !isLocked && isCompleted,
'ion-ios-circle-outline': !isLocked && !isCompleted,
'ion-locked': isLocked,
disabled: isLocked
});