Feature(redux): Add completed challenge logic to redux
This commit is contained in:
@ -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);
|
||||
|
||||
|
@ -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,
|
||||
|
@ -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);
|
||||
|
@ -7,6 +7,7 @@ export default createTypes([
|
||||
'addUser',
|
||||
'updateThisUser',
|
||||
'updateUserPoints',
|
||||
'updateCompletedChallenges',
|
||||
'showSignIn',
|
||||
|
||||
'makeToast',
|
||||
|
@ -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
|
||||
});
|
||||
|
Reference in New Issue
Block a user