refactor: move abort controller into useLazyApps hook

This commit is contained in:
Rıdvan Akca
2024-03-11 16:16:12 +03:00
parent 2ee5af8bfb
commit 5835def5d0
7 changed files with 47 additions and 38 deletions

View File

@@ -4,11 +4,11 @@ import { renderObject } from '../../../../helpers/renderer.js';
export default async (request, response) => { export default async (request, response) => {
let apps = await App.findAll(request.query.name); let apps = await App.findAll(request.query.name);
if (request.query.onlyWithTriggers === 'true') { if (request.query.onlyWithTriggers) {
apps = apps.filter((app) => app.triggers?.length); apps = apps.filter((app) => app.triggers?.length);
} }
if (request.query.onlyWithActions === 'true') { if (request.query.onlyWithActions) {
apps = apps.filter((app) => app.actions?.length); apps = apps.filter((app) => app.actions?.length);
} }

View File

@@ -39,14 +39,12 @@ function AddNewAppConnection(props) {
const formatMessage = useFormatMessage(); const formatMessage = useFormatMessage();
const [appName, setAppName] = React.useState(''); const [appName, setAppName] = React.useState('');
const [isLoading, setIsLoading] = React.useState(false); const [isLoading, setIsLoading] = React.useState(false);
const abortControllerRef = React.useRef(null);
const { data: apps, mutate } = useLazyApps({ const { data: apps, mutate } = useLazyApps({
appName, appName,
onSuccess: () => { onSuccess: () => {
setIsLoading(false); setIsLoading(false);
}, },
abortControllerRef,
}); });
const fetchData = React.useMemo(() => debounce(mutate, 300), [mutate]); const fetchData = React.useMemo(() => debounce(mutate, 300), [mutate]);
@@ -54,18 +52,10 @@ function AddNewAppConnection(props) {
React.useEffect(() => { React.useEffect(() => {
setIsLoading(true); setIsLoading(true);
if (abortControllerRef.current) {
abortControllerRef.current.abort();
}
abortControllerRef.current = new AbortController();
fetchData(appName); fetchData(appName);
return () => { return () => {
fetchData.cancel(); fetchData.cancel();
if (abortControllerRef.current) {
abortControllerRef.current.abort();
}
}; };
}, [fetchData, appName]); }, [fetchData, appName]);

View File

@@ -45,11 +45,17 @@ function ChooseAppAndEventSubstep(props) {
const editorContext = React.useContext(EditorContext); const editorContext = React.useContext(EditorContext);
const isTrigger = step.type === 'trigger'; const isTrigger = step.type === 'trigger';
const isAction = step.type === 'action'; const isAction = step.type === 'action';
const useAppsOptions = {};
const { data: apps } = useApps({ if (isTrigger) {
onlyWithTriggers: isTrigger, useAppsOptions.onlyWithTriggers = true;
onlyWithActions: isAction, }
});
if (isAction) {
useAppsOptions.onlyWithActions = true;
}
const { data: apps } = useApps(useAppsOptions);
const app = apps?.data?.find( const app = apps?.data?.find(
(currentApp) => currentApp?.key === step?.appKey, (currentApp) => currentApp?.key === step?.appKey,
@@ -68,7 +74,7 @@ function ChooseAppAndEventSubstep(props) {
const actionOrTriggerOptions = React.useMemo( const actionOrTriggerOptions = React.useMemo(
() => actionsOrTriggers.map((trigger) => eventOptionGenerator(trigger)), () => actionsOrTriggers.map((trigger) => eventOptionGenerator(trigger)),
[app?.key, actionsOrTriggers], [actionsOrTriggers],
); );
const selectedActionOrTrigger = actionsOrTriggers.find( const selectedActionOrTrigger = actionsOrTriggers.find(

View File

@@ -79,11 +79,17 @@ function ExecutionStep(props) {
const isTrigger = step.type === 'trigger'; const isTrigger = step.type === 'trigger';
const isAction = step.type === 'action'; const isAction = step.type === 'action';
const formatMessage = useFormatMessage(); const formatMessage = useFormatMessage();
const useAppsOptions = {};
const { data: apps } = useApps({ if (isTrigger) {
onlyWithTriggers: isTrigger, useAppsOptions.onlyWithTriggers = true;
onlyWithActions: isAction, }
});
if (isAction) {
useAppsOptions.onlyWithActions = true;
}
const { data: apps } = useApps(useAppsOptions);
const app = apps?.data?.find((currentApp) => currentApp.key === step.appKey); const app = apps?.data?.find((currentApp) => currentApp.key === step.appKey);

View File

@@ -39,7 +39,7 @@ import { StepPropType } from 'propTypes/propTypes';
import useTriggers from 'hooks/useTriggers'; import useTriggers from 'hooks/useTriggers';
import useActions from 'hooks/useActions'; import useActions from 'hooks/useActions';
import useTriggerSubsteps from 'hooks/useTriggerSubsteps'; import useTriggerSubsteps from 'hooks/useTriggerSubsteps';
import useActionSubsteps from 'hooks/useActionsSubsteps'; import useActionSubsteps from 'hooks/useActionSubsteps';
const validIcon = <CheckCircleIcon color="success" />; const validIcon = <CheckCircleIcon color="success" />;
const errorIcon = <ErrorIcon color="error" />; const errorIcon = <ErrorIcon color="error" />;
@@ -114,11 +114,17 @@ function FlowStep(props) {
const isAction = step.type === 'action'; const isAction = step.type === 'action';
const formatMessage = useFormatMessage(); const formatMessage = useFormatMessage();
const [currentSubstep, setCurrentSubstep] = React.useState(0); const [currentSubstep, setCurrentSubstep] = React.useState(0);
const useAppsOptions = {};
const { data: apps } = useApps({ if (isTrigger) {
onlyWithTriggers: isTrigger, useAppsOptions.onlyWithTriggers = true;
onlyWithActions: isAction, }
});
if (isAction) {
useAppsOptions.onlyWithActions = true;
}
const { data: apps } = useApps(useAppsOptions);
const [ const [
getStepWithTestExecutions, getStepWithTestExecutions,

View File

@@ -1,25 +1,26 @@
import { useMutation } from '@tanstack/react-query'; import { useMutation } from '@tanstack/react-query';
import api from 'helpers/api'; import api from 'helpers/api';
import React from 'react';
export default function useLazyApps({ export default function useLazyApps({ appName, onSuccess }) {
appName, const abortControllerRef = React.useRef(new AbortController());
onSuccess,
abortControllerRef, React.useEffect(() => {
}) {
const query = useMutation({
mutationFn: async ({ payload, signal }) => {
abortControllerRef.current = new AbortController(); abortControllerRef.current = new AbortController();
return () => {
abortControllerRef.current?.abort();
};
}, [appName]);
const query = useMutation({
mutationFn: async ({ payload }) => {
const { data } = await api.get('/v1/apps', { const { data } = await api.get('/v1/apps', {
params: { name: appName }, params: { name: appName },
signal: abortControllerRef.current.signal, signal: abortControllerRef.current.signal,
}); });
if (abortControllerRef.current.signal.aborted) {
return;
}
return data; return data;
}, },
onSuccess, onSuccess,