51 lines
1.3 KiB
JavaScript
Raw Normal View History

import _ from 'lodash';
2017-06-20 18:50:37 +02:00
import {takeLatest} from 'redux-saga';
import {fork, put} from 'redux-saga/effects';
2017-03-18 17:34:00 +01:00
2017-06-20 18:50:37 +02:00
import {loadedModels} from './actions';
import {LOAD_MODELS, UPDATE_SCHEMA} 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(
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
}
}
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
const displayedModels = _.filter(action.schema, model => (model.displayed !== false));
// Map links to format them
const leftMenuLinks = _.map(displayedModels, (model, key) => ({
label: model.labelPlural || model.label || key,
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);
yield fork(takeLatest, UPDATE_SCHEMA, schemaUpdated);
2017-03-18 17:34:00 +01:00
}
// All sagas to be loaded
2017-05-11 10:54:44 +02:00
export default [defaultSaga];