refactor: rewrite useApp with RQ
This commit is contained in:
@@ -1,12 +1,19 @@
|
|||||||
import { useQuery } from '@apollo/client';
|
import { useQuery } from '@tanstack/react-query';
|
||||||
import { GET_APP } from 'graphql/queries/get-app';
|
|
||||||
export default function useApp(key) {
|
import api from 'helpers/api';
|
||||||
const { data, loading } = useQuery(GET_APP, {
|
|
||||||
variables: { key },
|
export default function useApp(appKey) {
|
||||||
|
const query = useQuery({
|
||||||
|
queryKey: ['app', appKey],
|
||||||
|
queryFn: async ({ payload, signal }) => {
|
||||||
|
const { data } = await api.get(`/v1/apps/${appKey}`, {
|
||||||
|
signal,
|
||||||
|
});
|
||||||
|
|
||||||
|
return data;
|
||||||
|
},
|
||||||
|
enabled: !!appKey,
|
||||||
});
|
});
|
||||||
const app = data?.getApp;
|
|
||||||
return {
|
return query;
|
||||||
app,
|
|
||||||
loading,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
@@ -1,7 +1,9 @@
|
|||||||
import * as React from 'react';
|
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 useApp from './useApp';
|
||||||
|
|
||||||
function getSteps(auth, hasConnection, useShared) {
|
function getSteps(auth, hasConnection, useShared) {
|
||||||
if (hasConnection) {
|
if (hasConnection) {
|
||||||
if (useShared) {
|
if (useShared) {
|
||||||
@@ -9,19 +11,24 @@ function getSteps(auth, hasConnection, useShared) {
|
|||||||
}
|
}
|
||||||
return auth?.reconnectionSteps;
|
return auth?.reconnectionSteps;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (useShared) {
|
if (useShared) {
|
||||||
return auth?.sharedAuthenticationSteps;
|
return auth?.sharedAuthenticationSteps;
|
||||||
}
|
}
|
||||||
|
|
||||||
return auth?.authenticationSteps;
|
return auth?.authenticationSteps;
|
||||||
}
|
}
|
||||||
|
|
||||||
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 { app } = useApp(appKey);
|
const { data: app } = useApp(appKey);
|
||||||
const [authenticationInProgress, setAuthenticationInProgress] =
|
const [authenticationInProgress, setAuthenticationInProgress] =
|
||||||
React.useState(false);
|
React.useState(false);
|
||||||
const steps = getSteps(app?.auth, !!connectionId, useShared);
|
const steps = getSteps(app?.data?.auth, !!connectionId, useShared);
|
||||||
|
|
||||||
const authenticate = React.useMemo(() => {
|
const authenticate = React.useMemo(() => {
|
||||||
if (!steps?.length) return;
|
if (!steps?.length) return;
|
||||||
|
|
||||||
return async function authenticate(payload = {}) {
|
return async function authenticate(payload = {}) {
|
||||||
const { fields } = payload;
|
const { fields } = payload;
|
||||||
setAuthenticationInProgress(true);
|
setAuthenticationInProgress(true);
|
||||||
@@ -34,9 +41,11 @@ export default function useAuthenticateApp(payload) {
|
|||||||
fields,
|
fields,
|
||||||
};
|
};
|
||||||
let stepIndex = 0;
|
let stepIndex = 0;
|
||||||
|
|
||||||
while (stepIndex < steps?.length) {
|
while (stepIndex < steps?.length) {
|
||||||
const step = steps[stepIndex];
|
const step = steps[stepIndex];
|
||||||
const variables = computeAuthStepVariables(step.arguments, response);
|
const variables = computeAuthStepVariables(step.arguments, response);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const stepResponse = await processStep(step, variables);
|
const stepResponse = await processStep(step, variables);
|
||||||
response[step.name] = stepResponse;
|
response[step.name] = stepResponse;
|
||||||
@@ -46,6 +55,7 @@ export default function useAuthenticateApp(payload) {
|
|||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
stepIndex++;
|
stepIndex++;
|
||||||
|
|
||||||
if (stepIndex === steps.length) {
|
if (stepIndex === steps.length) {
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
@@ -53,6 +63,7 @@ export default function useAuthenticateApp(payload) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
}, [steps, appKey, appAuthClientId, connectionId]);
|
}, [steps, appKey, appAuthClientId, connectionId]);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
authenticate,
|
authenticate,
|
||||||
inProgress: authenticationInProgress,
|
inProgress: authenticationInProgress,
|
||||||
|
Reference in New Issue
Block a user