feat: add useAppAuth with RQ

This commit is contained in:
Rıdvan Akca
2024-03-07 15:54:00 +03:00
parent a4c0edf493
commit 2ee5af8bfb
8 changed files with 68 additions and 53 deletions

View File

@@ -16,10 +16,12 @@ import useAuthenticateApp from 'hooks/useAuthenticateApp.ee';
import useFormatMessage from 'hooks/useFormatMessage'; import useFormatMessage from 'hooks/useFormatMessage';
import { generateExternalLink } from 'helpers/translationValues'; import { generateExternalLink } from 'helpers/translationValues';
import { Form } from './style'; import { Form } from './style';
import useAppAuth from 'hooks/useAppAuth';
function AddAppConnection(props) { function AddAppConnection(props) {
const { application, connectionId, onClose } = props; const { application, connectionId, onClose } = props;
const { name, authDocUrl, key, auth } = application; const { name, authDocUrl, key } = application;
const { data: auth } = useAppAuth(key);
const navigate = useNavigate(); const navigate = useNavigate();
const [searchParams] = useSearchParams(); const [searchParams] = useSearchParams();
const formatMessage = useFormatMessage(); const formatMessage = useFormatMessage();
@@ -34,31 +36,40 @@ function AddAppConnection(props) {
appAuthClientId, appAuthClientId,
useShared: !!appAuthClientId, useShared: !!appAuthClientId,
}); });
React.useEffect(function relayProviderData() { React.useEffect(function relayProviderData() {
if (window.opener) { if (window.opener) {
window.opener.postMessage({ window.opener.postMessage({
source: 'automatisch', source: 'automatisch',
payload: { search: window.location.search, hash: window.location.hash }, payload: { search: window.location.search, hash: window.location.hash },
}); });
window.close(); window.close();
} }
}, []); }, []);
React.useEffect( React.useEffect(
function initiateSharedAuthenticationForGivenAuthClient() { function initiateSharedAuthenticationForGivenAuthClient() {
if (!appAuthClientId) return; if (!appAuthClientId) return;
if (!authenticate) return; if (!authenticate) return;
const asyncAuthenticate = async () => { const asyncAuthenticate = async () => {
await authenticate(); await authenticate();
navigate(URLS.APP_CONNECTIONS(key)); navigate(URLS.APP_CONNECTIONS(key));
}; };
asyncAuthenticate(); asyncAuthenticate();
}, },
[appAuthClientId, authenticate], [appAuthClientId, authenticate],
); );
const handleClientClick = (appAuthClientId) => const handleClientClick = (appAuthClientId) =>
navigate(URLS.APP_ADD_CONNECTION_WITH_AUTH_CLIENT_ID(key, appAuthClientId)); navigate(URLS.APP_ADD_CONNECTION_WITH_AUTH_CLIENT_ID(key, appAuthClientId));
const handleAuthClientsDialogClose = () => const handleAuthClientsDialogClose = () =>
navigate(URLS.APP_CONNECTIONS(key)); navigate(URLS.APP_CONNECTIONS(key));
const submitHandler = React.useCallback( const submitHandler = React.useCallback(
async (data) => { async (data) => {
if (!authenticate) return; if (!authenticate) return;
@@ -78,6 +89,7 @@ function AddAppConnection(props) {
}, },
[authenticate], [authenticate],
); );
if (useShared) if (useShared)
return ( return (
<AppAuthClientsDialog <AppAuthClientsDialog
@@ -86,7 +98,9 @@ function AddAppConnection(props) {
onClientClick={handleClientClick} onClientClick={handleClientClick}
/> />
); );
if (appAuthClientId) return <React.Fragment />; if (appAuthClientId) return <React.Fragment />;
return ( return (
<Dialog open={true} onClose={onClose} data-test="add-app-connection-dialog"> <Dialog open={true} onClose={onClose} data-test="add-app-connection-dialog">
<DialogTitle> <DialogTitle>
@@ -121,7 +135,7 @@ function AddAppConnection(props) {
<DialogContent> <DialogContent>
<DialogContentText tabIndex={-1} component="div"> <DialogContentText tabIndex={-1} component="div">
<Form onSubmit={submitHandler}> <Form onSubmit={submitHandler}>
{auth?.fields?.map((field) => ( {auth?.data?.fields?.map((field) => (
<InputCreator key={field.key} schema={field} /> <InputCreator key={field.key} schema={field} />
))} ))}

View File

@@ -8,12 +8,14 @@ import { CREATE_APP_AUTH_CLIENT } from 'graphql/mutations/create-app-auth-client
import useAppConfig from 'hooks/useAppConfig.ee'; import useAppConfig from 'hooks/useAppConfig.ee';
import useFormatMessage from 'hooks/useFormatMessage'; import useFormatMessage from 'hooks/useFormatMessage';
import AdminApplicationAuthClientDialog from 'components/AdminApplicationAuthClientDialog'; import AdminApplicationAuthClientDialog from 'components/AdminApplicationAuthClientDialog';
import useAppAuth from 'hooks/useAppAuth';
function AdminApplicationCreateAuthClient(props) { function AdminApplicationCreateAuthClient(props) {
const { appKey, application, onClose } = props; const { appKey, onClose } = props;
const { auth } = application; const { data: auth } = useAppAuth(appKey);
const formatMessage = useFormatMessage(); const formatMessage = useFormatMessage();
const { appConfig, loading: loadingAppConfig } = useAppConfig(appKey); const { appConfig, loading: loadingAppConfig } = useAppConfig(appKey);
const [ const [
createAppConfig, createAppConfig,
{ loading: loadingCreateAppConfig, error: createAppConfigError }, { loading: loadingCreateAppConfig, error: createAppConfigError },
@@ -21,6 +23,7 @@ function AdminApplicationCreateAuthClient(props) {
refetchQueries: ['GetAppConfig'], refetchQueries: ['GetAppConfig'],
context: { autoSnackbar: false }, context: { autoSnackbar: false },
}); });
const [ const [
createAppAuthClient, createAppAuthClient,
{ loading: loadingCreateAppAuthClient, error: createAppAuthClientError }, { loading: loadingCreateAppAuthClient, error: createAppAuthClientError },
@@ -28,8 +31,10 @@ function AdminApplicationCreateAuthClient(props) {
refetchQueries: ['GetAppAuthClients'], refetchQueries: ['GetAppAuthClients'],
context: { autoSnackbar: false }, context: { autoSnackbar: false },
}); });
const submitHandler = async (values) => { const submitHandler = async (values) => {
let appConfigId = appConfig?.id; let appConfigId = appConfig?.id;
if (!appConfigId) { if (!appConfigId) {
const { data: appConfigData } = await createAppConfig({ const { data: appConfigData } = await createAppConfig({
variables: { variables: {
@@ -41,8 +46,10 @@ function AdminApplicationCreateAuthClient(props) {
}, },
}, },
}); });
appConfigId = appConfigData.createAppConfig.id; appConfigId = appConfigData.createAppConfig.id;
} }
const { name, active, ...formattedAuthDefaults } = values; const { name, active, ...formattedAuthDefaults } = values;
await createAppAuthClient({ await createAppAuthClient({
variables: { variables: {
@@ -54,22 +61,28 @@ function AdminApplicationCreateAuthClient(props) {
}, },
}, },
}); });
onClose(); onClose();
}; };
const getAuthFieldsDefaultValues = useCallback(() => { const getAuthFieldsDefaultValues = useCallback(() => {
if (!auth?.fields) { if (!auth?.data?.fields) {
return {}; return {};
} }
const defaultValues = {}; const defaultValues = {};
auth.fields.forEach((field) => {
auth.data.fields.forEach((field) => {
if (field.value || field.type !== 'string') { if (field.value || field.type !== 'string') {
defaultValues[field.key] = field.value; defaultValues[field.key] = field.value;
} else if (field.type === 'string') { } else if (field.type === 'string') {
defaultValues[field.key] = ''; defaultValues[field.key] = '';
} }
}); });
return defaultValues; return defaultValues;
}, [auth?.fields]); }, [auth?.data?.fields]);
const defaultValues = useMemo( const defaultValues = useMemo(
() => ({ () => ({
name: '', name: '',
@@ -78,6 +91,7 @@ function AdminApplicationCreateAuthClient(props) {
}), }),
[getAuthFieldsDefaultValues], [getAuthFieldsDefaultValues],
); );
return ( return (
<AdminApplicationAuthClientDialog <AdminApplicationAuthClientDialog
onClose={onClose} onClose={onClose}
@@ -85,7 +99,7 @@ function AdminApplicationCreateAuthClient(props) {
title={formatMessage('createAuthClient.title')} title={formatMessage('createAuthClient.title')}
loading={loadingAppConfig} loading={loadingAppConfig}
submitHandler={submitHandler} submitHandler={submitHandler}
authFields={auth?.fields} authFields={auth?.data?.fields}
submitting={loadingCreateAppConfig || loadingCreateAppAuthClient} submitting={loadingCreateAppConfig || loadingCreateAppAuthClient}
defaultValues={defaultValues} defaultValues={defaultValues}
/> />

View File

@@ -176,6 +176,7 @@ function ChooseConnectionSubstep(props) {
} }
}, [step.connection?.id, retestConnection]); }, [step.connection?.id, retestConnection]);
const onToggle = expanded ? onCollapse : onExpand; const onToggle = expanded ? onCollapse : onExpand;
return ( return (
<React.Fragment> <React.Fragment>
<FlowSubstepTitle <FlowSubstepTitle

View File

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

View File

@@ -2,7 +2,7 @@ import * as React from 'react';
import { processStep } from 'helpers/authenticationSteps'; import { processStep } from 'helpers/authenticationSteps';
import computeAuthStepVariables from 'helpers/computeAuthStepVariables'; import computeAuthStepVariables from 'helpers/computeAuthStepVariables';
import useApp from './useApp'; import useAppAuth from './useAppAuth';
function getSteps(auth, hasConnection, useShared) { function getSteps(auth, hasConnection, useShared) {
if (hasConnection) { if (hasConnection) {
@@ -21,10 +21,10 @@ function getSteps(auth, hasConnection, useShared) {
export default function useAuthenticateApp(payload) { export default function useAuthenticateApp(payload) {
const { appKey, appAuthClientId, connectionId, useShared = false } = payload; const { appKey, appAuthClientId, connectionId, useShared = false } = payload;
const { data: app } = useApp(appKey); const { data: auth } = useAppAuth(appKey);
const [authenticationInProgress, setAuthenticationInProgress] = const [authenticationInProgress, setAuthenticationInProgress] =
React.useState(false); React.useState(false);
const steps = getSteps(app?.data?.auth, !!connectionId, useShared); const steps = getSteps(auth?.data, !!connectionId, useShared);
const authenticate = React.useMemo(() => { const authenticate = React.useMemo(() => {
if (!steps?.length) return; if (!steps?.length) return;

View File

@@ -24,8 +24,7 @@ import AdminApplicationSettings from 'components/AdminApplicationSettings';
import AdminApplicationAuthClients from 'components/AdminApplicationAuthClients'; import AdminApplicationAuthClients from 'components/AdminApplicationAuthClients';
import AdminApplicationCreateAuthClient from 'components/AdminApplicationCreateAuthClient'; import AdminApplicationCreateAuthClient from 'components/AdminApplicationCreateAuthClient';
import AdminApplicationUpdateAuthClient from 'components/AdminApplicationUpdateAuthClient'; import AdminApplicationUpdateAuthClient from 'components/AdminApplicationUpdateAuthClient';
import { useQuery } from '@tanstack/react-query'; import useApp from 'hooks/useApp';
import api from 'helpers/api';
export default function AdminApplication() { export default function AdminApplication() {
const theme = useTheme(); const theme = useTheme();
@@ -46,17 +45,7 @@ export default function AdminApplication() {
}); });
const { appKey } = useParams(); const { appKey } = useParams();
const { data, loading } = useQuery({ const { data, loading } = useApp(appKey);
queryKey: ['app', appKey],
queryFn: async ({ payload, signal }) => {
const { data } = await api.get(`/v1/apps/${appKey}`, {
signal,
});
return data;
},
enabled: !!appKey,
});
const app = data?.data || {}; const app = data?.data || {};

View File

@@ -2,7 +2,6 @@ import * as React from 'react';
import Grid from '@mui/material/Grid'; import Grid from '@mui/material/Grid';
import CircularProgress from '@mui/material/CircularProgress'; import CircularProgress from '@mui/material/CircularProgress';
import Divider from '@mui/material/Divider'; import Divider from '@mui/material/Divider';
import { useQuery } from '@tanstack/react-query';
import PageTitle from 'components/PageTitle'; import PageTitle from 'components/PageTitle';
import Container from 'components/Container'; import Container from 'components/Container';
@@ -10,23 +9,13 @@ import SearchInput from 'components/SearchInput';
import AppRow from 'components/AppRow'; import AppRow from 'components/AppRow';
import * as URLS from 'config/urls'; import * as URLS from 'config/urls';
import useFormatMessage from 'hooks/useFormatMessage'; import useFormatMessage from 'hooks/useFormatMessage';
import api from 'helpers/api'; import useApps from 'hooks/useApps';
function AdminApplications() { function AdminApplications() {
const formatMessage = useFormatMessage(); const formatMessage = useFormatMessage();
const [appName, setAppName] = React.useState(null); const [appName, setAppName] = React.useState(null);
const { data: apps, isLoading: appsLoading } = useQuery({ const { data: apps, isLoading: isAppsLoading } = useApps(appName);
queryKey: ['apps', appName],
queryFn: async ({ payload, signal }) => {
const { data } = await api.get('/v1/apps', {
params: { name: appName },
signal,
});
return data;
},
});
const onSearchChange = React.useCallback((event) => { const onSearchChange = React.useCallback((event) => {
setAppName(event.target.value); setAppName(event.target.value);
@@ -48,14 +37,14 @@ function AdminApplications() {
<Divider sx={{ mt: [2, 0], mb: 2 }} /> <Divider sx={{ mt: [2, 0], mb: 2 }} />
</Grid> </Grid>
{appsLoading && ( {isAppsLoading && (
<CircularProgress <CircularProgress
data-test="apps-loader" data-test="apps-loader"
sx={{ display: 'block', margin: '20px auto' }} sx={{ display: 'block', margin: '20px auto' }}
/> />
)} )}
{!appsLoading && {!isAppsLoading &&
apps?.data?.map((app) => ( apps?.data?.map((app) => (
<Grid item xs={12} key={app.name}> <Grid item xs={12} key={app.name}>
<AppRow application={app} url={URLS.ADMIN_APP(app.key)} /> <AppRow application={app} url={URLS.ADMIN_APP(app.key)} />

View File

@@ -28,12 +28,12 @@ import AddAppConnection from 'components/AddAppConnection';
import AppIcon from 'components/AppIcon'; import AppIcon from 'components/AppIcon';
import Container from 'components/Container'; import Container from 'components/Container';
import PageTitle from 'components/PageTitle'; import PageTitle from 'components/PageTitle';
import api from 'helpers/api'; import useApp from 'hooks/useApp';
import { useQuery } from '@tanstack/react-query';
const ReconnectConnection = (props) => { const ReconnectConnection = (props) => {
const { application, onClose } = props; const { application, onClose } = props;
const { connectionId } = useParams(); const { connectionId } = useParams();
return ( return (
<AddAppConnection <AddAppConnection
onClose={onClose} onClose={onClose}
@@ -56,18 +56,7 @@ export default function Application() {
const { appKey } = useParams(); const { appKey } = useParams();
const navigate = useNavigate(); const navigate = useNavigate();
const { data, loading } = useQuery({ const { data, loading } = useApp(appKey);
queryKey: ['app', appKey],
queryFn: async ({ payload, signal }) => {
const { data } = await api.get(`/v1/apps/${appKey}`, {
signal,
});
return data;
},
enabled: !!appKey,
});
const app = data?.data || {}; const app = data?.data || {};
const { appConfig } = useAppConfig(appKey); const { appConfig } = useAppConfig(appKey);