79 lines
2.3 KiB
JavaScript
Raw Normal View History

2017-11-27 17:27:16 +01:00
import { map, omit } 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 { getModelEntriesSucceeded, loadedModels, updateSchema } from './actions';
import { GET_MODEL_ENTRIES, 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
export function* modelEntriesGet(action) {
try {
2017-11-27 17:27:16 +01:00
const requestUrl = `${strapi.backendURL}/content-manager/explorer/${action.modelName}/count${action.source !== undefined ? `?source=${action.source}`: ''}`;
const response = yield call(request, requestUrl, { method: 'GET' });
yield put(getModelEntriesSucceeded(response.count));
} catch(error) {
strapi.notification.error('content-manager.error.model.fetch');
}
}
export const generateMenu = function () {
return request(`${strapi.backendURL}/content-manager/models`, {
2017-09-25 15:35:27 +02:00
method: 'GET',
})
.then(response => generateSchema(response))
.then(displayedModels => {
return [{
name: 'Content Types',
2017-11-27 17:27:16 +01:00
links: map(omit(displayedModels, 'plugins'), (model, key) => ({
2017-09-25 15:35:27 +02:00
label: model.labelPlural || model.label || key,
destination: key,
})),
}];
})
2017-09-27 17:22:21 +02:00
.catch((error) => {
strapi.notification.error('content-manager.error.model.fetch');
2017-09-27 17:22:21 +02:00
throw Error(error);
2017-09-25 15:35:27 +02:00
});
2017-10-13 16:46:18 +02:00
};
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,
`${strapi.backendURL}/content-manager/models`, {
2017-07-21 18:15:43 +02:00
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) {
strapi.notification.error('content-manager.error.model.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) {
strapi.notification.error('content-manager.error.schema.generation');
2017-07-06 17:51:13 +02:00
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);
yield fork(takeLatest, GET_MODEL_ENTRIES, modelEntriesGet);
2017-03-18 17:34:00 +01:00
}
// All sagas to be loaded
2017-08-29 17:32:48 +02:00
export default defaultSaga;