refactor: rewrite useApp with RQ

This commit is contained in:
Rıdvan Akca
2024-03-05 17:35:22 +03:00
parent 5fe3546d2a
commit be62c09d06
2 changed files with 30 additions and 12 deletions

View File

@@ -1,12 +1,19 @@
import { useQuery } from '@apollo/client';
import { GET_APP } from 'graphql/queries/get-app';
export default function useApp(key) {
const { data, loading } = useQuery(GET_APP, {
variables: { key },
import { useQuery } from '@tanstack/react-query';
import api from 'helpers/api';
export default function useApp(appKey) {
const query = useQuery({
queryKey: ['app', appKey],
queryFn: async ({ payload, signal }) => {
const { data } = await api.get(`/v1/apps/${appKey}`, {
signal,
});
const app = data?.getApp;
return {
app,
loading,
};
return data;
},
enabled: !!appKey,
});
return query;
}

View File

@@ -1,7 +1,9 @@
import * as React from 'react';
import { processStep } from 'helpers/authenticationSteps';
import computeAuthStepVariables from 'helpers/computeAuthStepVariables';
import useApp from './useApp';
function getSteps(auth, hasConnection, useShared) {
if (hasConnection) {
if (useShared) {
@@ -9,19 +11,24 @@ function getSteps(auth, hasConnection, useShared) {
}
return auth?.reconnectionSteps;
}
if (useShared) {
return auth?.sharedAuthenticationSteps;
}
return auth?.authenticationSteps;
}
export default function useAuthenticateApp(payload) {
const { appKey, appAuthClientId, connectionId, useShared = false } = payload;
const { app } = useApp(appKey);
const { data: app } = useApp(appKey);
const [authenticationInProgress, setAuthenticationInProgress] =
React.useState(false);
const steps = getSteps(app?.auth, !!connectionId, useShared);
const steps = getSteps(app?.data?.auth, !!connectionId, useShared);
const authenticate = React.useMemo(() => {
if (!steps?.length) return;
return async function authenticate(payload = {}) {
const { fields } = payload;
setAuthenticationInProgress(true);
@@ -34,9 +41,11 @@ export default function useAuthenticateApp(payload) {
fields,
};
let stepIndex = 0;
while (stepIndex < steps?.length) {
const step = steps[stepIndex];
const variables = computeAuthStepVariables(step.arguments, response);
try {
const stepResponse = await processStep(step, variables);
response[step.name] = stepResponse;
@@ -46,6 +55,7 @@ export default function useAuthenticateApp(payload) {
throw err;
}
stepIndex++;
if (stepIndex === steps.length) {
return response;
}
@@ -53,6 +63,7 @@ export default function useAuthenticateApp(payload) {
}
};
}, [steps, appKey, appAuthClientId, connectionId]);
return {
authenticate,
inProgress: authenticationInProgress,