Files
freeCodeCamp/common/app/routes/challenges/redux/next-challenge-saga.js

71 lines
2.3 KiB
JavaScript
Raw Normal View History

2016-06-09 16:02:51 -07:00
import { Observable } from 'rx';
import { push } from 'react-router-redux';
import types from './types';
import { resetUi, updateCurrentChallenge } from './actions';
2016-06-10 14:01:13 -07:00
import { createErrorObservable, makeToast } from '../../../redux/actions';
import {
getNextChallenge,
getFirstChallengeOfNextBlock,
getFirstChallengeOfNextSuperBlock
} from '../utils';
import { randomVerb } from '../../../utils/get-words';
2016-06-09 16:02:51 -07:00
const { moveToNextChallenge } = types;
2016-06-09 16:02:51 -07:00
export default function nextChallengeSaga(actions$, getState) {
return actions$
.filter(({ type }) => type === moveToNextChallenge)
.flatMap(() => {
2016-06-10 14:01:13 -07:00
let nextChallenge;
// let message = '';
// let isNewBlock = false;
// let isNewSuperBlock = false;
try {
const state = getState();
const { challenge, superBlocks } = state.challengesApp;
const { entities } = state;
nextChallenge = getNextChallenge(challenge, entities);
// block completed.
if (!nextChallenge) {
// isNewBlock = true;
nextChallenge = getFirstChallengeOfNextBlock(challenge, entities);
}
// superBlock completed
if (!nextChallenge) {
// isNewSuperBlock = true;
nextChallenge = getFirstChallengeOfNextSuperBlock(
challenge,
entities,
superBlocks
);
}
/* this requires user data not available yet
if (isNewSuperBlock || isNewBlock) {
const getName = isNewSuperBlock ?
getCurrentSuperBlockName :
getCurrentBlockName;
const blockType = isNewSuperBlock ? 'SuperBlock' : 'Block';
message =
`You've competed the ${getName(challenge, entities)} ${blockType}!`;
}
message += ' Your next challenge has arrived.';
const toast = {
// title: isNewSuperBlock || isNewBlock ? randomVerb() : null,
2016-06-10 14:01:13 -07:00
message
};
*/
return Observable.of(
updateCurrentChallenge(nextChallenge),
resetUi(),
makeToast({
title: randomVerb(),
2016-06-10 14:01:13 -07:00
message: 'Your next challenge has arrived.'
}),
push(`/challenges/${nextChallenge.block}/${nextChallenge.dashedName}`)
);
} catch (err) {
return createErrorObservable(err);
}
2016-06-09 16:02:51 -07:00
});
}