feat(useAuthenticateApp): use REST API endpoint to update connection

This commit is contained in:
Ali BARIN
2024-09-24 11:10:40 +00:00
parent 5e20ac07d1
commit 24d09fda4c
3 changed files with 46 additions and 2 deletions

View File

@@ -6,10 +6,11 @@ import {
processPopupMessage,
} from 'helpers/authenticationSteps';
import computeAuthStepVariables from 'helpers/computeAuthStepVariables';
import useFormatMessage from './useFormatMessage';
import useAppAuth from './useAppAuth';
import useCreateConnection from './useCreateConnection';
import useFormatMessage from './useFormatMessage';
import useCreateConnectionAuthUrl from './useCreateConnectionAuthUrl';
import useUpdateConnection from './useUpdateConnection';
function getSteps(auth, hasConnection, useShared) {
if (hasConnection) {
@@ -31,6 +32,7 @@ export default function useAuthenticateApp(payload) {
const { data: auth } = useAppAuth(appKey);
const { mutateAsync: createConnection } = useCreateConnection(appKey);
const { mutateAsync: createConnectionAuthUrl } = useCreateConnectionAuthUrl();
const { mutateAsync: updateConnection } = useUpdateConnection();
const [authenticationInProgress, setAuthenticationInProgress] =
React.useState(false);
const formatMessage = useFormatMessage();
@@ -76,6 +78,13 @@ export default function useAuthenticateApp(payload) {
const stepResponse = await createConnectionAuthUrl(
response.createConnection.id,
);
response[step.name] = stepResponse?.data;
} else if (step.name === 'updateConnection') {
const stepResponse = await updateConnection({
...variables,
connectionId: response.createConnection.id,
});
response[step.name] = stepResponse?.data;
} else {
const stepResponse = await processMutation(step.name, variables);
@@ -98,7 +107,16 @@ export default function useAuthenticateApp(payload) {
setAuthenticationInProgress(false);
}
};
}, [steps, appKey, appAuthClientId, connectionId, formatMessage]);
}, [
steps,
appKey,
appAuthClientId,
connectionId,
formatMessage,
createConnection,
createConnectionAuthUrl,
updateConnection,
]);
return {
authenticate,

View File

@@ -0,0 +1,18 @@
import { useMutation } from '@tanstack/react-query';
import api from 'helpers/api';
export default function useUpdateConnection() {
const query = useMutation({
mutationFn: async ({ connectionId, formattedData, appAuthClientId }) => {
const { data } = await api.patch(`/v1/connections/${connectionId}`, {
formattedData,
appAuthClientId,
});
return data;
},
});
return query;
}