diff --git a/packages/web/src/graphql/mutations/upsert-saml-auth-provider.js b/packages/web/src/graphql/mutations/upsert-saml-auth-provider.js deleted file mode 100644 index 575c846d..00000000 --- a/packages/web/src/graphql/mutations/upsert-saml-auth-provider.js +++ /dev/null @@ -1,8 +0,0 @@ -import { gql } from '@apollo/client'; -export const UPSERT_SAML_AUTH_PROVIDER = gql` - mutation UpsertSamlAuthProvider($input: UpsertSamlAuthProviderInput) { - upsertSamlAuthProvider(input: $input) { - id - } - } -`; diff --git a/packages/web/src/hooks/useAdminCreateSamlAuthProvider.js b/packages/web/src/hooks/useAdminCreateSamlAuthProvider.js new file mode 100644 index 00000000..33fa7e79 --- /dev/null +++ b/packages/web/src/hooks/useAdminCreateSamlAuthProvider.js @@ -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; +} diff --git a/packages/web/src/hooks/useAdminUpdateSamlAuthProvider.js b/packages/web/src/hooks/useAdminUpdateSamlAuthProvider.js new file mode 100644 index 00000000..9e6600f4 --- /dev/null +++ b/packages/web/src/hooks/useAdminUpdateSamlAuthProvider.js @@ -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; +} diff --git a/packages/web/src/pages/Authentication/SamlConfiguration.jsx b/packages/web/src/pages/Authentication/SamlConfiguration.jsx index 6a99b6bb..719ee1e8 100644 --- a/packages/web/src/pages/Authentication/SamlConfiguration.jsx +++ b/packages/web/src/pages/Authentication/SamlConfiguration.jsx @@ -1,5 +1,4 @@ import PropTypes from 'prop-types'; -import { useMutation } from '@apollo/client'; import LoadingButton from '@mui/lab/LoadingButton'; import Stack from '@mui/material/Stack'; import MuiTextField from '@mui/material/TextField'; @@ -10,8 +9,9 @@ import ControlledAutocomplete from 'components/ControlledAutocomplete'; import Form from 'components/Form'; import Switch from 'components/Switch'; import TextField from 'components/TextField'; -import { UPSERT_SAML_AUTH_PROVIDER } from 'graphql/mutations/upsert-saml-auth-provider'; import useFormatMessage from 'hooks/useFormatMessage'; +import useAdminCreateSamlAuthProvider from 'hooks/useAdminCreateSamlAuthProvider'; +import useAdminUpdateSamlAuthProvider from 'hooks/useAdminUpdateSamlAuthProvider'; import useRoles from 'hooks/useRoles.ee'; const defaultValues = { @@ -38,42 +38,26 @@ function SamlConfiguration({ provider, providerLoading }) { const roles = data?.data; const enqueueSnackbar = useEnqueueSnackbar(); - const [upsertSamlAuthProvider, { loading }] = useMutation( - UPSERT_SAML_AUTH_PROVIDER, - ); + const { + mutateAsync: createSamlAuthProvider, + isPending: isCreateSamlAuthProviderPending, + } = useAdminCreateSamlAuthProvider(); - const handleProviderUpdate = async (providerDataToUpdate) => { + const { + mutateAsync: updateSamlAuthProvider, + isPending: isUpdateSamlAuthProviderPending, + } = useAdminUpdateSamlAuthProvider(provider?.id); + + const isPending = + isCreateSamlAuthProviderPending || isUpdateSamlAuthProviderPending; + + const handleSubmit = async (providerData) => { try { - const { - name, - certificate, - signatureAlgorithm, - issuer, - entryPoint, - firstnameAttributeName, - surnameAttributeName, - emailAttributeName, - roleAttributeName, - active, - defaultRoleId, - } = providerDataToUpdate; - await upsertSamlAuthProvider({ - variables: { - input: { - name, - certificate, - signatureAlgorithm, - issuer, - entryPoint, - firstnameAttributeName, - surnameAttributeName, - emailAttributeName, - roleAttributeName, - active, - defaultRoleId, - }, - }, - }); + if (provider?.id) { + await updateSamlAuthProvider(providerData); + } else { + await createSamlAuthProvider(providerData); + } enqueueSnackbar(formatMessage('authenticationForm.successfullySaved'), { variant: 'success', @@ -91,10 +75,7 @@ function SamlConfiguration({ provider, providerLoading }) { } return ( -
+ {formatMessage('authenticationForm.save')} @@ -196,6 +177,7 @@ function SamlConfiguration({ provider, providerLoading }) { SamlConfiguration.propTypes = { provider: PropTypes.shape({ + id: PropTypes.string, active: PropTypes.bool, name: PropTypes.string, certificate: PropTypes.string,