2017-05-11 14:17:21 +02:00
|
|
|
|
import _ from 'lodash';
|
2017-07-06 17:51:13 +02:00
|
|
|
|
import { takeLatest } from 'redux-saga';
|
2017-07-21 18:15:43 +02:00
|
|
|
|
import { fork, put, select, call } 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';
|
|
|
|
|
import { LOAD_MODELS, LOADED_MODELS, UPDATE_SCHEMA } from './constants';
|
|
|
|
|
import { makeSelectModels } from './selectors';
|
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-06-17 17:01:50 +02:00
|
|
|
|
export function* schemaUpdated(action) {
|
2017-06-20 18:50:37 +02:00
|
|
|
|
// Display the links only if the `displayed` attribute is not set to false
|
2017-06-20 18:53:31 +02:00
|
|
|
|
const displayedModels = _.pickBy(action.schema, model => (model.displayed !== false));
|
2017-06-20 18:50:37 +02:00
|
|
|
|
|
|
|
|
|
// Map links to format them
|
2017-08-16 17:05:02 +02:00
|
|
|
|
const leftMenuSections = [{
|
|
|
|
|
name: 'Content Types',
|
|
|
|
|
links: _.map(displayedModels, (model, key) => ({
|
|
|
|
|
label: model.labelPlural || model.label || key,
|
|
|
|
|
destination: key,
|
|
|
|
|
})),
|
|
|
|
|
}];
|
2017-06-17 17:01:50 +02:00
|
|
|
|
|
|
|
|
|
// Update the admin left menu links
|
2017-08-16 17:05:02 +02:00
|
|
|
|
window.Strapi.refresh('content-manager').leftMenuSections(leftMenuSections);
|
2017-06-17 17:01:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-18 17:34:00 +01:00
|
|
|
|
// Individual exports for testing
|
|
|
|
|
export function* defaultSaga() {
|
|
|
|
|
yield fork(takeLatest, LOAD_MODELS, getModels);
|
2017-06-17 17:01:50 +02:00
|
|
|
|
yield fork(takeLatest, UPDATE_SCHEMA, schemaUpdated);
|
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-22 15:31:42 +02:00
|
|
|
|
export default defaultSaga;
|