feat(useAuthenticateApp): use REST API endpoint to reset connection
This commit is contained in:
@@ -27,12 +27,7 @@ const authenticationStepsWithoutAuthUrl = [
|
|||||||
{
|
{
|
||||||
type: 'mutation',
|
type: 'mutation',
|
||||||
name: 'verifyConnection',
|
name: 'verifyConnection',
|
||||||
arguments: [
|
arguments: [],
|
||||||
{
|
|
||||||
name: 'id',
|
|
||||||
value: '{createConnection.id}',
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
@@ -79,12 +74,7 @@ const authenticationStepsWithAuthUrl = [
|
|||||||
{
|
{
|
||||||
type: 'mutation',
|
type: 'mutation',
|
||||||
name: 'verifyConnection',
|
name: 'verifyConnection',
|
||||||
arguments: [
|
arguments: [],
|
||||||
{
|
|
||||||
name: 'id',
|
|
||||||
value: '{createConnection.id}',
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
@@ -131,12 +121,7 @@ const sharedAuthenticationStepsWithAuthUrl = [
|
|||||||
{
|
{
|
||||||
type: 'mutation',
|
type: 'mutation',
|
||||||
name: 'verifyConnection',
|
name: 'verifyConnection',
|
||||||
arguments: [
|
arguments: [],
|
||||||
{
|
|
||||||
name: 'id',
|
|
||||||
value: '{createConnection.id}',
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
|
@@ -1,54 +1,23 @@
|
|||||||
import cloneDeep from 'lodash/cloneDeep.js';
|
import cloneDeep from 'lodash/cloneDeep.js';
|
||||||
|
|
||||||
const connectionIdArgument = {
|
|
||||||
name: 'id',
|
|
||||||
value: '{connection.id}',
|
|
||||||
};
|
|
||||||
|
|
||||||
const resetConnectionStep = {
|
const resetConnectionStep = {
|
||||||
type: 'mutation',
|
type: 'mutation',
|
||||||
name: 'resetConnection',
|
name: 'resetConnection',
|
||||||
arguments: [connectionIdArgument],
|
arguments: [],
|
||||||
};
|
};
|
||||||
|
|
||||||
function replaceCreateConnection(string) {
|
|
||||||
return string.replace('{createConnection.id}', '{connection.id}');
|
|
||||||
}
|
|
||||||
|
|
||||||
function removeAppKeyArgument(args) {
|
function removeAppKeyArgument(args) {
|
||||||
return args.filter((argument) => argument.name !== 'key');
|
return args.filter((argument) => argument.name !== 'key');
|
||||||
}
|
}
|
||||||
|
|
||||||
function addConnectionId(step) {
|
|
||||||
step.arguments = step.arguments.map((argument) => {
|
|
||||||
if (typeof argument.value === 'string') {
|
|
||||||
argument.value = replaceCreateConnection(argument.value);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (argument.properties) {
|
|
||||||
argument.properties = argument.properties.map((property) => {
|
|
||||||
return {
|
|
||||||
name: property.name,
|
|
||||||
value: replaceCreateConnection(property.value),
|
|
||||||
};
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return argument;
|
|
||||||
});
|
|
||||||
|
|
||||||
return step;
|
|
||||||
}
|
|
||||||
|
|
||||||
function replaceCreateConnectionsWithUpdate(steps) {
|
function replaceCreateConnectionsWithUpdate(steps) {
|
||||||
const updatedSteps = cloneDeep(steps);
|
const updatedSteps = cloneDeep(steps);
|
||||||
return updatedSteps.map((step) => {
|
return updatedSteps.map((step) => {
|
||||||
const updatedStep = addConnectionId(step);
|
const updatedStep = { ...step };
|
||||||
|
|
||||||
if (step.name === 'createConnection') {
|
if (step.name === 'createConnection') {
|
||||||
updatedStep.name = 'updateConnection';
|
updatedStep.name = 'updateConnection';
|
||||||
updatedStep.arguments = removeAppKeyArgument(updatedStep.arguments);
|
updatedStep.arguments = removeAppKeyArgument(updatedStep.arguments);
|
||||||
updatedStep.arguments.unshift(connectionIdArgument);
|
|
||||||
|
|
||||||
return updatedStep;
|
return updatedStep;
|
||||||
}
|
}
|
||||||
|
@@ -11,6 +11,7 @@ import useAppAuth from './useAppAuth';
|
|||||||
import useCreateConnection from './useCreateConnection';
|
import useCreateConnection from './useCreateConnection';
|
||||||
import useCreateConnectionAuthUrl from './useCreateConnectionAuthUrl';
|
import useCreateConnectionAuthUrl from './useCreateConnectionAuthUrl';
|
||||||
import useUpdateConnection from './useUpdateConnection';
|
import useUpdateConnection from './useUpdateConnection';
|
||||||
|
import useResetConnection from './useResetConnection';
|
||||||
|
|
||||||
function getSteps(auth, hasConnection, useShared) {
|
function getSteps(auth, hasConnection, useShared) {
|
||||||
if (hasConnection) {
|
if (hasConnection) {
|
||||||
@@ -33,6 +34,7 @@ export default function useAuthenticateApp(payload) {
|
|||||||
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();
|
||||||
|
const { mutateAsync: resetConnection } = useResetConnection();
|
||||||
const [authenticationInProgress, setAuthenticationInProgress] =
|
const [authenticationInProgress, setAuthenticationInProgress] =
|
||||||
React.useState(false);
|
React.useState(false);
|
||||||
const formatMessage = useFormatMessage();
|
const formatMessage = useFormatMessage();
|
||||||
@@ -48,9 +50,7 @@ export default function useAuthenticateApp(payload) {
|
|||||||
const response = {
|
const response = {
|
||||||
key: appKey,
|
key: appKey,
|
||||||
appAuthClientId: appAuthClientId || payload.appAuthClientId,
|
appAuthClientId: appAuthClientId || payload.appAuthClientId,
|
||||||
connection: {
|
connectionId,
|
||||||
id: connectionId,
|
|
||||||
},
|
|
||||||
fields,
|
fields,
|
||||||
};
|
};
|
||||||
let stepIndex = 0;
|
let stepIndex = 0;
|
||||||
@@ -73,19 +73,24 @@ export default function useAuthenticateApp(payload) {
|
|||||||
if (step.type === 'mutation') {
|
if (step.type === 'mutation') {
|
||||||
if (step.name === 'createConnection') {
|
if (step.name === 'createConnection') {
|
||||||
const stepResponse = await createConnection(variables);
|
const stepResponse = await createConnection(variables);
|
||||||
response[step.name] = stepResponse?.data;
|
response[step.name] = stepResponse.data;
|
||||||
|
response.connectionId = stepResponse.data.id;
|
||||||
} else if (step.name === 'generateAuthUrl') {
|
} else if (step.name === 'generateAuthUrl') {
|
||||||
const stepResponse = await createConnectionAuthUrl(
|
const stepResponse = await createConnectionAuthUrl(
|
||||||
response.createConnection.id,
|
response.connectionId,
|
||||||
);
|
);
|
||||||
response[step.name] = stepResponse?.data;
|
response[step.name] = stepResponse.data;
|
||||||
} else if (step.name === 'updateConnection') {
|
} else if (step.name === 'updateConnection') {
|
||||||
const stepResponse = await updateConnection({
|
const stepResponse = await updateConnection({
|
||||||
...variables,
|
...variables,
|
||||||
connectionId: response.createConnection.id,
|
connectionId: response.connectionId,
|
||||||
});
|
});
|
||||||
|
|
||||||
response[step.name] = stepResponse?.data;
|
response[step.name] = stepResponse.data;
|
||||||
|
} else if (step.name === 'resetConnection') {
|
||||||
|
const stepResponse = await resetConnection(response.connectionId);
|
||||||
|
|
||||||
|
response[step.name] = stepResponse.data;
|
||||||
} else {
|
} else {
|
||||||
const stepResponse = await processMutation(step.name, variables);
|
const stepResponse = await processMutation(step.name, variables);
|
||||||
response[step.name] = stepResponse;
|
response[step.name] = stepResponse;
|
||||||
@@ -116,6 +121,7 @@ export default function useAuthenticateApp(payload) {
|
|||||||
createConnection,
|
createConnection,
|
||||||
createConnectionAuthUrl,
|
createConnectionAuthUrl,
|
||||||
updateConnection,
|
updateConnection,
|
||||||
|
resetConnection,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
15
packages/web/src/hooks/useResetConnection.js
Normal file
15
packages/web/src/hooks/useResetConnection.js
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
import { useMutation } from '@tanstack/react-query';
|
||||||
|
|
||||||
|
import api from 'helpers/api';
|
||||||
|
|
||||||
|
export default function useResetConnection() {
|
||||||
|
const query = useMutation({
|
||||||
|
mutationFn: async (connectionId) => {
|
||||||
|
const { data } = await api.post(`/v1/connections/${connectionId}/reset`);
|
||||||
|
|
||||||
|
return data;
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return query;
|
||||||
|
}
|
Reference in New Issue
Block a user