From f38b4d663b2a49fa82569b82525a85edb8c69e6c Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 15 May 2021 08:55:27 +0200 Subject: [PATCH] Can toggle admin role --- src/helpers/AdminRolesHelper.ts | 18 ++++++ .../accountSettings/RoleSettingsSection.tsx | 61 ++++++++++++++++++- src/ui/routes/AccountSettingsRoute.tsx | 20 +++++- 3 files changed, 95 insertions(+), 4 deletions(-) diff --git a/src/helpers/AdminRolesHelper.ts b/src/helpers/AdminRolesHelper.ts index fa60d4e..3bfe914 100644 --- a/src/helpers/AdminRolesHelper.ts +++ b/src/helpers/AdminRolesHelper.ts @@ -23,4 +23,22 @@ export class AdminRolesHelper { static async LoadRolesList() { RolesList = await serverRequest("roles/list"); } + + /** + * Toggle a role for an administrator + * + * @param adminID Target administrator ID + * @param role The role to toggle + */ + static async ToggleAdminRole( + adminID: number, + role: AdminRole, + enable: boolean + ) { + await serverRequest("roles/toggle", { + adminID: adminID, + role: role.id, + enable: enable, + }); + } } diff --git a/src/ui/accountSettings/RoleSettingsSection.tsx b/src/ui/accountSettings/RoleSettingsSection.tsx index 566310a..3b03ec6 100644 --- a/src/ui/accountSettings/RoleSettingsSection.tsx +++ b/src/ui/accountSettings/RoleSettingsSection.tsx @@ -4,15 +4,74 @@ * @author Pierre Hubert */ +import { Checkbox, FormControlLabel, Tooltip } from "@material-ui/core"; import React from "react"; import { AdminAccount } from "../../helpers/AccountHelper"; +import { + AdminRole, + AdminRolesHelper, + RolesList, +} from "../../helpers/AdminRolesHelper"; +import { matAlert, snackbar } from "../widgets/DialogsProvider"; import { SettingsSection } from "./SettingsSection"; export class RoleSettingsSection extends React.Component<{ admin: AdminAccount; editable: boolean; + onChanged: () => void; }> { + constructor(p: any) { + super(p); + + this.toggleRole = this.toggleRole.bind(this); + } + + async toggleRole(r: AdminRole, enable: boolean) { + try { + await AdminRolesHelper.ToggleAdminRole( + this.props.admin.id, + r, + enable + ); + this.props.onChanged(); + snackbar("Administrator roles were successfully updated!"); + } catch (e) { + console.error(e); + matAlert("Failed to update admin roles!"); + } + } + render() { - return Soon : roles; + return ( + +
+ {RolesList.map((r) => { + const hasRole = this.props.admin.roles.includes( + r.id as any + ); + return ( + + } + label={r.name} + checked={hasRole} + onChange={() => + this.toggleRole(r, !hasRole) + } + style={{ display: "block" }} + > + + ); + })} +
+
+ ); } } diff --git a/src/ui/routes/AccountSettingsRoute.tsx b/src/ui/routes/AccountSettingsRoute.tsx index aa88d7d..8346e75 100644 --- a/src/ui/routes/AccountSettingsRoute.tsx +++ b/src/ui/routes/AccountSettingsRoute.tsx @@ -5,6 +5,7 @@ */ import { Grid } from "@material-ui/core"; +import { version } from "process"; import React from "react"; import { useParams } from "react-router-dom"; import { AccountHelper, AdminAccount } from "../../helpers/AccountHelper"; @@ -27,6 +28,7 @@ interface SettingsRouteProps { interface SettingsRouteState { account: AdminAccount; + version: number; } class AccountSettingsRouteInner extends React.Component< @@ -38,6 +40,17 @@ class AccountSettingsRouteInner extends React.Component< this.load = this.load.bind(this); this.build = this.build.bind(this); + + this.state = { + account: {} as AdminAccount, + version: 1, + }; + + this.reload = this.reload.bind(this); + } + + reload() { + this.setState({ version: this.state.version + 1 }); } async load() { @@ -49,7 +62,7 @@ class AccountSettingsRouteInner extends React.Component< render() { return ( - {this.props.id == adminID() || canManageAdmins() ? ( + {this.props.id === adminID() || canManageAdmins() ? ( ) : (
@@ -78,6 +91,7 @@ class AccountSettingsRouteInner extends React.Component<