2017-01-23 20:04:12 +01:00
|
|
|
import { takeLatest } from 'redux-saga';
|
2017-03-20 22:08:49 +01:00
|
|
|
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';
|
|
|
|
|
|
2017-03-20 22:08:49 +01:00
|
|
|
import {
|
2017-04-10 16:28:30 +02:00
|
|
|
makeSelectCurrentModelName,
|
2017-03-20 22:08:49 +01:00
|
|
|
} from './selectors';
|
|
|
|
|
|
2017-01-23 20:04:12 +01:00
|
|
|
export function* getRecords() {
|
2017-04-10 16:28:30 +02:00
|
|
|
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'
|
|
|
|
|
};
|
2017-03-20 22:08:49 +01:00
|
|
|
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();
|
|
|
|
|
|
2017-03-20 22:08:49 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2017-01-20 16:22:57 +01:00
|
|
|
|
|
|
|
|
// Individual exports for testing
|
|
|
|
|
export function* defaultSaga() {
|
2017-01-23 20:04:12 +01:00
|
|
|
yield takeLatest(LOAD_RECORDS, getRecords);
|
2017-01-20 16:22:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// All sagas to be loaded
|
|
|
|
|
export default [
|
|
|
|
|
defaultSaga,
|
|
|
|
|
];
|