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

@@ -1,27 +1,35 @@
import template from 'lodash/template'; import template from 'lodash/template';
const interpolate = /{([\s\S]+?)}/g; const interpolate = /{([\s\S]+?)}/g;
const computeAuthStepVariables = (variableSchema, aggregatedData) => { const computeAuthStepVariables = (variableSchema, aggregatedData) => {
const variables = {}; const variables = {};
for (const variable of variableSchema) { for (const variable of variableSchema) {
if (variable.properties) { if (variable.properties) {
variables[variable.name] = computeAuthStepVariables( variables[variable.name] = computeAuthStepVariables(
variable.properties, variable.properties,
aggregatedData, aggregatedData,
); );
continue; continue;
} }
if (variable.value) { if (variable.value) {
if (variable.value.endsWith('.all}')) { if (variable.value.endsWith('.all}')) {
const key = variable.value.replace('{', '').replace('.all}', ''); const key = variable.value.replace('{', '').replace('.all}', '');
variables[variable.name] = aggregatedData[key]; variables[variable.name] = aggregatedData[key];
continue; continue;
} }
const computedVariable = template(variable.value, { interpolate })( const computedVariable = template(variable.value, { interpolate })(
aggregatedData, aggregatedData,
); );
variables[variable.name] = computedVariable; variables[variable.name] = computedVariable;
} }
} }
return variables; return variables;
}; };
export default computeAuthStepVariables; export default computeAuthStepVariables;

View File

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