Files
freeCodeCamp/common/app/routes/Jobs/redux/fetch-jobs-saga.js

37 lines
1.0 KiB
JavaScript
Raw Normal View History

2016-02-25 18:30:10 -08:00
import { Observable } from 'rx';
import { normalize, Schema, arrayOf } from 'normalizr';
2016-02-05 20:48:59 -08:00
2016-02-25 18:30:10 -08:00
import { fetchJobsCompleted } from './actions';
import { fetchJobs } from './types';
import { handleError } from '../../../redux/types';
const job = new Schema('job', { idAttribute: 'id' });
2016-04-24 21:54:48 -07:00
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 }));
});
}