From 1251762711ed96d499d8de04b7ee885e594e1764 Mon Sep 17 00:00:00 2001 From: Thomas Churchman Date: Thu, 18 Jan 2024 15:31:48 +0100 Subject: [PATCH] feat: ask confirmation when changing own kit role --- .../scenes/kit/access/components/Members.tsx | 203 ++++++++++-------- 1 file changed, 114 insertions(+), 89 deletions(-) diff --git a/astroplant-frontend/src/scenes/kit/access/components/Members.tsx b/astroplant-frontend/src/scenes/kit/access/components/Members.tsx index b641d65..cb1f5b8 100644 --- a/astroplant-frontend/src/scenes/kit/access/components/Members.tsx +++ b/astroplant-frontend/src/scenes/kit/access/components/Members.tsx @@ -10,7 +10,7 @@ import style from "./Members.module.css"; import clsx from "clsx"; import { IconCheck, IconPlus, IconX } from "@tabler/icons-react"; import { ChangeEvent, useEffect, useMemo, useRef, useState } from "react"; -import { ModalDialog } from "~/Components/ModalDialog"; +import { ModalDialog, useModalConfirmDialog } from "~/Components/ModalDialog"; import { Input } from "~/Components/Input"; import { useAppSelector, useDebounce } from "~/hooks"; import { selectMe } from "~/modules/me/reducer"; @@ -143,6 +143,11 @@ function RoleSelector({ const configure = role.accessConfigure && !role.accessSuper; const admin = role.accessSuper; + const { element: confirmElement, trigger: confirmTrigger } = + useModalConfirmDialog(); + + const me = useAppSelector(selectMe); + let currentRole = "View-only"; if (configure) { currentRole = "Configure"; @@ -168,7 +173,24 @@ function RoleSelector({ }, [error]); const onChange = useMemo( - () => (e: ChangeEvent) => { + () => async (e: ChangeEvent) => { + if (kitMembership.user.username === me.username) { + const confirmed = await confirmTrigger({ + header: "Are you sure?", + body: ( + <> + Are you sure you wish to change your own role? If you confirm, + your role will be changed immediately. You may not be able to + change your role back. + + ), + }); + + if (!confirmed) { + return; + } + } + const newRole = e.target.value; let patch: null | schemas["PatchKitMembership"] = null; switch (newRole) { @@ -191,97 +213,100 @@ function RoleSelector({ ); return ( - - Role: - {currentRole} - - } - title="Change this member's role" - header="Choose a role" - anchor="right" - > -
e.preventDefault}> -
    -
  • { - refRadioViewOnly.current!.click(); - }} - > - -
    - -
    -
    -
    View-only
    -
    Can view and download data
    -
    -
  • -
  • { - refRadioConfigure.current!.click(); - }} - > - -
    - -
    -
    -
    Configure
    + <> + {confirmElement} + + Role: + {currentRole} +
    + } + title="Change this member's role" + header="Choose a role" + anchor="right" + > + e.preventDefault}> +
      +
    • { + refRadioViewOnly.current!.click(); + }} + > +
      - Can view and download data, add and delete configurations +
      - -
    • -
    • { - refRadioAdmin.current!.click(); - }} - > - -
      - -
      -
      -
      Admin
      - Can view and download data, add and delete configurations, - manage members +
      View-only
      +
      Can view and download data
      -
      -
    • -
    -
  • - + +
  • { + refRadioConfigure.current!.click(); + }} + > + +
    + +
    +
    +
    Configure
    +
    + Can view and download data, add and delete configurations +
    +
    +
  • +
  • { + refRadioAdmin.current!.click(); + }} + > + +
    + +
    +
    +
    Admin
    +
    + Can view and download data, add and delete configurations, + manage members +
    +
    +
  • +
+ +
+ ); }