Skip to content

Commit

Permalink
MM-12056: Improve the state management for schema admin settings (mat…
Browse files Browse the repository at this point in the history
  • Loading branch information
jespino authored and jwilander committed Sep 17, 2018
1 parent 991537c commit fee61c1
Show file tree
Hide file tree
Showing 4 changed files with 229 additions and 51 deletions.
18 changes: 17 additions & 1 deletion components/admin_console/admin_console.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,22 @@ export default class AdminConsole extends React.Component {
UNSAFE_componentWillMount() { // eslint-disable-line camelcase
this.props.actions.getConfig();
this.props.actions.getEnvironmentConfig();
this.props.actions.loadRolesIfNeeded(['channel_user', 'team_user', 'system_user', 'channel_admin', 'team_admin', 'system_admin']);
reloadIfServerVersionChanged();
}

mainRolesLoaded(roles) {
return (
roles &&
roles.channel_admin &&
roles.channel_user &&
roles.team_admin &&
roles.team_user &&
roles.system_admin &&
roles.system_user
);
}

render() {
const {
license,
Expand All @@ -87,6 +100,10 @@ export default class AdminConsole extends React.Component {
);
}

if (!this.mainRolesLoaded(this.props.roles)) {
return null;
}

if (Object.keys(config).length === 0) {
return <div/>;
}
Expand Down Expand Up @@ -356,7 +373,6 @@ export default class AdminConsole extends React.Component {
extraProps={{
...extraProps,
roles: this.props.roles,
loadRolesIfNeeded: this.props.actions.loadRolesIfNeeded,
editRole: this.props.actions.editRole,
schema: AdminDefinition.settings.integrations.custom_integrations.schema,
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,21 @@ export default class CustomPluginSettings extends SchemaAdminSettings {
constructor(props) {
super(props);
this.isPlugin = true;
this.getStateFromConfig = CustomPluginSettings.getStateFromConfig;
}

UNSAFE_componentWillReceiveProps(nextProps) { // eslint-disable-line camelcase
const id = this.props.schema ? this.props.schema.id : '';
const nextId = nextProps.schema ? nextProps.schema.id : '';

if ((!this.props.schema && nextProps.schema) || (id !== nextId)) {
this.setState(this.getStateFromConfig(nextProps.config, nextProps.schema));
static getDerivedStateFromProps(props, state) {
if (props.schema && props.schema.id !== state.prevSchemaId) {
return {
prevSchemaId: props.schema.id,
saveNeeded: false,
saving: false,
serverError: null,
errorTooltip: false,
...CustomPluginSettings.getStateFromConfig(props.config, props.schema, props.roles),
};
}
return null;
}

getConfigFromState(config) {
Expand Down Expand Up @@ -43,7 +49,7 @@ export default class CustomPluginSettings extends SchemaAdminSettings {
return config;
}

getStateFromConfig(config, schema = this.props.schema) {
static getStateFromConfig(config, schema) {
const state = {};

if (schema) {
Expand Down
3 changes: 3 additions & 0 deletions components/admin_console/custom_plugin_settings/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import {connect} from 'react-redux';

import {getRoles} from 'mattermost-redux/selectors/entities/roles';

import CustomPluginSettings from './custom_plugin_settings.jsx';

function mapStateToProps(state, ownProps) {
Expand All @@ -14,6 +16,7 @@ function mapStateToProps(state, ownProps) {
const translate = (plugin && plugin.translate) || false;
return {
schema: plugin ? {...plugin.settings_schema, id: plugin.id, name: plugin.name, settings, translate} : null,
roles: getRoles(state),
};
}

Expand Down
Loading

0 comments on commit fee61c1

Please sign in to comment.