2017-05-11 14:17:21 +02:00
|
|
|
import _ from 'lodash';
|
2017-07-06 17:51:13 +02:00
|
|
|
import { takeLatest } from 'redux-saga';
|
|
|
|
import { fork, put, select } from 'redux-saga/effects';
|
|
|
|
|
|
|
|
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 {
|
|
|
|
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(
|
2017-06-19 19:49:15 +02:00
|
|
|
'/content-manager/models',
|
2017-05-11 10:54:44 +02:00
|
|
|
opts
|
|
|
|
);
|
2017-03-18 17:34:00 +01:00
|
|
|
const data = yield response.json();
|
|
|
|
|
|
|
|
yield put(loadedModels(data));
|
|
|
|
} 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
|
|
|
|
const leftMenuLinks = _.map(displayedModels, (model, key) => ({
|
|
|
|
label: model.labelPlural || model.label || key,
|
2017-06-17 17:01:50 +02:00
|
|
|
to: key,
|
|
|
|
}));
|
|
|
|
|
|
|
|
// Update the admin left menu links
|
|
|
|
window.Strapi.refresh('content-manager').leftMenuLinks(leftMenuLinks);
|
|
|
|
}
|
|
|
|
|
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-05-11 10:54:44 +02:00
|
|
|
export default [defaultSaga];
|