Merge pull request #1793 from automatisch/AUT-682

refactor: rewrite useAuthClients with RQ
This commit is contained in:
Ali BARIN
2024-04-08 14:40:44 +02:00
committed by GitHub
9 changed files with 54 additions and 84 deletions

View File

@@ -1,33 +0,0 @@
import AppConfig from '../../models/app-config.js';
const getAppAuthClients = async (_parent, params, context) => {
let canSeeAllClients = false;
try {
context.currentUser.can('read', 'App');
canSeeAllClients = true;
} catch {
// void
}
const appConfig = await AppConfig.query()
.findOne({
key: params.appKey,
})
.throwIfNotFound();
const appAuthClients = appConfig
.$relatedQuery('appAuthClients')
.where({ active: params.active })
.skipUndefined();
if (!canSeeAllClients) {
appAuthClients.where({
active: true,
});
}
return await appAuthClients;
};
export default getAppAuthClients;

View File

@@ -1,6 +1,5 @@
import getApp from './queries/get-app.js'; import getApp from './queries/get-app.js';
import getAppAuthClient from './queries/get-app-auth-client.ee.js'; import getAppAuthClient from './queries/get-app-auth-client.ee.js';
import getAppAuthClients from './queries/get-app-auth-clients.ee.js';
import getBillingAndUsage from './queries/get-billing-and-usage.ee.js'; import getBillingAndUsage from './queries/get-billing-and-usage.ee.js';
import getConnectedApps from './queries/get-connected-apps.js'; import getConnectedApps from './queries/get-connected-apps.js';
import getDynamicData from './queries/get-dynamic-data.js'; import getDynamicData from './queries/get-dynamic-data.js';
@@ -10,7 +9,6 @@ import testConnection from './queries/test-connection.js';
const queryResolvers = { const queryResolvers = {
getApp, getApp,
getAppAuthClient, getAppAuthClient,
getAppAuthClients,
getBillingAndUsage, getBillingAndUsage,
getConnectedApps, getConnectedApps,
getDynamicData, getDynamicData,

View File

@@ -1,7 +1,6 @@
type Query { type Query {
getApp(key: String!): App getApp(key: String!): App
getAppAuthClient(id: String!): AppAuthClient getAppAuthClient(id: String!): AppAuthClient
getAppAuthClients(appKey: String!, active: Boolean): [AppAuthClient]
getConnectedApps(name: String): [App] getConnectedApps(name: String): [App]
testConnection(id: String!): Connection testConnection(id: String!): Connection
getStepWithTestExecutions(stepId: String!): [Step] getStepWithTestExecutions(stepId: String!): [Step]

View File

@@ -10,16 +10,18 @@ import Chip from '@mui/material/Chip';
import Button from '@mui/material/Button'; import Button from '@mui/material/Button';
import * as URLS from 'config/urls'; import * as URLS from 'config/urls';
import useFormatMessage from 'hooks/useFormatMessage'; import useFormatMessage from 'hooks/useFormatMessage';
import useAppAuthClients from 'hooks/useAppAuthClients.ee'; import useAdminAppAuthClients from 'hooks/useAdminAppAuthClients';
import NoResultFound from 'components/NoResultFound'; import NoResultFound from 'components/NoResultFound';
function AdminApplicationAuthClients(props) { function AdminApplicationAuthClients(props) {
const { appKey } = props; const { appKey } = props;
const formatMessage = useFormatMessage(); const formatMessage = useFormatMessage();
const { appAuthClients, loading } = useAppAuthClients({ appKey }); const { data: appAuthClients, isLoading } = useAdminAppAuthClients(appKey);
if (loading)
if (isLoading)
return <CircularProgress sx={{ display: 'block', margin: '20px auto' }} />; return <CircularProgress sx={{ display: 'block', margin: '20px auto' }} />;
if (!appAuthClients?.length) {
if (!appAuthClients?.data.length) {
return ( return (
<NoResultFound <NoResultFound
to={URLS.ADMIN_APP_AUTH_CLIENTS_CREATE(appKey)} to={URLS.ADMIN_APP_AUTH_CLIENTS_CREATE(appKey)}
@@ -27,7 +29,8 @@ function AdminApplicationAuthClients(props) {
/> />
); );
} }
const sortedAuthClients = appAuthClients.slice().sort((a, b) => {
const sortedAuthClients = appAuthClients.data.slice().sort((a, b) => {
if (a.id < b.id) { if (a.id < b.id) {
return -1; return -1;
} }
@@ -36,6 +39,7 @@ function AdminApplicationAuthClients(props) {
} }
return 0; return 0;
}); });
return ( return (
<div> <div>
{sortedAuthClients.map((client) => ( {sortedAuthClients.map((client) => (

View File

@@ -6,29 +6,33 @@ import ListItem from '@mui/material/ListItem';
import ListItemButton from '@mui/material/ListItemButton'; import ListItemButton from '@mui/material/ListItemButton';
import ListItemText from '@mui/material/ListItemText'; import ListItemText from '@mui/material/ListItemText';
import * as React from 'react'; import * as React from 'react';
import useAppAuthClients from 'hooks/useAppAuthClients.ee'; import useAppAuthClients from 'hooks/useAppAuthClients';
import useFormatMessage from 'hooks/useFormatMessage'; import useFormatMessage from 'hooks/useFormatMessage';
function AppAuthClientsDialog(props) { function AppAuthClientsDialog(props) {
const { appKey, onClientClick, onClose } = props; const { appKey, onClientClick, onClose } = props;
const { appAuthClients } = useAppAuthClients({ appKey, active: true }); const { data: appAuthClients } = useAppAuthClients(appKey);
const formatMessage = useFormatMessage(); const formatMessage = useFormatMessage();
React.useEffect( React.useEffect(
function autoAuthenticateSingleClient() { function autoAuthenticateSingleClient() {
if (appAuthClients?.length === 1) { if (appAuthClients?.data.length === 1) {
onClientClick(appAuthClients[0].id); onClientClick(appAuthClients.data[0].id);
} }
}, },
[appAuthClients], [appAuthClients?.data],
); );
if (!appAuthClients?.length || appAuthClients?.length === 1)
if (!appAuthClients?.data.length || appAuthClients?.data.length === 1)
return <React.Fragment />; return <React.Fragment />;
return ( return (
<Dialog onClose={onClose} open={true}> <Dialog onClose={onClose} open={true}>
<DialogTitle>{formatMessage('appAuthClientsDialog.title')}</DialogTitle> <DialogTitle>{formatMessage('appAuthClientsDialog.title')}</DialogTitle>
<List sx={{ pt: 0 }}> <List sx={{ pt: 0 }}>
{appAuthClients.map((appAuthClient) => ( {appAuthClients.data.map((appAuthClient) => (
<ListItem disableGutters key={appAuthClient.id}> <ListItem disableGutters key={appAuthClient.id}>
<ListItemButton onClick={() => onClientClick(appAuthClient.id)}> <ListItemButton onClick={() => onClientClick(appAuthClient.id)}>
<ListItemText primary={appAuthClient.name} /> <ListItemText primary={appAuthClient.name} />

View File

@@ -1,11 +0,0 @@
import { gql } from '@apollo/client';
export const GET_APP_AUTH_CLIENTS = gql`
query GetAppAuthClients($appKey: String!, $active: Boolean) {
getAppAuthClients(appKey: $appKey, active: $active) {
id
appConfigId
name
active
}
}
`;

View File

@@ -0,0 +1,17 @@
import { useQuery } from '@tanstack/react-query';
import api from 'helpers/api';
export default function useAdminAppAuthClient(appKey) {
const query = useQuery({
queryKey: ['admin', 'apps', appKey, 'auth-clients'],
queryFn: async ({ signal }) => {
const { data } = await api.get(`/v1/admin/apps/${appKey}/auth-clients`, {
signal,
});
return data;
},
enabled: !!appKey,
});
return query;
}

View File

@@ -1,25 +0,0 @@
import { useLazyQuery } from '@apollo/client';
import * as React from 'react';
import { GET_APP_AUTH_CLIENTS } from 'graphql/queries/get-app-auth-clients.ee';
export default function useAppAuthClient({ appKey, active }) {
const [getAppAuthClients, { data, loading }] = useLazyQuery(
GET_APP_AUTH_CLIENTS,
{
context: { autoSnackbar: false },
},
);
const appAuthClients = data?.getAppAuthClients;
React.useEffect(
function fetchUponAppKey() {
if (!appKey) return;
getAppAuthClients({
variables: { appKey, ...(typeof active === 'boolean' && { active }) },
});
},
[appKey],
);
return {
appAuthClients,
loading,
};
}

View File

@@ -0,0 +1,17 @@
import { useQuery } from '@tanstack/react-query';
import api from 'helpers/api';
export default function useAppAuthClients(appKey) {
const query = useQuery({
queryKey: ['apps', appKey, 'auth-clients'],
queryFn: async ({ signal }) => {
const { data } = await api.get(`/v1/apps/${appKey}/auth-clients`, {
signal,
});
return data;
},
enabled: !!appKey,
});
return query;
}