Merge pull request #2306 from automatisch/AUT-1388
feat: introduce two separate forms in update auth client dialog
This commit is contained in:
@@ -0,0 +1,148 @@
|
|||||||
|
import * as React from 'react';
|
||||||
|
import PropTypes from 'prop-types';
|
||||||
|
import LoadingButton from '@mui/lab/LoadingButton';
|
||||||
|
import Alert from '@mui/material/Alert';
|
||||||
|
import Dialog from '@mui/material/Dialog';
|
||||||
|
import DialogContent from '@mui/material/DialogContent';
|
||||||
|
import DialogContentText from '@mui/material/DialogContentText';
|
||||||
|
import DialogTitle from '@mui/material/DialogTitle';
|
||||||
|
import CircularProgress from '@mui/material/CircularProgress';
|
||||||
|
import Divider from '@mui/material/Divider';
|
||||||
|
import IconButton from '@mui/material/IconButton';
|
||||||
|
import Stack from '@mui/material/Stack';
|
||||||
|
import CloseIcon from '@mui/icons-material/Close';
|
||||||
|
|
||||||
|
import { FieldPropType } from 'propTypes/propTypes';
|
||||||
|
import useFormatMessage from 'hooks/useFormatMessage';
|
||||||
|
import InputCreator from 'components/InputCreator';
|
||||||
|
import Switch from 'components/Switch';
|
||||||
|
import TextField from 'components/TextField';
|
||||||
|
import { Form } from './style';
|
||||||
|
|
||||||
|
function AdminApplicationOAuthClientUpdateDialog(props) {
|
||||||
|
const {
|
||||||
|
error,
|
||||||
|
onClose,
|
||||||
|
title,
|
||||||
|
loading,
|
||||||
|
authFields,
|
||||||
|
defaultValues,
|
||||||
|
disabled = false,
|
||||||
|
submitAuthDefault,
|
||||||
|
submitBasicData,
|
||||||
|
submittingBasicData,
|
||||||
|
submittingAuthDefaults,
|
||||||
|
} = props;
|
||||||
|
const formatMessage = useFormatMessage();
|
||||||
|
const { name, active, ...formattedAuthDefaults } = defaultValues;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Dialog open={true} onClose={onClose}>
|
||||||
|
<IconButton
|
||||||
|
aria-label="delete"
|
||||||
|
sx={{ position: 'absolute', top: 10, right: 10 }}
|
||||||
|
onClick={onClose}
|
||||||
|
>
|
||||||
|
<CloseIcon />
|
||||||
|
</IconButton>
|
||||||
|
<DialogTitle>{title}</DialogTitle>
|
||||||
|
{error && (
|
||||||
|
<Alert severity="error" sx={{ mt: 1, wordBreak: 'break-all' }}>
|
||||||
|
{error.message}
|
||||||
|
</Alert>
|
||||||
|
)}
|
||||||
|
<DialogContent>
|
||||||
|
{loading ? (
|
||||||
|
<CircularProgress
|
||||||
|
data-test="search-for-app-loader"
|
||||||
|
sx={{ display: 'block', margin: '20px auto' }}
|
||||||
|
/>
|
||||||
|
) : (
|
||||||
|
<DialogContentText tabIndex={-1} component="div">
|
||||||
|
<Stack spacing={4}>
|
||||||
|
<Form
|
||||||
|
data-test="auth-client-form-update-basic-data"
|
||||||
|
onSubmit={submitBasicData}
|
||||||
|
defaultValues={{
|
||||||
|
active,
|
||||||
|
name,
|
||||||
|
}}
|
||||||
|
render={({ formState: { isDirty } }) => (
|
||||||
|
<>
|
||||||
|
<Switch
|
||||||
|
name="active"
|
||||||
|
label={formatMessage('oauthClient.inputActive')}
|
||||||
|
/>
|
||||||
|
<TextField
|
||||||
|
required={true}
|
||||||
|
name="name"
|
||||||
|
label={formatMessage('oauthClient.inputName')}
|
||||||
|
fullWidth
|
||||||
|
/>
|
||||||
|
<LoadingButton
|
||||||
|
data-test="submit-auth-client-form-update-basic-data"
|
||||||
|
type="submit"
|
||||||
|
variant="contained"
|
||||||
|
color="primary"
|
||||||
|
sx={{ boxShadow: 2 }}
|
||||||
|
loading={submittingBasicData}
|
||||||
|
disabled={disabled || !isDirty}
|
||||||
|
>
|
||||||
|
{formatMessage('oauthClient.buttonSubmit')}
|
||||||
|
</LoadingButton>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
{authFields?.length > 0 && (
|
||||||
|
<>
|
||||||
|
<Divider />
|
||||||
|
<Form
|
||||||
|
data-test="auth-client-form-update-auth-defaults"
|
||||||
|
onSubmit={submitAuthDefault}
|
||||||
|
defaultValues={formattedAuthDefaults}
|
||||||
|
render={({ formState: { isDirty } }) => (
|
||||||
|
<>
|
||||||
|
{authFields?.map((field) => (
|
||||||
|
<InputCreator key={field.key} schema={field} />
|
||||||
|
))}
|
||||||
|
<LoadingButton
|
||||||
|
data-test="submit-auth-client-form-update-auth-defaults"
|
||||||
|
type="submit"
|
||||||
|
variant="contained"
|
||||||
|
color="primary"
|
||||||
|
sx={{ boxShadow: 2 }}
|
||||||
|
loading={submittingAuthDefaults}
|
||||||
|
disabled={disabled || !isDirty}
|
||||||
|
>
|
||||||
|
{formatMessage('oauthClient.buttonSubmit')}
|
||||||
|
</LoadingButton>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
</Stack>
|
||||||
|
</DialogContentText>
|
||||||
|
)}
|
||||||
|
</DialogContent>
|
||||||
|
</Dialog>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
AdminApplicationOAuthClientUpdateDialog.propTypes = {
|
||||||
|
error: PropTypes.shape({
|
||||||
|
message: PropTypes.string,
|
||||||
|
}),
|
||||||
|
onClose: PropTypes.func.isRequired,
|
||||||
|
title: PropTypes.string.isRequired,
|
||||||
|
loading: PropTypes.bool.isRequired,
|
||||||
|
submitAuthDefault: PropTypes.func.isRequired,
|
||||||
|
submitBasicData: PropTypes.func.isRequired,
|
||||||
|
authFields: PropTypes.arrayOf(FieldPropType),
|
||||||
|
submittingBasicData: PropTypes.bool.isRequired,
|
||||||
|
submittingAuthDefaults: PropTypes.bool.isRequired,
|
||||||
|
defaultValues: PropTypes.object.isRequired,
|
||||||
|
disabled: PropTypes.bool,
|
||||||
|
};
|
||||||
|
|
||||||
|
export default AdminApplicationOAuthClientUpdateDialog;
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
import { styled } from '@mui/material/styles';
|
||||||
|
import BaseForm from 'components/Form';
|
||||||
|
export const Form = styled(BaseForm)(({ theme }) => ({
|
||||||
|
display: 'flex',
|
||||||
|
flexDirection: 'column',
|
||||||
|
gap: theme.spacing(2),
|
||||||
|
paddingTop: theme.spacing(1),
|
||||||
|
}));
|
||||||
@@ -1,10 +1,11 @@
|
|||||||
import PropTypes from 'prop-types';
|
import PropTypes from 'prop-types';
|
||||||
import React, { useCallback, useMemo } from 'react';
|
import React, { useEffect, useMemo, useState } from 'react';
|
||||||
import { useParams } from 'react-router-dom';
|
import { useParams } from 'react-router-dom';
|
||||||
|
|
||||||
import { AppPropType } from 'propTypes/propTypes';
|
import { AppPropType } from 'propTypes/propTypes';
|
||||||
import useFormatMessage from 'hooks/useFormatMessage';
|
import useFormatMessage from 'hooks/useFormatMessage';
|
||||||
import AdminApplicationOAuthClientDialog from 'components/AdminApplicationOAuthClientDialog';
|
import AdminApplicationOAuthClientUpdateDialog from 'components/AdminApplicationOAuthClientUpdateDialog';
|
||||||
|
import useEnqueueSnackbar from 'hooks/useEnqueueSnackbar';
|
||||||
import useAdminOAuthClient from 'hooks/useAdminOAuthClient.ee';
|
import useAdminOAuthClient from 'hooks/useAdminOAuthClient.ee';
|
||||||
import useAdminUpdateOAuthClient from 'hooks/useAdminUpdateOAuthClient.ee';
|
import useAdminUpdateOAuthClient from 'hooks/useAdminUpdateOAuthClient.ee';
|
||||||
import useAppAuth from 'hooks/useAppAuth';
|
import useAppAuth from 'hooks/useAppAuth';
|
||||||
@@ -13,40 +14,68 @@ function AdminApplicationUpdateOAuthClient(props) {
|
|||||||
const { application, onClose } = props;
|
const { application, onClose } = props;
|
||||||
const formatMessage = useFormatMessage();
|
const formatMessage = useFormatMessage();
|
||||||
const { clientId } = useParams();
|
const { clientId } = useParams();
|
||||||
|
const enqueueSnackbar = useEnqueueSnackbar();
|
||||||
|
const [defaultValues, setDefaultValues] = useState({
|
||||||
|
name: '',
|
||||||
|
active: false,
|
||||||
|
});
|
||||||
|
|
||||||
const { data: adminOAuthClient, isLoading: isAdminOAuthClientLoading } =
|
const { data: adminOAuthClient, isLoading: isAdminOAuthClientLoading } =
|
||||||
useAdminOAuthClient(application.key, clientId);
|
useAdminOAuthClient(application.key, clientId);
|
||||||
|
|
||||||
const { data: auth } = useAppAuth(application.key);
|
const { data: auth } = useAppAuth(application.key);
|
||||||
|
|
||||||
const {
|
const { mutateAsync: updateOAuthClient, error: updateOAuthClientError } =
|
||||||
mutateAsync: updateOAuthClient,
|
useAdminUpdateOAuthClient(application.key, clientId);
|
||||||
isPending: isUpdateOAuthClientPending,
|
const [basicDataUpdatePending, setBasicDataUpdatePending] = useState(false);
|
||||||
error: updateOAuthClientError,
|
const [authDefaultsUpdatePending, setAuthDefaultsUpdatePending] =
|
||||||
} = useAdminUpdateOAuthClient(application.key, clientId);
|
useState(false);
|
||||||
|
|
||||||
const authFields = auth?.data?.fields?.map((field) => ({
|
const authFields = auth?.data?.fields;
|
||||||
...field,
|
|
||||||
required: false,
|
|
||||||
}));
|
|
||||||
|
|
||||||
const submitHandler = async (values) => {
|
const handleUpdateAuthDefaults = async (values) => {
|
||||||
if (!adminOAuthClient) {
|
if (!adminOAuthClient) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const { name, active, ...formattedAuthDefaults } = values;
|
const { name, active, ...formattedAuthDefaults } = values;
|
||||||
|
|
||||||
|
setAuthDefaultsUpdatePending(true);
|
||||||
|
await updateOAuthClient({
|
||||||
|
formattedAuthDefaults,
|
||||||
|
}).finally(() => {
|
||||||
|
setAuthDefaultsUpdatePending(false);
|
||||||
|
});
|
||||||
|
setDefaultValues((prev) => ({
|
||||||
|
name: prev.name,
|
||||||
|
active: prev.active,
|
||||||
|
...formattedAuthDefaults,
|
||||||
|
}));
|
||||||
|
|
||||||
|
enqueueSnackbar(formatMessage('updateOAuthClient.success'), {
|
||||||
|
variant: 'success',
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleUpdateBasicData = async (values) => {
|
||||||
|
if (!adminOAuthClient) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const { name, active } = values;
|
||||||
|
|
||||||
|
setBasicDataUpdatePending(true);
|
||||||
await updateOAuthClient({
|
await updateOAuthClient({
|
||||||
name,
|
name,
|
||||||
active,
|
active,
|
||||||
formattedAuthDefaults,
|
}).finally(() => {
|
||||||
|
setBasicDataUpdatePending(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
onClose();
|
enqueueSnackbar(formatMessage('updateOAuthClient.success'), {
|
||||||
|
variant: 'success',
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const getAuthFieldsDefaultValues = useCallback(() => {
|
const authFieldsDefaultValue = useMemo(() => {
|
||||||
if (!authFields) {
|
if (!authFields) {
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
@@ -62,24 +91,25 @@ function AdminApplicationUpdateOAuthClient(props) {
|
|||||||
return defaultValues;
|
return defaultValues;
|
||||||
}, [auth?.fields]);
|
}, [auth?.fields]);
|
||||||
|
|
||||||
const defaultValues = useMemo(
|
useEffect(() => {
|
||||||
() => ({
|
setDefaultValues({
|
||||||
name: adminOAuthClient?.data?.name || '',
|
name: adminOAuthClient?.data?.name || '',
|
||||||
active: adminOAuthClient?.data?.active || false,
|
active: adminOAuthClient?.data?.active || false,
|
||||||
...getAuthFieldsDefaultValues(),
|
...authFieldsDefaultValue,
|
||||||
}),
|
});
|
||||||
[adminOAuthClient, getAuthFieldsDefaultValues],
|
}, [adminOAuthClient, authFieldsDefaultValue]);
|
||||||
);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<AdminApplicationOAuthClientDialog
|
<AdminApplicationOAuthClientUpdateDialog
|
||||||
onClose={onClose}
|
onClose={onClose}
|
||||||
error={updateOAuthClientError}
|
error={updateOAuthClientError}
|
||||||
title={formatMessage('updateOAuthClient.title')}
|
title={formatMessage('updateOAuthClient.title')}
|
||||||
loading={isAdminOAuthClientLoading}
|
loading={isAdminOAuthClientLoading}
|
||||||
submitHandler={submitHandler}
|
submitAuthDefault={handleUpdateAuthDefaults}
|
||||||
|
submitBasicData={handleUpdateBasicData}
|
||||||
|
submittingBasicData={basicDataUpdatePending}
|
||||||
|
submittingAuthDefaults={authDefaultsUpdatePending}
|
||||||
authFields={authFields}
|
authFields={authFields}
|
||||||
submitting={isUpdateOAuthClientPending}
|
|
||||||
defaultValues={defaultValues}
|
defaultValues={defaultValues}
|
||||||
disabled={!adminOAuthClient}
|
disabled={!adminOAuthClient}
|
||||||
/>
|
/>
|
||||||
|
|||||||
@@ -318,6 +318,7 @@
|
|||||||
"oauthClient.inputName": "Name",
|
"oauthClient.inputName": "Name",
|
||||||
"oauthClient.inputActive": "Active",
|
"oauthClient.inputActive": "Active",
|
||||||
"updateOAuthClient.title": "Update OAuth client",
|
"updateOAuthClient.title": "Update OAuth client",
|
||||||
|
"updateOAuthClient.success": "OAuth client successfully updated",
|
||||||
"notFoundPage.title": "We can't seem to find a page you're looking for.",
|
"notFoundPage.title": "We can't seem to find a page you're looking for.",
|
||||||
"notFoundPage.button": "Back to home page",
|
"notFoundPage.button": "Back to home page",
|
||||||
"importFlowDialog.title": "Import flow",
|
"importFlowDialog.title": "Import flow",
|
||||||
|
|||||||
Reference in New Issue
Block a user