Skip to content

Commit

Permalink
add delete button
Browse files Browse the repository at this point in the history
  • Loading branch information
fiskus committed Aug 15, 2024
1 parent 60d5f3c commit d286906
Showing 1 changed file with 54 additions and 11 deletions.
65 changes: 54 additions & 11 deletions catalog/app/containers/Admin/UsersAndRoles/SsoConfig.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<FormValues>
close: Dialogs.Close<string | void>
formApi: RF.FormRenderProps<FormValues>
onDelete: () => Promise<void>
ssoConfig: Pick<Model.GQLTypes.SsoConfig, 'text'> | null
}

function Form({
close,
ssoConfig,
formApi: {
error,

Check warning on line 78 in catalog/app/containers/Admin/UsersAndRoles/SsoConfig.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/UsersAndRoles/SsoConfig.tsx#L78

Added line #L78 was not covered by tests
handleSubmit,
Expand All @@ -75,6 +83,8 @@ function Form({
submitFailed,
submitting,
},
onDelete,
ssoConfig,

Check warning on line 87 in catalog/app/containers/Admin/UsersAndRoles/SsoConfig.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/UsersAndRoles/SsoConfig.tsx#L86-L87

Added lines #L86 - L87 were not covered by tests
}: FormProps) {
const classes = useStyles()
return (
Expand All @@ -94,6 +104,14 @@ function Form({
{submitFailed && <FormError error={error || submitError} errors={FORM_ERRORS} />}
</M.DialogContent>
<M.DialogActions>
<M.Button
onClick={onDelete}
color="inherit"
disabled={submitting}
className={classes.delete}
>
Delete
</M.Button>
<M.Button onClick={() => close('cancel')} color="primary" disabled={submitting}>
Cancel
</M.Button>
Expand Down Expand Up @@ -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) => {

Check warning on line 146 in catalog/app/containers/Admin/UsersAndRoles/SsoConfig.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/UsersAndRoles/SsoConfig.tsx#L145-L146

Added lines #L145 - L146 were not covered by tests
try {
if (!config) {
return { config: 'required' }
}
const {
admin: { setSsoConfig: r },
} = await setSsoConfig({ config })
Expand All @@ -154,10 +169,38 @@ function Data({ children, close }: DataProps) {
},
[close, setSsoConfig],
)
const onSubmit = React.useCallback(

Check warning on line 172 in catalog/app/containers/Admin/UsersAndRoles/SsoConfig.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/UsersAndRoles/SsoConfig.tsx#L172

Added line #L172 was not covered by tests
({ config }: FormValues) => (config ? submitConfig(config) : { config: 'required' }),
[submitConfig],
)
const [deleting, setDeleting] = React.useState<

Check warning on line 176 in catalog/app/containers/Admin/UsersAndRoles/SsoConfig.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/UsersAndRoles/SsoConfig.tsx#L176

Added line #L176 was not covered by tests
FF.SubmissionErrors | boolean | undefined
>()
const onDelete = React.useCallback(async (): Promise<void> => {
setDeleting(true)
const errors = await submitConfig(null)
setDeleting(errors)

Check warning on line 182 in catalog/app/containers/Admin/UsersAndRoles/SsoConfig.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/UsersAndRoles/SsoConfig.tsx#L179-L182

Added lines #L179 - L182 were not covered by tests
}, [submitConfig])

return (
<RF.Form onSubmit={onSubmit}>
{(formApi) => children({ formApi, close, ssoConfig: data.admin?.ssoConfig })}
{(formApi) =>
children({

Check warning on line 188 in catalog/app/containers/Admin/UsersAndRoles/SsoConfig.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/UsersAndRoles/SsoConfig.tsx#L188

Added line #L188 was not covered by tests
onDelete,
// eslint-disable-next-line no-nested-ternary
formApi: !deleting
? formApi

Check warning on line 192 in catalog/app/containers/Admin/UsersAndRoles/SsoConfig.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/UsersAndRoles/SsoConfig.tsx#L192

Added line #L192 was not covered by tests
: deleting === true
? { ...formApi, submitting: true }
: {

Check warning on line 195 in catalog/app/containers/Admin/UsersAndRoles/SsoConfig.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/UsersAndRoles/SsoConfig.tsx#L194-L195

Added lines #L194 - L195 were not covered by tests
...formApi,
submitError: deleting[FF.FORM_ERROR] || formApi.submitError,
submitFailed: true,
},
close,
ssoConfig: data.admin?.ssoConfig,
})
}
</RF.Form>
)
}
Expand Down

0 comments on commit d286906

Please sign in to comment.