2017-07-31 20:04:01 -07:00
|
|
|
import { Observable } from 'rx';
|
|
|
|
import { combineEpics, ofType } from 'redux-epic';
|
|
|
|
import debug from 'debug';
|
|
|
|
|
|
|
|
import {
|
|
|
|
types,
|
|
|
|
|
|
|
|
createErrorObservable,
|
|
|
|
delayedRedirect,
|
|
|
|
|
|
|
|
fetchChallengeCompleted,
|
2018-02-23 12:20:13 +00:00
|
|
|
fetchChallengesCompleted,
|
2018-02-23 18:34:46 +00:00
|
|
|
fetchNewBlock,
|
|
|
|
challengeSelector,
|
2018-02-24 08:44:12 +00:00
|
|
|
nextChallengeSelector
|
2017-07-31 20:04:01 -07:00
|
|
|
} from './';
|
2018-02-23 18:34:46 +00:00
|
|
|
import {
|
|
|
|
isChallengeLoaded,
|
2018-02-24 08:44:12 +00:00
|
|
|
fullBlocksSelector
|
2018-02-23 18:34:46 +00:00
|
|
|
} from '../entities';
|
2017-11-09 17:10:30 -08:00
|
|
|
|
2017-07-31 20:04:01 -07:00
|
|
|
import { shapeChallenges } from './utils';
|
2017-11-09 17:10:30 -08:00
|
|
|
import { types as challenge } from '../routes/Challenges/redux';
|
|
|
|
import { langSelector } from '../Router/redux';
|
2017-07-31 20:04:01 -07:00
|
|
|
|
|
|
|
const isDev = debug.enabled('fcc:*');
|
|
|
|
|
2018-02-23 12:20:13 +00:00
|
|
|
function fetchChallengeEpic(actions, { getState }, { services }) {
|
2017-11-09 17:10:30 -08:00
|
|
|
return actions::ofType(challenge.onRouteChallenges)
|
|
|
|
.filter(({ payload }) => !isChallengeLoaded(getState(), payload))
|
|
|
|
.flatMapLatest(({ payload: params }) => {
|
2017-07-31 20:04:01 -07:00
|
|
|
const options = {
|
2018-02-23 17:08:42 +00:00
|
|
|
service: 'challenge',
|
2017-11-09 17:10:30 -08:00
|
|
|
params
|
2017-07-31 20:04:01 -07:00
|
|
|
};
|
|
|
|
return services.readService$(options)
|
|
|
|
.retry(3)
|
|
|
|
.map(({ entities, ...rest }) => ({
|
|
|
|
entities: shapeChallenges(entities, isDev),
|
|
|
|
...rest
|
|
|
|
}))
|
|
|
|
.flatMap(({ entities, result, redirect } = {}) => {
|
|
|
|
const actions = [
|
|
|
|
fetchChallengeCompleted({
|
|
|
|
entities,
|
|
|
|
currentChallenge: result.challenge,
|
|
|
|
challenge: entities.challenge[result.challenge],
|
|
|
|
result
|
|
|
|
}),
|
|
|
|
redirect ? delayedRedirect(redirect) : null
|
|
|
|
];
|
|
|
|
return Observable.from(actions).filter(Boolean);
|
|
|
|
})
|
|
|
|
.catch(createErrorObservable);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-02-23 12:20:13 +00:00
|
|
|
export function fetchChallengesForBlockEpic(
|
2017-07-31 20:04:01 -07:00
|
|
|
actions,
|
|
|
|
{ getState },
|
|
|
|
{ services }
|
|
|
|
) {
|
2018-02-16 23:18:53 +00:00
|
|
|
return actions::ofType(
|
|
|
|
types.appMounted,
|
2018-02-23 12:20:13 +00:00
|
|
|
types.updateChallenges,
|
|
|
|
types.fetchNewBlock.start
|
2018-02-16 23:18:53 +00:00
|
|
|
)
|
2018-02-23 12:20:13 +00:00
|
|
|
.flatMapLatest(({ type, payload }) => {
|
|
|
|
const fetchAnotherBlock = type === types.fetchNewBlock.start;
|
|
|
|
const state = getState();
|
|
|
|
let { block: blockName } = challengeSelector(state);
|
|
|
|
const lang = langSelector(state);
|
|
|
|
|
|
|
|
if (fetchAnotherBlock) {
|
|
|
|
const fullBlocks = fullBlocksSelector(state);
|
|
|
|
if (fullBlocks.includes(payload)) {
|
2018-02-24 08:44:12 +00:00
|
|
|
return Observable.of(null);
|
2018-02-23 12:20:13 +00:00
|
|
|
}
|
|
|
|
blockName = payload;
|
|
|
|
}
|
|
|
|
|
2017-07-31 20:04:01 -07:00
|
|
|
const options = {
|
2018-02-23 12:20:13 +00:00
|
|
|
params: { lang, blockName },
|
2018-02-23 17:08:42 +00:00
|
|
|
service: 'challenge'
|
2017-07-31 20:04:01 -07:00
|
|
|
};
|
|
|
|
return services.readService$(options)
|
|
|
|
.retry(3)
|
2018-02-23 12:20:13 +00:00
|
|
|
.map(fetchChallengesCompleted)
|
2017-07-31 20:04:01 -07:00
|
|
|
.startWith({ type: types.fetchChallenges.start })
|
|
|
|
.catch(createErrorObservable);
|
2018-02-24 08:44:12 +00:00
|
|
|
})
|
|
|
|
.filter(Boolean);
|
2017-07-31 20:04:01 -07:00
|
|
|
}
|
|
|
|
|
2018-02-23 18:34:46 +00:00
|
|
|
function fetchChallengesForNextBlockEpic(action$, { getState }) {
|
|
|
|
return action$::ofType(challenge.checkForNextBlock)
|
|
|
|
.map(() => {
|
2018-02-24 08:44:12 +00:00
|
|
|
const {
|
|
|
|
nextChallenge,
|
|
|
|
isNewBlock,
|
|
|
|
isNewSuperBlock
|
|
|
|
} = nextChallengeSelector(getState());
|
2018-02-23 18:34:46 +00:00
|
|
|
const isNewBlockRequired = (
|
|
|
|
(isNewBlock || isNewSuperBlock) &&
|
2018-02-24 08:44:12 +00:00
|
|
|
nextChallenge &&
|
2018-02-23 18:34:46 +00:00
|
|
|
!nextChallenge.description
|
|
|
|
);
|
|
|
|
return isNewBlockRequired ?
|
|
|
|
fetchNewBlock(nextChallenge.block) :
|
2018-02-24 08:44:12 +00:00
|
|
|
null;
|
|
|
|
})
|
|
|
|
.filter(Boolean);
|
2018-02-23 18:34:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default combineEpics(
|
|
|
|
fetchChallengeEpic,
|
|
|
|
fetchChallengesForBlockEpic,
|
|
|
|
fetchChallengesForNextBlockEpic
|
|
|
|
);
|