import { Observable } from 'rx';
import { push } from 'react-router-redux';
import types from './types';
import { resetUi, updateCurrentChallenge } from './actions';
import { createErrorObservable } from '../../../redux/actions';
import { makeToast } from '../../../toasts/redux/actions';
import {
  getNextChallenge,
  getFirstChallengeOfNextBlock,
  getFirstChallengeOfNextSuperBlock
} from '../utils';
import debug from 'debug';

const isDev = debug.enabled('fcc:*');
const { moveToNextChallenge } = types;

export default function nextChallengeSaga(actions$, getState) {
  return actions$
    .filter(({ type }) => type === moveToNextChallenge)
    .flatMap(() => {
      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, { isDev });
        // block completed.
        if (!nextChallenge) {
          // isNewBlock = true;
          nextChallenge = getFirstChallengeOfNextBlock(
            challenge,
            entities,
            { isDev }
          );
        }
        // superBlock completed
        if (!nextChallenge) {
          // isNewSuperBlock = true;
          nextChallenge = getFirstChallengeOfNextSuperBlock(
            challenge,
            entities,
            superBlocks,
            { isDev }
          );
        }
        /* 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,
          message
        };
        */
        return Observable.of(
          updateCurrentChallenge(nextChallenge),
          resetUi(),
          makeToast({ message: 'Your next challenge has arrived.' }),
          push(`/challenges/${nextChallenge.block}/${nextChallenge.dashedName}`)
        );
      } catch (err) {
        return createErrorObservable(err);
      }
    });
}

@@ -2105,16 +2105,384 @@
      */%0A
+        if (nextChallenge.isLocked) %7B%0A          return Observable.of(%0A            makeToast(%7B%0A              message: 'The next challenge has not been unlocked. ' +%0A              'Please revisit the required (*) challenges ' +%0A              'that have not been passed yet. ',%0A              timeout: 15000%0A            %7D),%0A            push('/map')%0A          );%0A        %7D%0A
         

Fix Final Video Challenge Completion