2017-05-11 14:17:21 +02:00
|
|
|
import _ from 'lodash';
|
2017-03-18 17:34:00 +01:00
|
|
|
import { takeLatest } from 'redux-saga';
|
|
|
|
import { fork, put } from 'redux-saga/effects';
|
|
|
|
|
2017-05-11 10:54:44 +02:00
|
|
|
import { loadedModels } from './actions';
|
|
|
|
import { LOAD_MODELS } from './constants';
|
2017-03-18 17:34:00 +01:00
|
|
|
|
|
|
|
export function* getModels() {
|
|
|
|
try {
|
|
|
|
const opts = {
|
|
|
|
method: 'GET',
|
|
|
|
mode: 'cors',
|
2017-05-11 10:54:44 +02:00
|
|
|
cache: 'default',
|
2017-03-18 17:34:00 +01:00
|
|
|
};
|
2017-05-11 10:54:44 +02:00
|
|
|
const response = yield fetch(
|
|
|
|
'http://localhost:1337/content-manager/models',
|
|
|
|
opts
|
|
|
|
);
|
2017-03-18 17:34:00 +01:00
|
|
|
const data = yield response.json();
|
|
|
|
|
|
|
|
yield put(loadedModels(data));
|
|
|
|
|
|
|
|
const leftMenuLinks = _.map(data, (model, key) => ({
|
|
|
|
label: model.globalId,
|
|
|
|
to: key,
|
|
|
|
}));
|
|
|
|
|
|
|
|
// Update the admin left menu links
|
|
|
|
window.Strapi.refresh('content-manager').leftMenuLinks(leftMenuLinks);
|
|
|
|
} catch (err) {
|
2017-05-11 10:54:44 +02:00
|
|
|
window.Strapi.notification.error(
|
|
|
|
'An error occurred during models config fetch.'
|
|
|
|
);
|
2017-03-18 17:34:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Individual exports for testing
|
|
|
|
export function* defaultSaga() {
|
|
|
|
// yield takeLatest(LOAD_MODELS, getModels);
|
|
|
|
yield fork(takeLatest, LOAD_MODELS, getModels);
|
|
|
|
}
|
|
|
|
|
|
|
|
// All sagas to be loaded
|
2017-05-11 10:54:44 +02:00
|
|
|
export default [defaultSaga];
|