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,
|
|
|
|
challengeSelector
|
2017-07-31 20:04:01 -07:00
|
|
|
} from './';
|
2018-02-23 12:20:13 +00:00
|
|
|
import { isChallengeLoaded, fullBlocksSelector } from '../entities/index.js';
|
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)) {
|
|
|
|
return Observable.of({ type: 'NULL'});
|
|
|
|
}
|
|
|
|
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-23 12:20:13 +00:00
|
|
|
export default combineEpics(fetchChallengeEpic, fetchChallengesForBlockEpic);
|