feat(useAuthenticateApp): centralize invalidating queries

This commit is contained in:
Ali BARIN
2024-09-24 12:11:02 +00:00
parent 4d5fc50f1a
commit 244eeeb816
3 changed files with 23 additions and 24 deletions

View File

@@ -1,4 +1,5 @@
import * as React from 'react'; import * as React from 'react';
import { useQueryClient } from '@tanstack/react-query';
import { import {
processMutation, processMutation,
@@ -32,6 +33,7 @@ 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: auth } = useAppAuth(appKey); const { data: auth } = useAppAuth(appKey);
const queryClient = useQueryClient();
const { mutateAsync: createConnection } = useCreateConnection(appKey); const { mutateAsync: createConnection } = useCreateConnection(appKey);
const { mutateAsync: createConnectionAuthUrl } = useCreateConnectionAuthUrl(); const { mutateAsync: createConnectionAuthUrl } = useCreateConnectionAuthUrl();
const { mutateAsync: updateConnection } = useUpdateConnection(); const { mutateAsync: updateConnection } = useUpdateConnection();
@@ -94,7 +96,9 @@ export default function useAuthenticateApp(payload) {
response[step.name] = stepResponse.data; response[step.name] = stepResponse.data;
} else if (step.name === 'verifyConnection') { } else if (step.name === 'verifyConnection') {
const stepResponse = await verifyConnection(variables?.id); const stepResponse = await verifyConnection(
response.connectionId,
);
response[step.name] = stepResponse?.data; response[step.name] = stepResponse?.data;
} else { } else {
const stepResponse = await processMutation(step.name, variables); const stepResponse = await processMutation(step.name, variables);
@@ -107,26 +111,37 @@ export default function useAuthenticateApp(payload) {
} catch (err) { } catch (err) {
console.log(err); console.log(err);
setAuthenticationInProgress(false); setAuthenticationInProgress(false);
queryClient.invalidateQueries({
queryKey: ['apps', appKey, 'connections'],
});
throw err; throw err;
} }
stepIndex++;
if (stepIndex === steps.length) { stepIndex++;
return response;
}
setAuthenticationInProgress(false);
} }
await queryClient.invalidateQueries({
queryKey: ['apps', appKey, 'connections'],
});
setAuthenticationInProgress(false);
return response;
}; };
}, [ }, [
steps, steps,
appKey, appKey,
appAuthClientId, appAuthClientId,
connectionId, connectionId,
queryClient,
formatMessage, formatMessage,
createConnection, createConnection,
createConnectionAuthUrl, createConnectionAuthUrl,
updateConnection, updateConnection,
resetConnection, resetConnection,
verifyConnection,
]); ]);
return { return {

View File

@@ -1,10 +1,8 @@
import { useMutation, useQueryClient } from '@tanstack/react-query'; import { useMutation } from '@tanstack/react-query';
import api from 'helpers/api'; import api from 'helpers/api';
export default function useCreateConnection(appKey) { export default function useCreateConnection(appKey) {
const queryClient = useQueryClient();
const query = useMutation({ const query = useMutation({
mutationFn: async ({ appAuthClientId, formattedData }) => { mutationFn: async ({ appAuthClientId, formattedData }) => {
const { data } = await api.post(`/v1/apps/${appKey}/connections`, { const { data } = await api.post(`/v1/apps/${appKey}/connections`, {
@@ -14,12 +12,6 @@ export default function useCreateConnection(appKey) {
return data; return data;
}, },
onSuccess: () => {
queryClient.invalidateQueries({
queryKey: ['apps', appKey, 'connections'],
});
},
}); });
return query; return query;

View File

@@ -1,9 +1,7 @@
import { useMutation, useQueryClient } from '@tanstack/react-query'; import { useMutation } from '@tanstack/react-query';
import api from 'helpers/api'; import api from 'helpers/api';
export default function useVerifyConnection() { export default function useVerifyConnection() {
const queryClient = useQueryClient();
const query = useMutation({ const query = useMutation({
mutationFn: async (connectionId) => { mutationFn: async (connectionId) => {
try { try {
@@ -16,12 +14,6 @@ export default function useVerifyConnection() {
throw new Error('Failed while verifying connection!'); throw new Error('Failed while verifying connection!');
} }
}, },
onSuccess: (data) => {
const appKey = data?.data.key;
queryClient.invalidateQueries({
queryKey: ['apps', appKey, 'connections'],
});
},
}); });
return query; return query;