From 54c9b84945927cacc92baf612ba3c54bb9b36503 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 15 May 2021 10:05:56 +0200 Subject: [PATCH] Can create admin accounts --- src/helpers/AccountHelper.ts | 10 ++++++++++ src/ui/routes/AccountsListRoute.tsx | 28 +++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/helpers/AccountHelper.ts b/src/helpers/AccountHelper.ts index 963d97f..397473c 100644 --- a/src/helpers/AccountHelper.ts +++ b/src/helpers/AccountHelper.ts @@ -97,6 +97,16 @@ export class AccountHelper { return await serverRequest("accounts/list"); } + /** + * Create a new administrator + */ + static async CreateAdmin(name: string, email: string) { + await serverRequest("accounts/create", { + name: name, + mail: email, + }); + } + /** * Attempt to refresh current account information */ diff --git a/src/ui/routes/AccountsListRoute.tsx b/src/ui/routes/AccountsListRoute.tsx index 387e991..845c1d7 100644 --- a/src/ui/routes/AccountsListRoute.tsx +++ b/src/ui/routes/AccountsListRoute.tsx @@ -21,6 +21,7 @@ import { Link } from "react-router-dom"; import { AccountHelper, AdminAccount } from "../../helpers/AccountHelper"; import { canManageAdmins } from "../../utils/AccountUtils"; import { AsyncWidget } from "../widgets/AsyncWidget"; +import { input, matAlert, snackbar } from "../widgets/DialogsProvider"; import { PageTitle } from "../widgets/PageTitle"; import { TimestampWidget } from "../widgets/TimestampWidget"; @@ -37,6 +38,7 @@ export class AccountsListRoute extends React.Component< }; this.load = this.load.bind(this); + this.createAccount = this.createAccount.bind(this); this.build = this.build.bind(this); } @@ -56,6 +58,30 @@ export class AccountsListRoute extends React.Component< ); } + async createAccount() { + try { + const name = await input({ + label: "Administrator name", + minLength: 5, + title: "New administrator", + }); + + const email = await input({ + label: "Administrator email", + minLength: 5, + title: "New administrator", + }); + + await AccountHelper.CreateAdmin(name, email); + + snackbar("Account successfully created!"); + this.setState({ version: this.state.version + 1 }); + } catch (e) { + console.error(e); + matAlert("Failed to create admin account!"); + } + } + build() { return (
@@ -63,7 +89,7 @@ export class AccountsListRoute extends React.Component< name="Administrators list" actions={ canManageAdmins() ? ( - + ) : (