Commit 73454e5d authored by Jesús Espino's avatar Jesús Espino Committed by George Goldberg
Browse files

Some extra fixes needed for the scheme edition. (#1254)

* Fixing permissions edition

* Improve redirections

* Fix teams deletion from team schemes

* Removed old scroll on expand behavior
parent f294d324
......@@ -126,6 +126,7 @@ export default class PermissionSchemesSettings extends React.PureComponent {
const schemes = Object.values(this.props.schemes).map((scheme) => (
<PermissionsSchemeSummary
scheme={scheme}
history={this.props.history}
key={scheme.id}
/>
));
......
......@@ -99,7 +99,7 @@ export default class PermissionTeamSchemeSettings extends React.Component {
if (this.state.openRoles.all_users) {
selected.scrollIntoView({behavior: 'smooth', block: 'center'});
} else {
this.toggleRole('all_users', false);
this.toggleRole('all_users');
// Give it time to open and show everything
setTimeout(() => {
......@@ -146,7 +146,7 @@ export default class PermissionTeamSchemeSettings extends React.Component {
channelUser = this.props.roles.channel_user;
channelAdmin = this.props.roles.channel_admin;
} else {
return {};
return null;
}
return {
team_admin: teamAdmin,
......@@ -236,8 +236,22 @@ export default class PermissionTeamSchemeSettings extends React.Component {
const channelUserPromise = this.props.actions.editRole(channelUser);
const teamEditPromises = [];
for (const team of (this.state.teams || this.props.teams || [])) {
teamEditPromises.push(this.props.actions.updateTeamScheme(team.id, schemeId));
const currentTeams = new Set((this.state.teams || this.props.teams || []).map((t) => t.id));
const serverTeams = new Set((this.props.teams || []).map((t) => t.id));
// Difference of sets (currentTeams - serverTeams)
const addedTeams = new Set([...currentTeams].filter((t) => !serverTeams.has(t)));
// Difference of sets (serverTeams - currentTeams)
const removedTeams = new Set([...serverTeams].filter((t) => !currentTeams.has(t)));
for (const teamId of addedTeams) {
teamEditPromises.push(this.props.actions.updateTeamScheme(teamId, schemeId));
}
for (const teamId of removedTeams) {
teamEditPromises.push(this.props.actions.updateTeamScheme(teamId, ''));
}
const results = await Promise.all([teamAdminPromise, channelAdminPromise, teamUserPromise, channelUserPromise, ...teamEditPromises]);
......@@ -253,25 +267,25 @@ export default class PermissionTeamSchemeSettings extends React.Component {
}
this.setState({serverError, saving: false, saveNeeded});
this.props.history.push('/admin_console/permissions/schemes');
}
toggleRole = (roleId, scrollOnOpen = true) => {
toggleRole = (roleId) => {
const newOpenRoles = {...this.state.openRoles};
newOpenRoles[roleId] = !newOpenRoles[roleId];
if (newOpenRoles[roleId] && scrollOnOpen) {
const block = document.querySelector('.permissions-block.' + roleId);
if (block) {
setTimeout(() => {
block.scrollIntoView({behavior: 'smooth', block: 'start'});
}, 300);
}
}
this.setState({openRoles: newOpenRoles});
}
togglePermission = (roleId, permissions) => {
const roles = {...this.getStateRoles()};
const role = {...roles[roleId]};
let role = null;
if (roles.team_admin.name === roleId) {
role = {...roles.team_admin};
} else if (roles.channel_admin.name === roleId) {
role = {...roles.channel_admin};
} else if (roles.all_users.name === roleId) {
role = {...roles.all_users};
}
const newPermissions = [...role.permissions];
for (const permission of permissions) {
if (newPermissions.indexOf(permission) === -1) {
......@@ -281,7 +295,13 @@ export default class PermissionTeamSchemeSettings extends React.Component {
}
}
role.permissions = newPermissions;
roles[roleId] = role;
if (roles.team_admin.name === roleId) {
roles.team_admin = role;
} else if (roles.channel_admin.name === roleId) {
roles.channel_admin = role;
} else if (roles.all_users.name === roleId) {
roles.all_users = role;
}
this.setState({roles, saveNeeded: true});
}
......
......@@ -9,7 +9,6 @@ import {FormattedMessage} from 'react-intl';
import ConfirmModal from 'components/confirm_modal.jsx';
import {browserHistory} from 'utils/browser_history';
import Constants from 'utils/constants.jsx';
const MAX_TEAMS_PER_SCHEME_SUMMARY = 8;
......@@ -120,7 +119,7 @@ export default class PermissionsSchemeSummary extends React.Component {
}
goToEdit = () => {
browserHistory.push('/admin_console/permissions/team-override-scheme/' + this.props.scheme.id);
this.props.history.push('/admin_console/permissions/team-override-scheme/' + this.props.scheme.id);
}
render = () => {
......
......@@ -64,6 +64,9 @@ describe('components/admin_console/permission_schemes_settings/permission_team_s
createScheme: jest.fn(),
updateTeamScheme: jest.fn(),
},
history: {
push: jest.fn(),
},
};
test('should match snapshot on new with default roles without permissions', (done) => {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment