Feature(redux): Add completed challenge logic to redux
This commit is contained in:
@ -38,6 +38,11 @@ export const updateUserPoints = createAction(
|
|||||||
types.updateUserPoints,
|
types.updateUserPoints,
|
||||||
(username, points) => ({ username, points })
|
(username, points) => ({ username, points })
|
||||||
);
|
);
|
||||||
|
// updateCompletedChallenges(username: String) => Action
|
||||||
|
export const updateCompletedChallenges = createAction(
|
||||||
|
types.updateCompletedChallenges
|
||||||
|
);
|
||||||
|
|
||||||
// used when server needs client to redirect
|
// used when server needs client to redirect
|
||||||
export const delayedRedirect = createAction(types.delayedRedirect);
|
export const delayedRedirect = createAction(types.delayedRedirect);
|
||||||
|
|
||||||
|
@ -1,4 +1,7 @@
|
|||||||
import { updateUserPoints } from './types';
|
import {
|
||||||
|
updateUserPoints,
|
||||||
|
updateCompletedChallenges
|
||||||
|
} from './types';
|
||||||
|
|
||||||
const initialState = {
|
const initialState = {
|
||||||
superBlock: {},
|
superBlock: {},
|
||||||
@ -9,6 +12,22 @@ const initialState = {
|
|||||||
|
|
||||||
export default function entities(state = initialState, action) {
|
export default function entities(state = initialState, action) {
|
||||||
const { type, payload: { username, points } = {} } = 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) {
|
if (type === updateUserPoints) {
|
||||||
return {
|
return {
|
||||||
...state,
|
...state,
|
||||||
|
@ -3,6 +3,7 @@ import { fetchUser } from './types';
|
|||||||
import {
|
import {
|
||||||
addUser,
|
addUser,
|
||||||
updateThisUser,
|
updateThisUser,
|
||||||
|
updateCompletedChallenges,
|
||||||
createErrorObservable,
|
createErrorObservable,
|
||||||
showSignIn
|
showSignIn
|
||||||
} from './actions';
|
} from './actions';
|
||||||
@ -18,7 +19,8 @@ export default function getUserSaga(action$, getState, { services }) {
|
|||||||
}
|
}
|
||||||
return Observable.of(
|
return Observable.of(
|
||||||
addUser(entities),
|
addUser(entities),
|
||||||
updateThisUser(result)
|
updateThisUser(result),
|
||||||
|
updateCompletedChallenges(result)
|
||||||
);
|
);
|
||||||
})
|
})
|
||||||
.catch(createErrorObservable);
|
.catch(createErrorObservable);
|
||||||
|
@ -7,6 +7,7 @@ export default createTypes([
|
|||||||
'addUser',
|
'addUser',
|
||||||
'updateThisUser',
|
'updateThisUser',
|
||||||
'updateUserPoints',
|
'updateUserPoints',
|
||||||
|
'updateCompletedChallenges',
|
||||||
'showSignIn',
|
'showSignIn',
|
||||||
|
|
||||||
'makeToast',
|
'makeToast',
|
||||||
|
@ -24,13 +24,20 @@ export class Block extends PureComponent {
|
|||||||
return <div>No Challenges Found</div>;
|
return <div>No Challenges Found</div>;
|
||||||
}
|
}
|
||||||
return challenges.map(challenge => {
|
return challenges.map(challenge => {
|
||||||
const { title, dashedName, isLocked, isRequired } = challenge;
|
const {
|
||||||
|
title,
|
||||||
|
dashedName,
|
||||||
|
isLocked,
|
||||||
|
isRequired,
|
||||||
|
isCompleted
|
||||||
|
} = challenge;
|
||||||
const challengeClassName = classnames({
|
const challengeClassName = classnames({
|
||||||
'text-primary': true,
|
'text-primary': true,
|
||||||
'padded-ionic-icon': true,
|
'padded-ionic-icon': true,
|
||||||
'negative-15': true,
|
'negative-15': true,
|
||||||
'challenge-title': true,
|
'challenge-title': true,
|
||||||
'ion-checkmark-circled': !isLocked,
|
'ion-checkmark-circled faded': !isLocked && isCompleted,
|
||||||
|
'ion-ios-circle-outline': !isLocked && !isCompleted,
|
||||||
'ion-locked': isLocked,
|
'ion-locked': isLocked,
|
||||||
disabled: isLocked
|
disabled: isLocked
|
||||||
});
|
});
|
||||||
|
Reference in New Issue
Block a user