From c879ffa76833c46fc0f64d66082bad77d23ecb8c Mon Sep 17 00:00:00 2001 From: Ali BARIN Date: Tue, 24 Sep 2024 08:29:58 +0000 Subject: [PATCH] feat(useAuthenticateApp): use REST API endpoint to create connection --- .../web/src/hooks/useAuthenticateApp.ee.js | 11 ++++++-- packages/web/src/hooks/useCreateConnection.js | 26 +++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 packages/web/src/hooks/useCreateConnection.js diff --git a/packages/web/src/hooks/useAuthenticateApp.ee.js b/packages/web/src/hooks/useAuthenticateApp.ee.js index a4cca1cd..c140c3e7 100644 --- a/packages/web/src/hooks/useAuthenticateApp.ee.js +++ b/packages/web/src/hooks/useAuthenticateApp.ee.js @@ -7,6 +7,7 @@ import { } from 'helpers/authenticationSteps'; import computeAuthStepVariables from 'helpers/computeAuthStepVariables'; import useAppAuth from './useAppAuth'; +import useCreateConnection from './useCreateConnection'; import useFormatMessage from './useFormatMessage'; function getSteps(auth, hasConnection, useShared) { @@ -27,6 +28,7 @@ function getSteps(auth, hasConnection, useShared) { export default function useAuthenticateApp(payload) { const { appKey, appAuthClientId, connectionId, useShared = false } = payload; const { data: auth } = useAppAuth(appKey); + const { mutateAsync: createConnection } = useCreateConnection(appKey); const [authenticationInProgress, setAuthenticationInProgress] = React.useState(false); const formatMessage = useFormatMessage(); @@ -65,8 +67,13 @@ export default function useAuthenticateApp(payload) { } if (step.type === 'mutation') { - const stepResponse = await processMutation(step.name, variables); - response[step.name] = stepResponse; + if (step.name === 'createConnection') { + const stepResponse = await createConnection(variables); + response[step.name] = stepResponse?.data; + } else { + const stepResponse = await processMutation(step.name, variables); + response[step.name] = stepResponse; + } } else if (step.type === 'openWithPopup') { const stepResponse = await processPopupMessage(popup); response[step.name] = stepResponse; diff --git a/packages/web/src/hooks/useCreateConnection.js b/packages/web/src/hooks/useCreateConnection.js new file mode 100644 index 00000000..afcbe458 --- /dev/null +++ b/packages/web/src/hooks/useCreateConnection.js @@ -0,0 +1,26 @@ +import { useMutation, useQueryClient } from '@tanstack/react-query'; + +import api from 'helpers/api'; + +export default function useCreateConnection(appKey) { + const queryClient = useQueryClient(); + + const query = useMutation({ + mutationFn: async ({ appAuthClientId, formattedData }) => { + const { data } = await api.post(`/v1/apps/${appKey}/connections`, { + appAuthClientId, + formattedData, + }); + + return data; + }, + + onSuccess: () => { + queryClient.invalidateQueries({ + queryKey: ['apps', appKey, 'connections'], + }); + }, + }); + + return query; +}