Files
freeCodeCamp/common/app/routes/Jobs/redux/fetch-jobs-saga.js
Berkeley Martinez d511be3332 Add new rx saga
2016-07-28 23:39:17 -07:00

37 lines
1.0 KiB
JavaScript

import { Observable } from 'rx';
import { normalize, Schema, arrayOf } from 'normalizr';
import { fetchJobsCompleted } from './actions';
import { fetchJobs } from './types';
import { handleError } from '../../../redux/types';
const job = new Schema('job', { idAttribute: 'id' });
export default function fetchJobsSaga(action$, getState, { services }) {
return action$
.filter(action => action.type === fetchJobs)
.flatMap(action => {
const { payload: id } = action;
const data = { service: 'jobs' };
if (id) {
data.params = { id };
}
return services.readService$(data)
.map(jobs => {
if (!Array.isArray(jobs)) {
jobs = [jobs];
}
const { entities, result } = normalize(
{ jobs },
{ jobs: arrayOf(job) }
);
return fetchJobsCompleted(
entities,
result.jobs[0],
result.jobs
);
})
.catch(error => Observable.just({ type: handleError, error }));
});
}