From 585a66ef0a7fa9526598dda5a4bd5a26158f041c Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Fri, 14 May 2021 15:04:22 +0200 Subject: [PATCH] Can delete auth keys --- src/helpers/AccountHelper.ts | 13 ++++++++ src/ui/routes/AccountSettingsRoute.tsx | 43 +++++++++++++++++++++++--- src/ui/routes/MainRoute.tsx | 2 +- 3 files changed, 53 insertions(+), 5 deletions(-) diff --git a/src/helpers/AccountHelper.ts b/src/helpers/AccountHelper.ts index 9449174..b5f5785 100644 --- a/src/helpers/AccountHelper.ts +++ b/src/helpers/AccountHelper.ts @@ -265,4 +265,17 @@ export class AccountHelper { id: adminID, }); } + + /** + * Delete an admin auth key + * + * @param adminID The id of the target admin + * @param keyID The id of the key to delete + */ + static async DeleteAuthKey(adminID: number, keyID: number) { + return await serverRequest("accounts/delete_auth_key", { + adminID: adminID, + keyID: keyID, + }); + } } diff --git a/src/ui/routes/AccountSettingsRoute.tsx b/src/ui/routes/AccountSettingsRoute.tsx index 98b00db..947190f 100644 --- a/src/ui/routes/AccountSettingsRoute.tsx +++ b/src/ui/routes/AccountSettingsRoute.tsx @@ -8,6 +8,7 @@ import { Button, Divider, Grid, + IconButton, Paper, Table, TableBody, @@ -17,6 +18,7 @@ import { TextField, Typography, } from "@material-ui/core"; +import { Delete } from "@material-ui/icons"; import React from "react"; import { useParams } from "react-router-dom"; import { @@ -25,7 +27,12 @@ import { AdminAccountKey, } from "../../helpers/AccountHelper"; import { AsyncWidget } from "../widgets/AsyncWidget"; -import { input, matAlert, snackbar } from "../widgets/DialogsProvider"; +import { + input, + matAlert, + matConfirm, + snackbar, +} from "../widgets/DialogsProvider"; import { PageTitle } from "../widgets/PageTitle"; import { TimestampWidget } from "../widgets/TimestampWidget"; @@ -186,6 +193,7 @@ export class KeySettingsSection extends React.Component< this.load = this.load.bind(this); this.build = this.build.bind(this); this.registerNewKey = this.registerNewKey.bind(this); + this.deleteKey = this.deleteKey.bind(this); } async load() { @@ -216,6 +224,25 @@ export class KeySettingsSection extends React.Component< } } + async deleteKey(key: AdminAccountKey) { + try { + if ( + !(await matConfirm( + "Do you really want to delete the key '" + key.name + "' ?" + )) + ) + return; + + await AccountHelper.DeleteAuthKey(this.props.admin.id, key.id); + + snackbar("The key was successfully deleted!"); + this.setState({ counter: this.state.counter + 1 }); + } catch (e) { + console.error(e); + matAlert("Failed to delete key!"); + } + } + render() { return ( + Key name Date added - Actions + @@ -247,7 +274,15 @@ export class KeySettingsSection extends React.Component< - Delete + + this.deleteKey(key)} + > + + + ))} diff --git a/src/ui/routes/MainRoute.tsx b/src/ui/routes/MainRoute.tsx index 339a14b..33d99a9 100644 --- a/src/ui/routes/MainRoute.tsx +++ b/src/ui/routes/MainRoute.tsx @@ -146,7 +146,7 @@ export function MainRoute() { {AccountHelper.currentAccount.email} - +