Skip to content

Commit

Permalink
Improve hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
cnasikas committed Feb 15, 2021
1 parent c1d1b2b commit 4155a16
Show file tree
Hide file tree
Showing 21 changed files with 571 additions and 457 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import { useState, useEffect, useRef } from 'react';
import { HttpSetup, ToastsApi } from 'kibana/public';
import { AbortError } from '../../../../../../../../src/plugins/kibana_utils/common';
import { ActionConnector } from '../../../containers/types';
import { getFieldsByIssueType } from './api';
import { Fields } from './types';
Expand Down Expand Up @@ -35,27 +36,28 @@ export const useGetFieldsByIssueType = ({
}: Props): UseGetFieldsByIssueType => {
const [isLoading, setIsLoading] = useState(true);
const [fields, setFields] = useState<Fields>({});
const didCancel = useRef(false);
const abortCtrl = useRef(new AbortController());

useEffect(() => {
let didCancel = false;
const fetchData = async () => {
if (!connector || !issueType) {
setIsLoading(false);
return;
}

abortCtrl.current = new AbortController();
setIsLoading(true);
try {
abortCtrl.current = new AbortController();
setIsLoading(true);

const res = await getFieldsByIssueType({
http,
signal: abortCtrl.current.signal,
connectorId: connector.id,
id: issueType,
});

if (!didCancel) {
if (!didCancel.current) {
setIsLoading(false);
setFields(res.data ?? {});
if (res.status && res.status === 'error') {
Expand All @@ -66,22 +68,24 @@ export const useGetFieldsByIssueType = ({
}
}
} catch (error) {
if (!didCancel) {
if (!didCancel.current) {
setIsLoading(false);
toastNotifications.addDanger({
title: i18n.FIELDS_API_ERROR,
text: error.message,
});
if (!(error instanceof AbortError)) {
toastNotifications.addDanger({
title: i18n.FIELDS_API_ERROR,
text: error.message,
});
}
}
}
};

didCancel.current = false;
abortCtrl.current.abort();
fetchData();

return () => {
didCancel = true;
setIsLoading(false);
didCancel.current = true;
abortCtrl.current.abort();
};
}, [http, connector, issueType, toastNotifications]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import { useState, useEffect, useRef } from 'react';
import { HttpSetup, ToastsApi } from 'kibana/public';
import { AbortError } from '../../../../../../../../src/plugins/kibana_utils/common';
import { ActionConnector } from '../../../containers/types';
import { getIssueTypes } from './api';
import { IssueTypes } from './types';
Expand Down Expand Up @@ -35,27 +36,27 @@ export const useGetIssueTypes = ({
}: Props): UseGetIssueTypes => {
const [isLoading, setIsLoading] = useState(true);
const [issueTypes, setIssueTypes] = useState<IssueTypes>([]);
const didCancel = useRef(false);
const abortCtrl = useRef(new AbortController());

useEffect(() => {
let didCancel = false;
const fetchData = async () => {
if (!connector) {
setIsLoading(false);
return;
}

abortCtrl.current = new AbortController();
setIsLoading(true);

try {
abortCtrl.current = new AbortController();
setIsLoading(true);

const res = await getIssueTypes({
http,
signal: abortCtrl.current.signal,
connectorId: connector.id,
});

if (!didCancel) {
if (!didCancel.current) {
setIsLoading(false);
const asOptions = (res.data ?? []).map((type) => ({
text: type.name ?? '',
Expand All @@ -71,22 +72,24 @@ export const useGetIssueTypes = ({
}
}
} catch (error) {
if (!didCancel) {
if (!didCancel.current) {
setIsLoading(false);
toastNotifications.addDanger({
title: i18n.ISSUE_TYPES_API_ERROR,
text: error.message,
});
if (!(error instanceof AbortError)) {
toastNotifications.addDanger({
title: i18n.ISSUE_TYPES_API_ERROR,
text: error.message,
});
}
}
}
};

didCancel.current = false;
abortCtrl.current.abort();
fetchData();

return () => {
didCancel = true;
setIsLoading(false);
didCancel.current = true;
abortCtrl.current.abort();
};
}, [http, connector, toastNotifications, handleIssueType]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import { isEmpty, debounce } from 'lodash/fp';
import { useState, useEffect, useRef } from 'react';
import { HttpSetup, ToastsApi } from 'kibana/public';
import { AbortError } from '../../../../../../../../src/plugins/kibana_utils/common';
import { ActionConnector } from '../../../containers/types';
import { getIssues } from './api';
import { Issues } from './types';
Expand Down Expand Up @@ -36,28 +37,28 @@ export const useGetIssues = ({
}: Props): UseGetIssues => {
const [isLoading, setIsLoading] = useState(false);
const [issues, setIssues] = useState<Issues>([]);
const didCancel = useRef(false);
const abortCtrl = useRef(new AbortController());

useEffect(() => {
let didCancel = false;
const fetchData = debounce(500, async () => {
if (!actionConnector || isEmpty(query)) {
setIsLoading(false);
return;
}

abortCtrl.current = new AbortController();
setIsLoading(true);

try {
abortCtrl.current = new AbortController();
setIsLoading(true);

const res = await getIssues({
http,
signal: abortCtrl.current.signal,
connectorId: actionConnector.id,
title: query ?? '',
});

if (!didCancel) {
if (!didCancel.current) {
setIsLoading(false);
setIssues(res.data ?? []);
if (res.status && res.status === 'error') {
Expand All @@ -68,22 +69,24 @@ export const useGetIssues = ({
}
}
} catch (error) {
if (!didCancel) {
if (!didCancel.current) {
setIsLoading(false);
toastNotifications.addDanger({
title: i18n.ISSUES_API_ERROR,
text: error.message,
});
if (!(error instanceof AbortError)) {
toastNotifications.addDanger({
title: i18n.ISSUES_API_ERROR,
text: error.message,
});
}
}
}
});

didCancel.current = false;
abortCtrl.current.abort();
fetchData();

return () => {
didCancel = true;
setIsLoading(false);
didCancel.current = true;
abortCtrl.current.abort();
};
}, [http, actionConnector, toastNotifications, query]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import { useState, useEffect, useRef } from 'react';
import { HttpSetup, ToastsApi } from 'kibana/public';
import { AbortError } from '../../../../../../../../src/plugins/kibana_utils/common';
import { ActionConnector } from '../../../containers/types';
import { getIssue } from './api';
import { Issue } from './types';
Expand Down Expand Up @@ -35,10 +36,10 @@ export const useGetSingleIssue = ({
}: Props): UseGetSingleIssue => {
const [isLoading, setIsLoading] = useState(false);
const [issue, setIssue] = useState<Issue | null>(null);
const didCancel = useRef(false);
const abortCtrl = useRef(new AbortController());

useEffect(() => {
let didCancel = false;
const fetchData = async () => {
if (!actionConnector || !id) {
setIsLoading(false);
Expand All @@ -55,7 +56,7 @@ export const useGetSingleIssue = ({
id,
});

if (!didCancel) {
if (!didCancel.current) {
setIsLoading(false);
setIssue(res.data ?? null);
if (res.status && res.status === 'error') {
Expand All @@ -66,22 +67,24 @@ export const useGetSingleIssue = ({
}
}
} catch (error) {
if (!didCancel) {
if (!didCancel.current) {
setIsLoading(false);
toastNotifications.addDanger({
title: i18n.GET_ISSUE_API_ERROR(id),
text: error.message,
});
if (!(error instanceof AbortError)) {
toastNotifications.addDanger({
title: i18n.GET_ISSUE_API_ERROR(id),
text: error.message,
});
}
}
}
};

didCancel.current = false;
abortCtrl.current.abort();
fetchData();

return () => {
didCancel = true;
setIsLoading(false);
didCancel.current = true;
abortCtrl.current.abort();
};
}, [http, actionConnector, id, toastNotifications]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import { useState, useEffect, useRef } from 'react';
import { HttpSetup, ToastsApi } from 'kibana/public';
import { AbortError } from '../../../../../../../../src/plugins/kibana_utils/common';
import { ActionConnector } from '../../../containers/types';
import { getIncidentTypes } from './api';
import * as i18n from './translations';
Expand Down Expand Up @@ -34,27 +35,27 @@ export const useGetIncidentTypes = ({
}: Props): UseGetIncidentTypes => {
const [isLoading, setIsLoading] = useState(true);
const [incidentTypes, setIncidentTypes] = useState<IncidentTypes>([]);
const didCancel = useRef(false);
const abortCtrl = useRef(new AbortController());

useEffect(() => {
let didCancel = false;
const fetchData = async () => {
if (!connector) {
setIsLoading(false);
return;
}

abortCtrl.current = new AbortController();
setIsLoading(true);

try {
abortCtrl.current = new AbortController();
setIsLoading(true);

const res = await getIncidentTypes({
http,
signal: abortCtrl.current.signal,
connectorId: connector.id,
});

if (!didCancel) {
if (!didCancel.current) {
setIsLoading(false);
setIncidentTypes(res.data ?? []);
if (res.status && res.status === 'error') {
Expand All @@ -65,22 +66,24 @@ export const useGetIncidentTypes = ({
}
}
} catch (error) {
if (!didCancel) {
if (!didCancel.current) {
setIsLoading(false);
toastNotifications.addDanger({
title: i18n.INCIDENT_TYPES_API_ERROR,
text: error.message,
});
if (!(error instanceof AbortError)) {
toastNotifications.addDanger({
title: i18n.INCIDENT_TYPES_API_ERROR,
text: error.message,
});
}
}
}
};

didCancel.current = false;
abortCtrl.current.abort();
fetchData();

return () => {
didCancel = true;
setIsLoading(false);
didCancel.current = true;
abortCtrl.current.abort();
};
}, [http, connector, toastNotifications]);
Expand Down
Loading

0 comments on commit 4155a16

Please sign in to comment.