refactor: rewrite useSamlAuthProviders with RQ

This commit is contained in:
Rıdvan Akca
2024-03-14 15:13:17 +03:00
parent 3aa86eebf2
commit ab49535b6c
12 changed files with 52 additions and 48 deletions

View File

@@ -1,9 +0,0 @@
import SamlAuthProvider from '../../models/saml-auth-provider.ee.js';
const listSamlAuthProviders = async () => {
const providers = await SamlAuthProvider.query().where({ active: true });
return providers;
};
export default listSamlAuthProviders;

View File

@@ -14,7 +14,6 @@ import getSamlAuthProviderRoleMappings from './queries/get-saml-auth-provider-ro
import getStepWithTestExecutions from './queries/get-step-with-test-executions.js';
import getTrialStatus from './queries/get-trial-status.ee.js';
import getUsers from './queries/get-users.js';
import listSamlAuthProviders from './queries/list-saml-auth-providers.ee.js';
import testConnection from './queries/test-connection.js';
const queryResolvers = {
@@ -34,7 +33,6 @@ const queryResolvers = {
getStepWithTestExecutions,
getTrialStatus,
getUsers,
listSamlAuthProviders,
testConnection,
};

View File

@@ -30,7 +30,6 @@ type Query {
getSamlAuthProviderRoleMappings(id: String!): [SamlAuthProvidersRoleMapping]
getTrialStatus: GetTrialStatus
getUsers(limit: Int!, offset: Int!): UserConnection
listSamlAuthProviders: [ListSamlAuthProvider]
}
type Mutation {
@@ -642,13 +641,6 @@ type Usage {
task: Int
}
type ListSamlAuthProvider {
id: String
name: String
issuer: String
loginUrl: String
}
type Permission {
id: String
action: String

View File

@@ -44,7 +44,6 @@ export const authenticationRules = {
'*': isAuthenticatedRule,
getConfig: allow,
getNotifications: allow,
listSamlAuthProviders: allow,
},
Mutation: {
'*': isAuthenticatedRule,

View File

@@ -3,19 +3,25 @@ import Paper from '@mui/material/Paper';
import Button from '@mui/material/Button';
import Stack from '@mui/material/Stack';
import Divider from '@mui/material/Divider';
import useSamlAuthProviders from 'hooks/useSamlAuthProviders.ee';
import useFormatMessage from 'hooks/useFormatMessage';
function SsoProviders() {
const formatMessage = useFormatMessage();
const { providers, loading } = useSamlAuthProviders();
if (!loading && providers.length === 0) return null;
const { data, isLoading: isSamlAuthProvidersLoading } =
useSamlAuthProviders();
const providers = data?.data;
if (!isSamlAuthProvidersLoading && providers?.length === 0) return null;
return (
<>
<Divider>{formatMessage('loginPage.divider')}</Divider>
<Paper sx={{ px: 2, py: 4 }}>
<Stack direction="column" gap={1}>
{providers.map((provider) => (
{providers?.map((provider) => (
<Button
key={provider.id}
component="a"

View File

@@ -1,11 +0,0 @@
import { gql } from '@apollo/client';
export const LIST_SAML_AUTH_PROVIDERS = gql`
query ListSamlAuthProviders {
listSamlAuthProviders {
id
name
loginUrl
issuer
}
}
`;

View File

@@ -0,0 +1,18 @@
import { useQuery } from '@tanstack/react-query';
import api from 'helpers/api';
export default function useAdminSamlAuthProviders() {
const query = useQuery({
queryKey: ['adminSamlAuthProviders'],
queryFn: async ({ signal }) => {
const { data } = await api.get('/v1/admin/saml-auth-providers', {
signal,
});
return data;
},
});
return query;
}

View File

@@ -3,7 +3,7 @@ import { useMutation } from '@tanstack/react-query';
import api from 'helpers/api';
import React from 'react';
export default function useLazyApps({ appName } = {}, { onSuccess }) {
export default function useLazyApps({ appName } = {}, { onSuccess } = {}) {
const abortControllerRef = React.useRef(new AbortController());
React.useEffect(() => {

View File

@@ -1,9 +1,18 @@
import { useQuery } from '@apollo/client';
import { LIST_SAML_AUTH_PROVIDERS } from 'graphql/queries/list-saml-auth-providers.ee';
import { useQuery } from '@tanstack/react-query';
import api from 'helpers/api';
export default function useSamlAuthProviders() {
const { data, loading } = useQuery(LIST_SAML_AUTH_PROVIDERS);
return {
providers: data?.listSamlAuthProviders || [],
loading,
};
const query = useQuery({
queryKey: ['samlAuthProviders'],
queryFn: async ({ signal }) => {
const { data } = await api.get('/v1/saml-auth-providers', {
signal,
});
return data;
},
});
return query;
}

View File

@@ -6,12 +6,14 @@ import useFormatMessage from 'hooks/useFormatMessage';
import useSamlAuthProvider from 'hooks/useSamlAuthProvider';
import SamlConfiguration from './SamlConfiguration';
import RoleMappings from './RoleMappings';
import useSamlAuthProviders from 'hooks/useSamlAuthProviders.ee';
import useAdminSamlAuthProviders from 'hooks/useAdminSamlAuthProviders.ee';
function AuthenticationPage() {
const formatMessage = useFormatMessage();
const { providers } = useSamlAuthProviders();
const samlAuthProviderId = providers[0]?.id;
const { data, loading: isProviderLoading } = useSamlAuthProvider({
const { data: adminSamlAuthProviders } = useAdminSamlAuthProviders();
const samlAuthProviderId = adminSamlAuthProviders?.data?.[0].id;
const { data, isLoading: isProviderLoading } = useSamlAuthProvider({
samlAuthProviderId,
});
const provider = data?.data;

View File

@@ -30,7 +30,7 @@ export default function EditUser() {
const { userId } = useParams();
const { data: userData, loading: isUserLoading } = useUser({ userId });
const user = userData?.data;
const { data, loading: isRolesLoading } = useRoles();
const { data, isLoading: isRolesLoading } = useRoles();
const roles = data?.data;
const enqueueSnackbar = useEnqueueSnackbar();
const navigate = useNavigate();

View File

@@ -63,7 +63,7 @@ function ProfileSettings() {
optimisticResponse: {
updateCurrentUser: {
__typename: 'User',
id: currentUser?.id,
id: currentUser.id,
fullName,
email,
},