refactor(SamlConfiguration): rewrite mutations with REST API endpoints
This commit is contained in:
@@ -1,8 +0,0 @@
|
|||||||
import { gql } from '@apollo/client';
|
|
||||||
export const UPSERT_SAML_AUTH_PROVIDER = gql`
|
|
||||||
mutation UpsertSamlAuthProvider($input: UpsertSamlAuthProviderInput) {
|
|
||||||
upsertSamlAuthProvider(input: $input) {
|
|
||||||
id
|
|
||||||
}
|
|
||||||
}
|
|
||||||
`;
|
|
21
packages/web/src/hooks/useAdminCreateSamlAuthProvider.js
Normal file
21
packages/web/src/hooks/useAdminCreateSamlAuthProvider.js
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
import { useMutation, useQueryClient } from '@tanstack/react-query';
|
||||||
|
import api from 'helpers/api';
|
||||||
|
|
||||||
|
export default function useAdminCreateSamlAuthProvider() {
|
||||||
|
const queryClient = useQueryClient();
|
||||||
|
|
||||||
|
const query = useMutation({
|
||||||
|
mutationFn: async (payload) => {
|
||||||
|
const { data } = await api.post(`/v1/admin/saml-auth-providers`, payload);
|
||||||
|
|
||||||
|
return data;
|
||||||
|
},
|
||||||
|
onSuccess: () => {
|
||||||
|
queryClient.invalidateQueries({
|
||||||
|
queryKey: ['admin', 'samlAuthProviders'],
|
||||||
|
});
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return query;
|
||||||
|
}
|
24
packages/web/src/hooks/useAdminUpdateSamlAuthProvider.js
Normal file
24
packages/web/src/hooks/useAdminUpdateSamlAuthProvider.js
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
import { useMutation, useQueryClient } from '@tanstack/react-query';
|
||||||
|
import api from 'helpers/api';
|
||||||
|
|
||||||
|
export default function useAdminUpdateSamlAuthProvider(samlAuthProviderId) {
|
||||||
|
const queryClient = useQueryClient();
|
||||||
|
|
||||||
|
const query = useMutation({
|
||||||
|
mutationFn: async (payload) => {
|
||||||
|
const { data } = await api.patch(
|
||||||
|
`/v1/admin/saml-auth-providers/${samlAuthProviderId}`,
|
||||||
|
payload,
|
||||||
|
);
|
||||||
|
|
||||||
|
return data;
|
||||||
|
},
|
||||||
|
onSuccess: () => {
|
||||||
|
queryClient.invalidateQueries({
|
||||||
|
queryKey: ['admin', 'samlAuthProviders'],
|
||||||
|
});
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return query;
|
||||||
|
}
|
@@ -1,5 +1,4 @@
|
|||||||
import PropTypes from 'prop-types';
|
import PropTypes from 'prop-types';
|
||||||
import { useMutation } from '@apollo/client';
|
|
||||||
import LoadingButton from '@mui/lab/LoadingButton';
|
import LoadingButton from '@mui/lab/LoadingButton';
|
||||||
import Stack from '@mui/material/Stack';
|
import Stack from '@mui/material/Stack';
|
||||||
import MuiTextField from '@mui/material/TextField';
|
import MuiTextField from '@mui/material/TextField';
|
||||||
@@ -10,8 +9,9 @@ import ControlledAutocomplete from 'components/ControlledAutocomplete';
|
|||||||
import Form from 'components/Form';
|
import Form from 'components/Form';
|
||||||
import Switch from 'components/Switch';
|
import Switch from 'components/Switch';
|
||||||
import TextField from 'components/TextField';
|
import TextField from 'components/TextField';
|
||||||
import { UPSERT_SAML_AUTH_PROVIDER } from 'graphql/mutations/upsert-saml-auth-provider';
|
|
||||||
import useFormatMessage from 'hooks/useFormatMessage';
|
import useFormatMessage from 'hooks/useFormatMessage';
|
||||||
|
import useAdminCreateSamlAuthProvider from 'hooks/useAdminCreateSamlAuthProvider';
|
||||||
|
import useAdminUpdateSamlAuthProvider from 'hooks/useAdminUpdateSamlAuthProvider';
|
||||||
import useRoles from 'hooks/useRoles.ee';
|
import useRoles from 'hooks/useRoles.ee';
|
||||||
|
|
||||||
const defaultValues = {
|
const defaultValues = {
|
||||||
@@ -38,42 +38,26 @@ function SamlConfiguration({ provider, providerLoading }) {
|
|||||||
const roles = data?.data;
|
const roles = data?.data;
|
||||||
const enqueueSnackbar = useEnqueueSnackbar();
|
const enqueueSnackbar = useEnqueueSnackbar();
|
||||||
|
|
||||||
const [upsertSamlAuthProvider, { loading }] = useMutation(
|
|
||||||
UPSERT_SAML_AUTH_PROVIDER,
|
|
||||||
);
|
|
||||||
|
|
||||||
const handleProviderUpdate = async (providerDataToUpdate) => {
|
|
||||||
try {
|
|
||||||
const {
|
const {
|
||||||
name,
|
mutateAsync: createSamlAuthProvider,
|
||||||
certificate,
|
isPending: isCreateSamlAuthProviderPending,
|
||||||
signatureAlgorithm,
|
} = useAdminCreateSamlAuthProvider();
|
||||||
issuer,
|
|
||||||
entryPoint,
|
const {
|
||||||
firstnameAttributeName,
|
mutateAsync: updateSamlAuthProvider,
|
||||||
surnameAttributeName,
|
isPending: isUpdateSamlAuthProviderPending,
|
||||||
emailAttributeName,
|
} = useAdminUpdateSamlAuthProvider(provider?.id);
|
||||||
roleAttributeName,
|
|
||||||
active,
|
const isPending =
|
||||||
defaultRoleId,
|
isCreateSamlAuthProviderPending || isUpdateSamlAuthProviderPending;
|
||||||
} = providerDataToUpdate;
|
|
||||||
await upsertSamlAuthProvider({
|
const handleSubmit = async (providerData) => {
|
||||||
variables: {
|
try {
|
||||||
input: {
|
if (provider?.id) {
|
||||||
name,
|
await updateSamlAuthProvider(providerData);
|
||||||
certificate,
|
} else {
|
||||||
signatureAlgorithm,
|
await createSamlAuthProvider(providerData);
|
||||||
issuer,
|
}
|
||||||
entryPoint,
|
|
||||||
firstnameAttributeName,
|
|
||||||
surnameAttributeName,
|
|
||||||
emailAttributeName,
|
|
||||||
roleAttributeName,
|
|
||||||
active,
|
|
||||||
defaultRoleId,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
enqueueSnackbar(formatMessage('authenticationForm.successfullySaved'), {
|
enqueueSnackbar(formatMessage('authenticationForm.successfullySaved'), {
|
||||||
variant: 'success',
|
variant: 'success',
|
||||||
@@ -91,10 +75,7 @@ function SamlConfiguration({ provider, providerLoading }) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Form
|
<Form defaultValues={provider || defaultValues} onSubmit={handleSubmit}>
|
||||||
defaultValues={provider || defaultValues}
|
|
||||||
onSubmit={handleProviderUpdate}
|
|
||||||
>
|
|
||||||
<Stack direction="column" gap={2}>
|
<Stack direction="column" gap={2}>
|
||||||
<Switch
|
<Switch
|
||||||
name="active"
|
name="active"
|
||||||
@@ -185,7 +166,7 @@ function SamlConfiguration({ provider, providerLoading }) {
|
|||||||
variant="contained"
|
variant="contained"
|
||||||
color="primary"
|
color="primary"
|
||||||
sx={{ boxShadow: 2 }}
|
sx={{ boxShadow: 2 }}
|
||||||
loading={loading}
|
loading={isPending}
|
||||||
>
|
>
|
||||||
{formatMessage('authenticationForm.save')}
|
{formatMessage('authenticationForm.save')}
|
||||||
</LoadingButton>
|
</LoadingButton>
|
||||||
@@ -196,6 +177,7 @@ function SamlConfiguration({ provider, providerLoading }) {
|
|||||||
|
|
||||||
SamlConfiguration.propTypes = {
|
SamlConfiguration.propTypes = {
|
||||||
provider: PropTypes.shape({
|
provider: PropTypes.shape({
|
||||||
|
id: PropTypes.string,
|
||||||
active: PropTypes.bool,
|
active: PropTypes.bool,
|
||||||
name: PropTypes.string,
|
name: PropTypes.string,
|
||||||
certificate: PropTypes.string,
|
certificate: PropTypes.string,
|
||||||
|
Reference in New Issue
Block a user