Skip to content

Commit

Permalink
PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
cnasikas committed Feb 25, 2021
1 parent cbdd68a commit f8c4468
Show file tree
Hide file tree
Showing 16 changed files with 267 additions and 258 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,25 @@ export const useActionTypes = (): UseActionTypesResponse => {
const [, dispatchToaster] = useStateToaster();
const [loading, setLoading] = useState(true);
const [actionTypes, setActionTypes] = useState<ActionTypeConnector[]>([]);
const didCancel = useRef(false);
const abortCtrl = useRef(new AbortController());
const isCancelledRef = useRef(false);
const abortCtrlRef = useRef(new AbortController());
const queryFirstTime = useRef(true);

const refetchActionTypes = useCallback(async () => {
try {
setLoading(true);
didCancel.current = false;
abortCtrl.current.abort();
abortCtrl.current = new AbortController();
isCancelledRef.current = false;
abortCtrlRef.current.abort();
abortCtrlRef.current = new AbortController();

const res = await fetchActionTypes({ signal: abortCtrl.current.signal });
const res = await fetchActionTypes({ signal: abortCtrlRef.current.signal });

if (!didCancel.current) {
if (!isCancelledRef.current) {
setLoading(false);
setActionTypes(res);
}
} catch (error) {
if (!didCancel.current) {
if (!isCancelledRef.current) {
setLoading(false);
setActionTypes([]);
errorToToaster({
Expand All @@ -59,8 +59,8 @@ export const useActionTypes = (): UseActionTypesResponse => {
}

return () => {
didCancel.current = true;
abortCtrl.current.abort();
isCancelledRef.current = true;
abortCtrlRef.current.abort();
queryFirstTime.current = true;
};
}, [refetchActionTypes]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { useEffect, useCallback, useReducer } from 'react';
import { useEffect, useCallback, useReducer, useRef } from 'react';
import { getCaseConfigure, patchCaseConfigure, postCaseConfigure } from './api';

import {
Expand Down Expand Up @@ -207,129 +207,128 @@ export const useCaseConfigure = (): ReturnUseCaseConfigure => {
}, []);

const [, dispatchToaster] = useStateToaster();
const isCancelledRefetchRef = useRef(false);
const abortCtrlRefetchRef = useRef(new AbortController());

const refetchCaseConfigure = useCallback(() => {
let didCancel = false;
const abortCtrl = new AbortController();
const isCancelledPersistRef = useRef(false);
const abortCtrlPersistRef = useRef(new AbortController());

const fetchCaseConfiguration = async () => {
try {
setLoading(true);
const res = await getCaseConfigure({ signal: abortCtrl.signal });
if (!didCancel) {
if (res != null) {
setConnector(res.connector);
if (setClosureType != null) {
setClosureType(res.closureType);
}
setVersion(res.version);
setMappings(res.mappings);
const refetchCaseConfigure = useCallback(async () => {
try {
isCancelledRefetchRef.current = false;
abortCtrlRefetchRef.current.abort();
abortCtrlRefetchRef.current = new AbortController();

if (!state.firstLoad) {
setFirstLoad(true);
if (setCurrentConfiguration != null) {
setCurrentConfiguration({
closureType: res.closureType,
connector: {
...res.connector,
},
});
}
}
if (res.error != null) {
errorToToaster({
dispatchToaster,
error: new Error(res.error),
title: i18n.ERROR_TITLE,
setLoading(true);
const res = await getCaseConfigure({ signal: abortCtrlRefetchRef.current.signal });

if (!isCancelledRefetchRef.current) {
if (res != null) {
setConnector(res.connector);
if (setClosureType != null) {
setClosureType(res.closureType);
}
setVersion(res.version);
setMappings(res.mappings);

if (!state.firstLoad) {
setFirstLoad(true);
if (setCurrentConfiguration != null) {
setCurrentConfiguration({
closureType: res.closureType,
connector: {
...res.connector,
},
});
}
}
setLoading(false);
if (res.error != null) {
errorToToaster({
dispatchToaster,
error: new Error(res.error),
title: i18n.ERROR_TITLE,
});
}
}
} catch (error) {
if (!didCancel) {
setLoading(false);
setLoading(false);
}
} catch (error) {
if (!isCancelledRefetchRef.current) {
if (error.name !== 'AbortError') {
errorToToaster({
dispatchToaster,
error: error.body && error.body.message ? new Error(error.body.message) : error,
title: i18n.ERROR_TITLE,
});
}
setLoading(false);
}
};

fetchCaseConfiguration();

return () => {
didCancel = true;
abortCtrl.abort();
};
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [state.firstLoad]);

const persistCaseConfigure = useCallback(
async ({ connector, closureType }: ConnectorConfiguration) => {
let didCancel = false;
const abortCtrl = new AbortController();
const saveCaseConfiguration = async () => {
try {
setPersistLoading(true);
const connectorObj = {
connector,
closure_type: closureType,
};
const res =
state.version.length === 0
? await postCaseConfigure(connectorObj, abortCtrl.signal)
: await patchCaseConfigure(
{
...connectorObj,
version: state.version,
},
abortCtrl.signal
);
if (!didCancel) {
setConnector(res.connector);
if (setClosureType) {
setClosureType(res.closureType);
}
setVersion(res.version);
setMappings(res.mappings);
if (setCurrentConfiguration != null) {
setCurrentConfiguration({
closureType: res.closureType,
connector: {
...res.connector,
try {
isCancelledPersistRef.current = false;
abortCtrlPersistRef.current.abort();
abortCtrlPersistRef.current = new AbortController();
setPersistLoading(true);

const connectorObj = {
connector,
closure_type: closureType,
};

const res =
state.version.length === 0
? await postCaseConfigure(connectorObj, abortCtrlPersistRef.current.signal)
: await patchCaseConfigure(
{
...connectorObj,
version: state.version,
},
});
}
if (res.error != null) {
errorToToaster({
dispatchToaster,
error: new Error(res.error),
title: i18n.ERROR_TITLE,
});
}
displaySuccessToast(i18n.SUCCESS_CONFIGURE, dispatchToaster);
setPersistLoading(false);
abortCtrlPersistRef.current.signal
);

if (!isCancelledPersistRef.current) {
setConnector(res.connector);
if (setClosureType) {
setClosureType(res.closureType);
}
setVersion(res.version);
setMappings(res.mappings);
if (setCurrentConfiguration != null) {
setCurrentConfiguration({
closureType: res.closureType,
connector: {
...res.connector,
},
});
}
} catch (error) {
if (!didCancel) {
setConnector(state.currentConfiguration.connector);
setPersistLoading(false);
if (res.error != null) {
errorToToaster({
dispatchToaster,
error: new Error(res.error),
title: i18n.ERROR_TITLE,
});
}
displaySuccessToast(i18n.SUCCESS_CONFIGURE, dispatchToaster);
setPersistLoading(false);
}
} catch (error) {
if (!isCancelledPersistRef.current) {
if (error.name !== 'AbortError') {
errorToToaster({
title: i18n.ERROR_TITLE,
error: error.body && error.body.message ? new Error(error.body.message) : error,
dispatchToaster,
});
}
setConnector(state.currentConfiguration.connector);
setPersistLoading(false);
}
};
saveCaseConfiguration();
return () => {
didCancel = true;
abortCtrl.abort();
};
}
},
[
dispatchToaster,
Expand All @@ -345,6 +344,12 @@ export const useCaseConfigure = (): ReturnUseCaseConfigure => {

useEffect(() => {
refetchCaseConfigure();
return () => {
isCancelledRefetchRef.current = true;
abortCtrlRefetchRef.current.abort();
isCancelledPersistRef.current = true;
abortCtrlPersistRef.current.abort();
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { useState, useEffect, useCallback } from 'react';
import { useState, useEffect, useCallback, useRef } from 'react';

import { useStateToaster, errorToToaster } from '../../../common/components/toasters';
import * as i18n from '../translations';
Expand All @@ -22,40 +22,45 @@ export const useConnectors = (): UseConnectorsResponse => {
const [, dispatchToaster] = useStateToaster();
const [loading, setLoading] = useState(true);
const [connectors, setConnectors] = useState<ActionConnector[]>([]);
const isCancelledRef = useRef(false);
const abortCtrlRef = useRef(new AbortController());

const refetchConnectors = useCallback(() => {
let didCancel = false;
const abortCtrl = new AbortController();
const getConnectors = async () => {
try {
setLoading(true);
const res = await fetchConnectors({ signal: abortCtrl.signal });
if (!didCancel) {
setLoading(false);
setConnectors(res);
}
} catch (error) {
if (!didCancel) {
setLoading(false);
setConnectors([]);
const refetchConnectors = useCallback(async () => {
try {
isCancelledRef.current = false;
abortCtrlRef.current.abort();
abortCtrlRef.current = new AbortController();

setLoading(true);
const res = await fetchConnectors({ signal: abortCtrlRef.current.signal });

if (!isCancelledRef.current) {
setLoading(false);
setConnectors(res);
}
} catch (error) {
if (!isCancelledRef.current) {
if (error.name !== 'AbortError') {
errorToToaster({
title: i18n.ERROR_TITLE,
error: error.body && error.body.message ? new Error(error.body.message) : error,
dispatchToaster,
});
}

setLoading(false);
setConnectors([]);
}
};
getConnectors();
return () => {
didCancel = true;
abortCtrl.abort();
};
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

useEffect(() => {
refetchConnectors();
return () => {
isCancelledRef.current = true;
abortCtrlRef.current.abort();
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

Expand Down
Loading

0 comments on commit f8c4468

Please sign in to comment.