PUT new settings

This commit is contained in:
cyril lopez 2018-07-05 17:57:30 +02:00
parent 56152f4462
commit 23a354092b
10 changed files with 79 additions and 41 deletions

View File

@ -18,6 +18,7 @@ import {
ON_REMOVE,
ON_RESET,
ON_SUBMIT,
SUBMIT_SUCCEEDED,
} from './constants';
export function emptyStore() {
@ -109,4 +110,10 @@ export function onSubmit() {
return {
type: ON_SUBMIT,
};
}
export function submitSucceeded() {
return {
type: SUBMIT_SUCCEEDED,
};
}

View File

@ -16,3 +16,4 @@ export const ON_CLICK_ADD_ATTR = 'contentManager/App/ON_CLICK_ADD_ATTR';
export const ON_REMOVE = 'contentManager/App/ON_REMOVE';
export const ON_RESET = 'contentManager/App/ON_RESET';
export const ON_SUBMIT = 'contentManager/App/ON_SUBMIT';
export const SUBMIT_SUCCEEDED = 'contentManager/App/SUBMIT_SUCCEEDED';

View File

@ -16,15 +16,16 @@ import {
ON_CLICK_ADD_ATTR,
ON_REMOVE,
ON_RESET,
ON_SUBMIT,
SUBMIT_SUCCEEDED,
} from './constants';
const initialState = fromJS({
modelEntries: 0,
formValidations: List([]),
loading: true,
modelEntries: 0,
modifiedSchema: fromJS({}),
schema: fromJS({}),
formValidations: List([]),
submitSuccess: false,
});
function appReducer(state = initialState, action) {
@ -101,8 +102,9 @@ function appReducer(state = initialState, action) {
case ON_RESET:
return state
.update('modifiedSchema', () => state.get('schema'));
case ON_SUBMIT:
case SUBMIT_SUCCEEDED:
return state
.update('submitSuccess', v => v = !v)
.update('schema', () => state.get('modifiedSchema'));
default:
return state;

View File

@ -1,10 +1,11 @@
import { LOCATION_CHANGE } from 'react-router-redux';
import { fork, put, call, takeLatest, take, cancel } from 'redux-saga/effects';
import { fork, put, call, takeLatest, take, cancel, select } from 'redux-saga/effects';
import request from 'utils/request';
import { getModelEntriesSucceeded, loadedModels } from './actions';
import { GET_MODEL_ENTRIES, LOAD_MODELS } from './constants';
import { getModelEntriesSucceeded, loadedModels, submitSucceeded } from './actions';
import { GET_MODEL_ENTRIES, LOAD_MODELS, ON_SUBMIT } from './constants';
import { makeSelectModifiedSchema } from './selectors';
export function* modelEntriesGet(action) {
try {
@ -29,10 +30,23 @@ export function* getModels() {
}
}
export function* submit() {
try {
const schema = yield select(makeSelectModifiedSchema());
yield call(request, '/content-manager/models', { method: 'PUT', body: { schema } });
yield put(submitSucceeded());
} catch(err) {
// Silent
// NOTE: should we add another notification??
}
}
// Individual exports for testing
export function* defaultSaga() {
const loadModelsWatcher = yield fork(takeLatest, LOAD_MODELS, getModels);
const loadEntriesWatcher = yield fork(takeLatest, GET_MODEL_ENTRIES, modelEntriesGet);
yield fork(takeLatest, ON_SUBMIT, submit);
yield take(LOCATION_CHANGE);

View File

@ -44,6 +44,9 @@ const makeSelectSchema = () =>
const makeSelectModifiedSchema = () =>
createSelector(selectGlobalDomain(), substate => substate.get('modifiedSchema').toJS());
const makeSelectSubmitSuccess = () =>
createSelector(selectGlobalDomain(), substate => substate.get('submitSuccess'));
export {
selectGlobalDomain,
selectLocationState,
@ -51,4 +54,5 @@ export {
makeSelectModelEntries,
makeSelectModifiedSchema,
makeSelectSchema,
makeSelectSubmitSuccess,
};

View File

@ -24,7 +24,7 @@ import {
onReset,
onSubmit,
} from 'containers/App/actions';
import { makeSelectModifiedSchema } from 'containers/App/selectors';
import { makeSelectModifiedSchema , makeSelectSubmitSuccess } from 'containers/App/selectors';
import BackHeader from 'components/BackHeader';
import Input from 'components/InputsIndex';
@ -52,6 +52,16 @@ class SettingPage extends React.PureComponent {
this.handleClickEditAttr(0);
}
componentDidUpdate(prevProps) {
if (prevProps.submitSuccess !== this.props.submitSuccess) {
this.toggle();
}
}
componentWillUnmount() {
this.props.onReset();
}
getDefaultSort = () => this.getValue(`${this.getPath()}.defaultSort`, 'string');
getDropDownItems = () => {
@ -129,7 +139,6 @@ class SettingPage extends React.PureComponent {
const value = get(this.props.schema, ['models'].concat(keys.split('.')));
return type === 'toggle' ? value : value.toString();
}
handleChange = (e) => {
@ -156,7 +165,7 @@ class SettingPage extends React.PureComponent {
}
handleClickEditAttr = (index) => {
const attrToEdit = get(this.props.schema, ['models', this.getModelName()].concat(['listDisplay', index]), {});
const attrToEdit = get(this.props.schema, ['models'].concat(this.getPath().split('.')).concat(['listDisplay', index]), {});
this.props.onClickEditListItem(attrToEdit);
}
@ -229,7 +238,6 @@ class SettingPage extends React.PureComponent {
popUpWarningType="danger"
onConfirm={() => {
onSubmit();
this.toggle();
}}
/>
<div className={cn('row', styles.container)}>
@ -377,6 +385,7 @@ SettingPage.propTypes = {
onSubmit: PropTypes.func.isRequired,
schema: PropTypes.object.isRequired,
settingPage: PropTypes.object.isRequired,
submitSuccess: PropTypes.bool.isRequired,
};
const mapDispatchToProps = (dispatch) => (
@ -397,6 +406,7 @@ const mapDispatchToProps = (dispatch) => (
const mapStateToProps = createStructuredSelector({
schema: makeSelectModifiedSchema(),
settingPage: makeSelectSettingPage(),
submitSuccess: makeSelectSubmitSuccess(),
});
const withConnect = connect(mapStateToProps, mapDispatchToProps);

View File

@ -12,7 +12,7 @@ import { get, sortBy } from 'lodash';
import PropTypes from 'prop-types';
import { onChange, onSubmit, onReset } from 'containers/App/actions';
import { makeSelectModifiedSchema } from 'containers/App/selectors';
import { makeSelectModifiedSchema, makeSelectSubmitSuccess } from 'containers/App/selectors';
import Input from 'components/InputsIndex';
import PluginHeader from 'components/PluginHeader';
@ -33,6 +33,12 @@ import forms from './forms.json';
class SettingsPage extends React.PureComponent {
state = { showWarning: false };
componentDidUpdate(prevProps) {
if (prevProps.submitSuccess !== this.props.submitSuccess) {
this.toggle();
}
}
getModels = (data = this.props.schema.models, destination = '/') => {
const models = Object.keys(data).reduce((acc, curr) => {
if (curr !== 'plugins') {
@ -112,7 +118,6 @@ class SettingsPage extends React.PureComponent {
popUpWarningType="danger"
onConfirm={() => {
onSubmit();
this.toggle();
}}
/>
<div className={cn('row', styles.container)}>
@ -160,6 +165,7 @@ SettingsPage.propTypes = {
onReset: PropTypes.func.isRequired,
onSubmit: PropTypes.func.isRequired,
schema: PropTypes.object.isRequired,
submitSuccess: PropTypes.bool.isRequired,
};
const mapDispatchToProps = (dispatch) => (
@ -175,6 +181,7 @@ const mapDispatchToProps = (dispatch) => (
const mapStateToProps = createStructuredSelector({
schema: makeSelectModifiedSchema(),
submitSuccess: makeSelectSubmitSuccess(),
});
const withConnect = connect(mapStateToProps, mapDispatchToProps);

View File

@ -227,7 +227,7 @@ module.exports = async cb => {
});
});
pluginStore.set({ key: 'schema', value: prevSchema });
await pluginStore.set({ key: 'schema', value: prevSchema });
} catch(err) {
console.log('error', err);

View File

@ -32,6 +32,14 @@
"policies": ["routing"]
}
},
{
"method": "PUT",
"path": "/models",
"handler": "ContentManager.updateSettings",
"config": {
"policies": ["routing"]
}
},
{
"method": "GET",
"path": "/explorer/:model/:id",

View File

@ -14,33 +14,6 @@ module.exports = {
},
models: async ctx => {
// const pickData = (model) => _.pick(model, [
// 'info',
// 'connection',
// 'collectionName',
// 'attributes',
// 'identity',
// 'globalId',
// 'globalName',
// 'orm',
// 'loadedModel',
// 'primaryKey',
// 'associations'
// ]);
// const models = _.mapValues(strapi.models, pickData);
// delete models['core_store'];
// ctx.body = {
// models,
// plugins: Object.keys(strapi.plugins).reduce((acc, current) => {
// acc[current] = {
// models: _.mapValues(strapi.plugins[current].models, pickData)
// };
// return acc;
// }, {})
// };
const pluginsStore = strapi.store({
environment: '',
type: 'plugin',
@ -115,6 +88,18 @@ module.exports = {
}
},
updateSettings: async ctx => {
const { schema } = ctx.request.body;
const pluginStore = strapi.store({
environment: '',
type: 'plugin',
name: 'content-manager'
});
await pluginStore.set({ key: 'schema', value: schema });
return ctx.body = { ok: true };
},
delete: async ctx => {
ctx.body = await strapi.plugins['content-manager'].services['contentmanager'].delete(ctx.params, ctx.request.query);
},