2016-06-09 16:02:51 -07:00
|
|
|
import { Observable } from 'rx';
|
|
|
|
import { push } from 'react-router-redux';
|
2016-06-23 20:05:30 -07:00
|
|
|
import types from './types';
|
2016-06-10 10:45:29 -07:00
|
|
|
import { resetUi, updateCurrentChallenge } from './actions';
|
2016-07-06 11:47:16 -07:00
|
|
|
import { createErrorObservable } from '../../../redux/actions';
|
|
|
|
import { makeToast } from '../../../toasts/redux/actions';
|
2016-06-10 14:01:13 -07:00
|
|
|
import {
|
|
|
|
getNextChallenge,
|
|
|
|
getFirstChallengeOfNextBlock,
|
|
|
|
getFirstChallengeOfNextSuperBlock
|
|
|
|
} from '../utils';
|
2016-07-01 19:05:50 -07:00
|
|
|
import debug from 'debug';
|
2016-06-09 16:02:51 -07:00
|
|
|
|
2016-07-01 19:05:50 -07:00
|
|
|
const isDev = debug.enabled('fcc:*');
|
2016-06-23 20:05:30 -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;
|
2016-07-01 19:05:50 -07:00
|
|
|
nextChallenge = getNextChallenge(challenge, entities, { isDev });
|
2016-06-10 14:01:13 -07:00
|
|
|
// block completed.
|
|
|
|
if (!nextChallenge) {
|
|
|
|
// isNewBlock = true;
|
2016-07-01 19:05:50 -07:00
|
|
|
nextChallenge = getFirstChallengeOfNextBlock(
|
|
|
|
challenge,
|
|
|
|
entities,
|
|
|
|
{ isDev }
|
|
|
|
);
|
2016-06-10 14:01:13 -07:00
|
|
|
}
|
|
|
|
// superBlock completed
|
|
|
|
if (!nextChallenge) {
|
|
|
|
// isNewSuperBlock = true;
|
|
|
|
nextChallenge = getFirstChallengeOfNextSuperBlock(
|
|
|
|
challenge,
|
|
|
|
entities,
|
2016-07-01 19:05:50 -07:00
|
|
|
superBlocks,
|
|
|
|
{ isDev }
|
2016-06-10 14:01:13 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
/* 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 = {
|
2016-06-13 12:26:30 -07:00
|
|
|
// title: isNewSuperBlock || isNewBlock ? randomVerb() : null,
|
2016-06-10 14:01:13 -07:00
|
|
|
message
|
|
|
|
};
|
|
|
|
*/
|
2016-10-31 14:47:31 +00:00
|
|
|
if (nextChallenge.isLocked) {
|
|
|
|
return Observable.of(
|
|
|
|
makeToast({
|
|
|
|
message: 'The next challenge has not been unlocked. ' +
|
|
|
|
'Please revisit the required (*) challenges ' +
|
|
|
|
'that have not been passed yet. ',
|
|
|
|
timeout: 15000
|
|
|
|
}),
|
|
|
|
push('/map')
|
|
|
|
);
|
|
|
|
}
|
2016-06-10 14:01:13 -07:00
|
|
|
return Observable.of(
|
|
|
|
updateCurrentChallenge(nextChallenge),
|
|
|
|
resetUi(),
|
2016-09-08 22:31:42 -07:00
|
|
|
makeToast({ message: 'Your next challenge has arrived.' }),
|
2016-06-10 14:01:13 -07:00
|
|
|
push(`/challenges/${nextChallenge.block}/${nextChallenge.dashedName}`)
|
|
|
|
);
|
|
|
|
} catch (err) {
|
|
|
|
return createErrorObservable(err);
|
|
|
|
}
|
2016-06-09 16:02:51 -07:00
|
|
|
});
|
|
|
|
}
|