feat(useAuthenticateApp): early exit connection creation at blocked pop-up

This commit is contained in:
Ali BARIN
2024-09-05 12:08:32 +00:00
parent 0c53ee8460
commit 297543f9dd
2 changed files with 15 additions and 19 deletions

View File

@@ -31,7 +31,7 @@ function AddAppConnection(props) {
const hasConnection = Boolean(connectionId); const hasConnection = Boolean(connectionId);
const useShared = searchParams.get('shared') === 'true'; const useShared = searchParams.get('shared') === 'true';
const appAuthClientId = searchParams.get('appAuthClientId') || undefined; const appAuthClientId = searchParams.get('appAuthClientId') || undefined;
const { authenticate, isPopupBlocked } = useAuthenticateApp({ const { authenticate } = useAuthenticateApp({
appKey: key, appKey: key,
connectionId, connectionId,
appAuthClientId, appAuthClientId,
@@ -134,12 +134,6 @@ function AddAppConnection(props) {
</Alert> </Alert>
)} )}
{isPopupBlocked && (
<Alert severity="warning" sx={{ fontWeight: 300, mt: 1 }}>
{formatMessage('addAppConnection.popupReminder')}
</Alert>
)}
{error && ( {error && (
<Alert <Alert
severity="error" severity="error"

View File

@@ -7,6 +7,7 @@ import {
} from 'helpers/authenticationSteps'; } from 'helpers/authenticationSteps';
import computeAuthStepVariables from 'helpers/computeAuthStepVariables'; import computeAuthStepVariables from 'helpers/computeAuthStepVariables';
import useAppAuth from './useAppAuth'; import useAppAuth from './useAppAuth';
import useFormatMessage from './useFormatMessage';
function getSteps(auth, hasConnection, useShared) { function getSteps(auth, hasConnection, useShared) {
if (hasConnection) { if (hasConnection) {
@@ -28,7 +29,7 @@ export default function useAuthenticateApp(payload) {
const { data: auth } = useAppAuth(appKey); const { data: auth } = useAppAuth(appKey);
const [authenticationInProgress, setAuthenticationInProgress] = const [authenticationInProgress, setAuthenticationInProgress] =
React.useState(false); React.useState(false);
const [isPopupBlocked, setPopupBlocked] = React.useState(false); const formatMessage = useFormatMessage();
const steps = getSteps(auth?.data, !!connectionId, useShared); const steps = getSteps(auth?.data, !!connectionId, useShared);
const authenticate = React.useMemo(() => { const authenticate = React.useMemo(() => {
@@ -37,6 +38,7 @@ export default function useAuthenticateApp(payload) {
return async function authenticate(payload = {}) { return async function authenticate(payload = {}) {
const { fields } = payload; const { fields } = payload;
setAuthenticationInProgress(true); setAuthenticationInProgress(true);
const response = { const response = {
key: appKey, key: appKey,
appAuthClientId: appAuthClientId || payload.appAuthClientId, appAuthClientId: appAuthClientId || payload.appAuthClientId,
@@ -50,17 +52,18 @@ export default function useAuthenticateApp(payload) {
while (stepIndex < steps?.length) { while (stepIndex < steps?.length) {
const step = steps[stepIndex]; const step = steps[stepIndex];
const variables = computeAuthStepVariables(step.arguments, response); const variables = computeAuthStepVariables(step.arguments, response);
let popup;
if (step.type === 'openWithPopup') {
popup = processOpenWithPopup(variables.url);
if (!popup) {
setPopupBlocked(true);
}
}
try { try {
let popup;
if (step.type === 'openWithPopup') {
popup = processOpenWithPopup(variables.url);
if (!popup) {
throw new Error(formatMessage('addAppConnection.popupReminder'));
}
}
if (step.type === 'mutation') { if (step.type === 'mutation') {
const stepResponse = await processMutation(step.name, variables); const stepResponse = await processMutation(step.name, variables);
response[step.name] = stepResponse; response[step.name] = stepResponse;
@@ -81,11 +84,10 @@ export default function useAuthenticateApp(payload) {
setAuthenticationInProgress(false); setAuthenticationInProgress(false);
} }
}; };
}, [steps, appKey, appAuthClientId, connectionId]); }, [steps, appKey, appAuthClientId, connectionId, formatMessage]);
return { return {
authenticate, authenticate,
isPopupBlocked,
inProgress: authenticationInProgress, inProgress: authenticationInProgress,
}; };
} }