From 3d03119897957b4da2beab5bcc038be2e8a2f6ea Mon Sep 17 00:00:00 2001 From: cyril lopez Date: Tue, 29 Aug 2017 15:11:06 +0200 Subject: [PATCH] Edit content type in all views --- .../admin/src/containers/App/index.js | 16 +++++++++++- .../admin/src/containers/App/reducer.js | 2 +- .../admin/src/containers/Form/index.js | 26 ++++++++++++++++++- .../admin/src/containers/Form/reducer.js | 11 ++++++-- .../admin/src/containers/Form/sagas.js | 6 +++++ .../admin/src/containers/Form/selectors.js | 6 +++++ .../controllers/ContentTypeBuilder.js | 2 +- 7 files changed, 63 insertions(+), 6 deletions(-) diff --git a/packages/strapi-plugin-content-type-builder/admin/src/containers/App/index.js b/packages/strapi-plugin-content-type-builder/admin/src/containers/App/index.js index 03f61ac2cc..6d57606513 100755 --- a/packages/strapi-plugin-content-type-builder/admin/src/containers/App/index.js +++ b/packages/strapi-plugin-content-type-builder/admin/src/containers/App/index.js @@ -12,8 +12,13 @@ import { createStructuredSelector } from 'reselect'; import { map } from 'lodash'; import { pluginId } from 'app'; import { define } from 'i18n'; + +import { makeSelectShouldRefetchContentType } from 'containers/Form/selectors'; + import { storeData } from '../../utils/storeData'; import messages from '../../translations/en.json'; + + import styles from './styles.scss'; import { modelsFetch } from './actions'; @@ -28,6 +33,12 @@ class App extends React.Component { this.props.modelsFetch(); } + componentWillReceiveProps(nextProps) { + if (nextProps.shouldRefetchContentType !== this.props.shouldRefetchContentType) { + this.props.modelsFetch(); + } + } + componentWillUnmount() { // Empty the app localStorage @@ -58,6 +69,7 @@ App.propTypes = { children: React.PropTypes.node, exposedComponents: React.PropTypes.object.isRequired, modelsFetch: React.PropTypes.func, + shouldRefetchContentType: React.PropTypes.bool, }; export function mapDispatchToProps(dispatch) { @@ -69,7 +81,9 @@ export function mapDispatchToProps(dispatch) { ) } -const mapStateToProps = createStructuredSelector({}); +const mapStateToProps = createStructuredSelector({ + shouldRefetchContentType: makeSelectShouldRefetchContentType(), +}); // Wrap the component to inject dispatch and state into it export default connect(mapStateToProps, mapDispatchToProps)(App); diff --git a/packages/strapi-plugin-content-type-builder/admin/src/containers/App/reducer.js b/packages/strapi-plugin-content-type-builder/admin/src/containers/App/reducer.js index 512519b981..d9af383bfe 100755 --- a/packages/strapi-plugin-content-type-builder/admin/src/containers/App/reducer.js +++ b/packages/strapi-plugin-content-type-builder/admin/src/containers/App/reducer.js @@ -37,7 +37,7 @@ function appReducer(state = initialState, action) { const modelsSize = size(state.get('models').toJS()) return state .updateIn(['menu', '0', 'items'], (list) => list.splice(action.position, action.nbElementToRemove, action.newLink)) - .update('models', array => array.splice(action.nbElementToRemove === 0 ? modelsSize : modelsSize -1 , 1, action.newModel)); + .update('models', array => array.splice(action.nbElementToRemove === 0 ? modelsSize : modelsSize - 1 , 1, action.newModel)); } default: return state; diff --git a/packages/strapi-plugin-content-type-builder/admin/src/containers/Form/index.js b/packages/strapi-plugin-content-type-builder/admin/src/containers/Form/index.js index 54ad82650d..d106f1d919 100644 --- a/packages/strapi-plugin-content-type-builder/admin/src/containers/Form/index.js +++ b/packages/strapi-plugin-content-type-builder/admin/src/containers/Form/index.js @@ -116,7 +116,29 @@ export class Form extends React.Component { // eslint-disable-line react/prefer- } else { this.setState({ showModal: false }); } + } + // Close modal when updating a content type && success updating + if (nextProps.shouldRefetchContentType !== this.props.shouldRefetchContentType) { + // Check if localStorage because the PluginLeftMenu is based on the localStorage + if (storeData.getMenu()) { + // Update localStorage + const oldMenu = storeData.getMenu(); + const index = findIndex(oldMenu, ['name', replace(this.props.hash.split('::')[0], '#edit', '')]); + const modifiedContentType = { + name: this.props.modifiedDataEdit.name, + icon: 'fa-caret-square-o-right', + }; + + oldMenu.splice(index, 1, modifiedContentType); + const newMenu = oldMenu; + storeData.setMenu(newMenu); + } + + // Close Modal + router.push(`${this.props.redirectRoute}/${this.props.modifiedDataEdit.name}`); + // Reset props + this.props.resetDidFetchModelProp(); } } @@ -146,6 +168,8 @@ export class Form extends React.Component { // eslint-disable-line react/prefer- // Store new menu in localStorage and update App leftMenu this.props.storeTemporaryMenu(newMenu, position, index !== -1 ? 1 : 0); + this.props.resetDidFetchModelProp(); + router.push(`${this.props.redirectRoute}/${data.name}`); } @@ -258,7 +282,6 @@ export class Form extends React.Component { // eslint-disable-line react/prefer- toggle = () => { this.props.toggle(); - // Set the didFetchModel props to false when the modal is closing so the store is emptied // Only for editing if (this.state.showModal && includes(this.props.hash, 'edit')) { @@ -349,6 +372,7 @@ Form.propTypes = { selectOptionsFetchSucceeded: React.PropTypes.bool, setAttributeForm: React.PropTypes.func, setForm: React.PropTypes.func.isRequired, + shouldRefetchContentType: React.PropTypes.bool, storeTemporaryMenu: React.PropTypes.func, toggle: React.PropTypes.func.isRequired, }; diff --git a/packages/strapi-plugin-content-type-builder/admin/src/containers/Form/reducer.js b/packages/strapi-plugin-content-type-builder/admin/src/containers/Form/reducer.js index d3580ad498..87aff058c6 100644 --- a/packages/strapi-plugin-content-type-builder/admin/src/containers/Form/reducer.js +++ b/packages/strapi-plugin-content-type-builder/admin/src/containers/Form/reducer.js @@ -9,6 +9,7 @@ import { CHANGE_INPUT, CHANGE_INPUT_ATTRIBUTE, CONNECTIONS_FETCH_SUCCEEDED, + CONTENT_TYPE_ACTION_SUCCEEDED, CONTENT_TYPE_FETCH_SUCCEEDED, RESET_DID_FETCH_MODEL_PROP, SET_ATTRIBUTE_FORM, @@ -28,6 +29,7 @@ const initialState = fromJS({ modifiedDataEdit: Map(), isFormSet: false, didFetchModel: false, + shouldRefetchContentType: false, }); function formReducer(state = initialState, action) { @@ -41,6 +43,10 @@ function formReducer(state = initialState, action) { return state .set('selectOptions', List(action.connections)) .set('selectOptionsFetchSucceeded', !state.get('selectOptionsFetchSucceeded')); + case CONTENT_TYPE_ACTION_SUCCEEDED: + return state + .set('shouldRefetchContentType', !state.get('shouldRefetchContentType')) + .set('initialDataEdit', state.get('modifiedDataEdit')); case CONTENT_TYPE_FETCH_SUCCEEDED: return state .set('didFetchModel', true) @@ -48,12 +54,13 @@ function formReducer(state = initialState, action) { .set('modifiedDataEdit', action.data); case RESET_DID_FETCH_MODEL_PROP: return state - .set('didFetchModel', false); + .set('didFetchModel', false) + .set('isFormSet', false); case SET_ATTRIBUTE_FORM: { if (state.get('isFormSet')) { return state.set('form', Map(action.form)); } - + return state .set('isFormSet', true) .set('form', Map(action.form)) diff --git a/packages/strapi-plugin-content-type-builder/admin/src/containers/Form/sagas.js b/packages/strapi-plugin-content-type-builder/admin/src/containers/Form/sagas.js index 14e16afa91..b83becaa56 100644 --- a/packages/strapi-plugin-content-type-builder/admin/src/containers/Form/sagas.js +++ b/packages/strapi-plugin-content-type-builder/admin/src/containers/Form/sagas.js @@ -34,6 +34,12 @@ export function* editContentType() { const requestUrl = `/content-type-builder/models/${initialContentType.name}`; yield call(request, requestUrl, opts); + + yield new Promise(resolve => { + setTimeout(() => { + resolve(); + }, 5000); + }); yield put(contentTypeActionSucceeded()); } catch(error) { diff --git a/packages/strapi-plugin-content-type-builder/admin/src/containers/Form/selectors.js b/packages/strapi-plugin-content-type-builder/admin/src/containers/Form/selectors.js index c266cf363b..e515ec68e9 100644 --- a/packages/strapi-plugin-content-type-builder/admin/src/containers/Form/selectors.js +++ b/packages/strapi-plugin-content-type-builder/admin/src/containers/Form/selectors.js @@ -39,6 +39,11 @@ const makeSelectDidFetchModel = () => createSelector( (substate) => substate.get('didFetchModel'), ); +const makeSelectShouldRefetchContentType = () => createSelector( + selectFormDomain(), + (substate) => substate.get('shouldRefetchContentType'), +); + export default selectForm; export { selectFormDomain, @@ -46,4 +51,5 @@ export { makeSelectInitialDataEdit, makeSelectModifiedData, makeSelectModifiedDataEdit, + makeSelectShouldRefetchContentType, }; diff --git a/packages/strapi-plugin-content-type-builder/controllers/ContentTypeBuilder.js b/packages/strapi-plugin-content-type-builder/controllers/ContentTypeBuilder.js index 80ca933cda..b281e160a5 100755 --- a/packages/strapi-plugin-content-type-builder/controllers/ContentTypeBuilder.js +++ b/packages/strapi-plugin-content-type-builder/controllers/ContentTypeBuilder.js @@ -85,7 +85,7 @@ module.exports = { if (!name) return ctx.badRequest(null, [{ messages: [{ id: 'request.error.name.missing' }] }]); if (!_.includes(Service.getConnections(), connection)) return ctx.badRequest(null, [{ messages: [{ id: 'request.error.connection.unknow' }] }]); - if (strapi.models[name]) return ctx.badRequest(null, [{ messages: [{ id: 'request.error.model.exist' }] }]); + if (strapi.models[name] && name !== model) return ctx.badRequest(null, [{ messages: [{ id: 'request.error.model.exist' }] }]); if (!strapi.models[model]) return ctx.badRequest(null, [{ messages: [{ id: 'request.error.model.unknow' }] }]); if (!_.isNaN(parseFloat(name[0]))) return ctx.badRequest(null, [{ messages: [{ id: 'request.error.model.name' }] }]);