From 20ab1e3a498f13965118a7c00a99483abee8ad85 Mon Sep 17 00:00:00 2001 From: HelaKaraa Date: Tue, 1 Oct 2024 13:19:27 +0200 Subject: [PATCH] feat: allow bulk admin rights modification Fix #754 --- izanami-frontend/src/pages/users.tsx | 148 +++++++++++++++----- izanami-frontend/src/utils/types.ts | 1 + test/fr/maif/izanami/api/BaseAPISpec.scala | 1 - test/fr/maif/izanami/api/UsersAPISpec.scala | 1 - 4 files changed, 116 insertions(+), 35 deletions(-) diff --git a/izanami-frontend/src/pages/users.tsx b/izanami-frontend/src/pages/users.tsx index 17159ee0f..b44ecb8e9 100644 --- a/izanami-frontend/src/pages/users.tsx +++ b/izanami-frontend/src/pages/users.tsx @@ -39,8 +39,7 @@ export function Users() { const [bulkOperation, setBulkOperation] = useState( undefined ); - - const BULK_OPERATIONS = ["Delete"] as const; + const BULK_OPERATIONS = ["Delete", "Toggle Admin Role"] as const; const isAdmin = useAdmin(); const context = useContext(IzanamiContext); const isTenantAdmin = Boolean( @@ -59,7 +58,6 @@ export function Users() { }, } ); - const userUpdateMutation = useMutation( (user: { username: string; admin: boolean; rights: TRights }) => { const { username, ...rest } = user; @@ -90,6 +88,115 @@ export function Users() { createInvitation(data.email, data.admin, data.rights) ); + function OperationToggleForm(props: { + bulkOperation: string; + selectedRows: UserType[]; + cancel: () => void; + }) { + const { bulkOperation, selectedRows, cancel } = props; + switch (bulkOperation) { + case "Toggle Admin Role": { + const adminOptions = [ + { + label: "Add Admin right", + value: "true", + }, + { + label: "Remove Admin right", + value: "false", + }, + ]; + + return ( + <> +
"", + type: "string", + format: "select", + props: { styles: customStyles }, + placeholder: "Select Admin Role...", + options: adminOptions, + }, + }} + onSubmit={(ctx) => { + return askConfirmation( + `Are you sure you want to change admin role for ${ + selectedRows.length + } user${selectedRows.length > 1 ? "s" : ""}?`, + () => { + return Promise.all( + selectedRows.map((row) => { + return fetch(`/api/admin/users/${row.username}`) + .then((response) => { + return response.json(); + }) + .then((data) => { + return userUpdateMutation.mutateAsync({ + username: row.username, + admin: ctx.admin === "true", + rights: data.rights, + }); + }); + }) + ).then(cancel); + } + ); + }} + footer={({ valid }: { valid: () => void }) => { + return ( +
+ + +
+ ); + }} + /> + + ); + } + default: + return ( + + ); + } + } + if (userQuery.isLoading) { return ; } else if (userQuery.isSuccess) { @@ -254,36 +361,11 @@ export function Users() { />   {bulkOperation && ( - <> - - + setBulkOperation(undefined)} + /> )}