2017-05-11 14:17:21 +02:00
|
|
|
|
import _ from 'lodash';
|
2017-08-29 17:32:48 +02:00
|
|
|
|
import { fork, put, select, call, takeLatest } from 'redux-saga/effects';
|
2017-07-06 17:51:13 +02:00
|
|
|
|
|
2017-07-21 18:15:43 +02:00
|
|
|
|
import request from 'utils/request';
|
2017-07-06 17:51:13 +02:00
|
|
|
|
import { generateSchema } from 'utils/schema';
|
|
|
|
|
|
|
|
|
|
import { loadedModels, updateSchema } from './actions';
|
2017-09-14 11:10:05 +02:00
|
|
|
|
import { LOAD_MODELS, LOADED_MODELS } from './constants';
|
2017-07-06 17:51:13 +02:00
|
|
|
|
import { makeSelectModels } from './selectors';
|
2017-03-18 17:34:00 +01:00
|
|
|
|
|
2017-09-14 11:10:05 +02:00
|
|
|
|
export const generateMenu = function () {
|
|
|
|
|
try {
|
|
|
|
|
return request(`${window.Strapi.apiUrl}/content-manager/models`, {
|
|
|
|
|
method: 'GET',
|
|
|
|
|
})
|
|
|
|
|
.then(response => generateSchema(response))
|
|
|
|
|
.then(displayedModels => {
|
|
|
|
|
return [{
|
|
|
|
|
name: 'Content Types',
|
|
|
|
|
links: _.map(displayedModels, (model, key) => ({
|
|
|
|
|
label: model.labelPlural || model.label || key,
|
|
|
|
|
destination: key,
|
|
|
|
|
})),
|
|
|
|
|
}];
|
|
|
|
|
});
|
|
|
|
|
} catch (err) {
|
|
|
|
|
window.Strapi.notification.error(
|
|
|
|
|
'An error occurred during models config fetch.'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-03-18 17:34:00 +01:00
|
|
|
|
|
|
|
|
|
export function* getModels() {
|
|
|
|
|
try {
|
2017-07-21 18:15:43 +02:00
|
|
|
|
const response = yield call(request,
|
|
|
|
|
`${window.Strapi.apiUrl}/content-manager/models`, {
|
|
|
|
|
method: 'GET',
|
|
|
|
|
});
|
2017-03-18 17:34:00 +01:00
|
|
|
|
|
2017-07-21 18:15:43 +02:00
|
|
|
|
yield put(loadedModels(response));
|
2017-03-18 17:34:00 +01:00
|
|
|
|
} 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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-06 17:51:13 +02:00
|
|
|
|
export function* modelsLoaded() {
|
|
|
|
|
const models = yield select(makeSelectModels());
|
|
|
|
|
let schema;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
schema = generateSchema(models);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
window.Strapi.notification.error(
|
|
|
|
|
'An error occurred during schema generation.'
|
|
|
|
|
);
|
|
|
|
|
throw new Error(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
yield put(updateSchema(schema));
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-18 17:34:00 +01:00
|
|
|
|
// Individual exports for testing
|
|
|
|
|
export function* defaultSaga() {
|
|
|
|
|
yield fork(takeLatest, LOAD_MODELS, getModels);
|
2017-07-06 17:51:13 +02:00
|
|
|
|
yield fork(takeLatest, LOADED_MODELS, modelsLoaded);
|
2017-03-18 17:34:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// All sagas to be loaded
|
2017-08-29 17:32:48 +02:00
|
|
|
|
export default defaultSaga;
|