From 3e9b1a70f6d8086a13bd6cb1054d8f468fc20c17 Mon Sep 17 00:00:00 2001 From: nl_0 Date: Tue, 18 Jun 2024 09:29:56 +0200 Subject: [PATCH] RoleSwitcher: add button for confirming role switch --- .../app/containers/NavBar/RoleSwitcher.tsx | 153 +++++++++++------- 1 file changed, 96 insertions(+), 57 deletions(-) diff --git a/catalog/app/containers/NavBar/RoleSwitcher.tsx b/catalog/app/containers/NavBar/RoleSwitcher.tsx index 6e83780bdf2..22e27a4ac08 100644 --- a/catalog/app/containers/NavBar/RoleSwitcher.tsx +++ b/catalog/app/containers/NavBar/RoleSwitcher.tsx @@ -1,4 +1,6 @@ +import * as FF from 'final-form' import * as React from 'react' +import * as RF from 'react-final-form' import * as M from '@material-ui/core' import * as Lab from '@material-ui/lab' import * as Sentry from '@sentry/react' @@ -49,18 +51,24 @@ interface RoleSwitcherProps { function RoleSwitcher({ user, close }: RoleSwitcherProps) { const switchRole = GQL.useMutation(SWITCH_ROLE_MUTATION) const classes = useRoleSwitcherStyles() - const [loading, setLoading] = React.useState(false) - const [error, setError] = React.useState(null) - const selectRole = React.useCallback( - async (roleName: string) => { - if (roleName === user.role.name) return close() - setLoading(true) + + interface FormValues { + role: string + } + + const onSubmit = React.useCallback( + async ({ role }: FormValues) => { + if (role === user.role.name) { + close() + return new Promise(() => {}) // don't unlock the form controls + } + try { - const { switchRole: r } = await switchRole({ roleName }) + const { switchRole: r } = await switchRole({ roleName: role }) switch (r.__typename) { case 'Me': window.location.reload() - break + return await new Promise(() => {}) // don't unlock the form controls case 'InvalidInput': const [e] = r.errors throw new Error(`InputError (${e.name}) at '${e.path}': ${e.message}`) @@ -73,61 +81,92 @@ function RoleSwitcher({ user, close }: RoleSwitcherProps) { // eslint-disable-next-line no-console console.error('Error switching role', err) Sentry.captureException(err) - if (err instanceof Error) { - setError(err.message) - } else { - setError(`${err}`) - } - setLoading(false) + const message = err instanceof Error ? err.message : `${err}` + return { [FF.FORM_ERROR]: message } } }, [close, switchRole, user.role.name], ) return ( - <> - Switch role -
- - - Could not switch role - Try again or contact support. -
- Error details: -
- {error} -
-
- - {user.roles.map((role) => ( - selectRole(role.name)} - selected={role.name === user.role.name} - > - - - - - {role.name} - - - ))} - - {loading && ( -
- + initialValues={{ role: user.role.name }} onSubmit={onSubmit}> + {({ handleSubmit, submitting, submitError }) => ( + <> + Switch role + +
+ + + Could not switch role + Try again or contact support. +
+ Error details: +
+ {submitError} +
+
+
+ +
+ name="role"> + {(props) => ( + + {user.roles.map((role) => ( + props.input.onChange(role.name)} + selected={role.name === props.input.value} + > + + + + + {role.name} + {role.name === user.role.name && ( + +  (current) + + )} + + + ))} + + )} + + {submitting && ( +
+ +
+ )}
- )} -
- + + + + Cancel + + + Switch + + + + )} + ) }