From d286906e93eae14071c063a964b76331a9914cf2 Mon Sep 17 00:00:00 2001 From: Maksim Chervonnyi Date: Thu, 15 Aug 2024 15:04:07 +0200 Subject: [PATCH] add delete button --- .../Admin/UsersAndRoles/SsoConfig.tsx | 65 +++++++++++++++---- 1 file changed, 54 insertions(+), 11 deletions(-) diff --git a/catalog/app/containers/Admin/UsersAndRoles/SsoConfig.tsx b/catalog/app/containers/Admin/UsersAndRoles/SsoConfig.tsx index 42fd99c1343..d4b12af7bd2 100644 --- a/catalog/app/containers/Admin/UsersAndRoles/SsoConfig.tsx +++ b/catalog/app/containers/Admin/UsersAndRoles/SsoConfig.tsx @@ -46,26 +46,34 @@ function TextField({ errors, input, meta }: TextFieldProps) { } const useStyles = M.makeStyles((t) => ({ - lock: { - bottom: t.spacing(6.5), - top: t.spacing(8), + delete: { + background: t.palette.error.light, + color: t.palette.error.contrastText, + marginRight: 'auto', + '&:hover': { + background: t.palette.error.main, + }, }, error: { marginTop: t.spacing(2), }, + lock: { + bottom: t.spacing(6.5), + top: t.spacing(8), + }, })) type FormValues = Record<'config', string> interface FormProps { - formApi: RF.FormRenderProps close: Dialogs.Close + formApi: RF.FormRenderProps + onDelete: () => Promise ssoConfig: Pick | null } function Form({ close, - ssoConfig, formApi: { error, handleSubmit, @@ -75,6 +83,8 @@ function Form({ submitFailed, submitting, }, + onDelete, + ssoConfig, }: FormProps) { const classes = useStyles() return ( @@ -94,6 +104,14 @@ function Form({ {submitFailed && } + + Delete + close('cancel')} color="primary" disabled={submitting}> Cancel @@ -124,12 +142,9 @@ function Data({ children, close }: DataProps) { loadMode('yaml') const setSsoConfig = GQL.useMutation(SET_SSO_CONFIG_MUTATION) - const onSubmit = React.useCallback( - async ({ config }: FormValues) => { + const submitConfig = React.useCallback( + async (config: string | null) => { try { - if (!config) { - return { config: 'required' } - } const { admin: { setSsoConfig: r }, } = await setSsoConfig({ config }) @@ -154,10 +169,38 @@ function Data({ children, close }: DataProps) { }, [close, setSsoConfig], ) + const onSubmit = React.useCallback( + ({ config }: FormValues) => (config ? submitConfig(config) : { config: 'required' }), + [submitConfig], + ) + const [deleting, setDeleting] = React.useState< + FF.SubmissionErrors | boolean | undefined + >() + const onDelete = React.useCallback(async (): Promise => { + setDeleting(true) + const errors = await submitConfig(null) + setDeleting(errors) + }, [submitConfig]) return ( - {(formApi) => children({ formApi, close, ssoConfig: data.admin?.ssoConfig })} + {(formApi) => + children({ + onDelete, + // eslint-disable-next-line no-nested-ternary + formApi: !deleting + ? formApi + : deleting === true + ? { ...formApi, submitting: true } + : { + ...formApi, + submitError: deleting[FF.FORM_ERROR] || formApi.submitError, + submitFailed: true, + }, + close, + ssoConfig: data.admin?.ssoConfig, + }) + } ) }