Files
freeCodeCamp/common/app/redux/fetch-challenges-saga.js

33 lines
1022 B
JavaScript
Raw Normal View History

import { Observable } from 'rx';
import { fetchChallenge, fetchChallenges } from './types';
import {
createErrorObserable,
fetchChallengeCompleted,
fetchChallengesCompleted,
setChallenge
} from './actions';
2016-03-21 15:39:45 -07:00
2016-04-24 21:54:48 -07:00
export default function fetchChallengesSaga(action$, getState, { services }) {
return action$
.filter(
({ type }) => type === fetchChallenges || type === fetchChallenge
)
.flatMap(({ type, payload })=> {
const options = { service: 'map' };
if (type === fetchChallenge) {
options.params = { dashedName: payload };
}
return services.readService$(options)
.flatMap(({ entities, result } = {}) => {
if (type === fetchChallenge) {
return Observable.of(
fetchChallengeCompleted(entities, result),
setChallenge(entities.challenge[result])
);
}
return Observable.just(fetchChallengesCompleted(entities, result));
2016-04-24 21:54:48 -07:00
})
2016-05-06 16:04:23 -07:00
.catch(createErrorObserable);
2016-04-24 21:54:48 -07:00
});
}