44 lines
853 B
JavaScript
Raw Normal View History

2017-01-23 20:04:12 +01:00
import { takeLatest } from 'redux-saga';
import { put, select } from 'redux-saga/effects';
2017-01-23 20:04:12 +01:00
import {
2017-01-26 18:53:52 +01:00
loadedRecord
2017-01-23 20:04:12 +01:00
} from './actions';
import {
2017-01-26 18:53:52 +01:00
LOAD_RECORDS
2017-01-23 20:04:12 +01:00
} from './constants';
import {
makeSelectCurrentModelName,
} from './selectors';
2017-01-23 20:04:12 +01:00
export function* getRecords() {
const currentModel = yield select(makeSelectCurrentModelName());
2017-01-23 20:04:12 +01:00
2017-01-28 18:11:54 +01:00
try {
const opts = {
method: 'GET',
mode: 'cors',
cache: 'default'
};
const response = yield fetch(`http://localhost:1337/content-manager/explorer/${currentModel}`, opts);
2017-01-28 18:11:54 +01:00
const data = yield response.json();
yield put(loadedRecord(data));
2017-01-28 18:11:54 +01:00
} catch (err) {
console.error(err);
}
2017-01-23 20:04:12 +01:00
}
// Individual exports for testing
export function* defaultSaga() {
2017-01-23 20:04:12 +01:00
yield takeLatest(LOAD_RECORDS, getRecords);
}
// All sagas to be loaded
export default [
defaultSaga,
];