Skip to content

Commit 4fac2b5

Browse files
handle axios cancel request
1 parent 5656af5 commit 4fac2b5

File tree

8 files changed

+66
-17
lines changed

8 files changed

+66
-17
lines changed

src/api/fetchApi/index.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import axios from 'axios';
22
import { httpMethods } from '../constants';
3-
export const source = axios.CancelToken.source();
3+
export let source: any = { cancel: [] };
44

55
export const DEFAULT_HEADERS = {
66
Accept: 'application/json',
@@ -42,11 +42,16 @@ export const fetchApiWithAuthRequest = ({
4242
authenticationToken: string;
4343
headers?: any;
4444
}): Promise<any> => {
45+
const CancelToken = axios.CancelToken;
46+
4547
return axios({
4648
method: method || httpMethods.get,
4749
url,
4850
data,
49-
cancelToken: source.token,
51+
cancelToken: new CancelToken(function executor(c) {
52+
// An executor function receives a cancel function as a parameter
53+
source.cancel.push(c);
54+
}),
5055
headers: {
5156
...DEFAULT_HEADERS,
5257
...headers,

src/redux/sagas/requestSaga.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,11 @@ export function* handleRequestSaga(action: any) {
9595
} catch (e) {
9696
if (isUnauthenticatedError(e, action)) {
9797
yield* handleUnauthenticated(action);
98-
} else if (e.response.status === httpStatus.unprocessablEntity) {
98+
} else if (e?.response?.status === httpStatus.unprocessablEntity) {
9999
yield* unprocessablEntity();
100100
} else {
101101
let errorText = 'Something went wrong!';
102-
if (e.message.indexOf('Network Error') === -1) {
102+
if (e?.message?.indexOf('Network Error') === -1) {
103103
// this means its not a generic message from the server
104104
errorText = e.response ? e.response.data.detail : '';
105105
}

src/ui/layouts/pipelines/Pipelines/index.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState } from 'react';
1+
import React, { useEffect, useState } from 'react';
22
import { translate } from './translate';
33
import { List } from './List';
44
import { AllRuns } from './AllRuns';
@@ -10,6 +10,7 @@ import FilterComponent, {
1010
getInitialFilterStateForPipeline,
1111
getInitialFilterStateForRuns,
1212
} from '../../../components/Filters';
13+
import { source } from '../../../../api/fetchApi';
1314

1415
const FilterWrapper = () => {
1516
// TODO: Dev please note: getInitialFilterState is for stack inital filter value for any other component you need to modify it
@@ -71,8 +72,9 @@ const PAGES = [
7172
];
7273

7374
export const Pipelines: React.FC = () => {
74-
const { setFetching } = useService();
75-
console.log(setFetching);
75+
const { setFetchingForAllRuns } = useService();
76+
77+
console.log(setFetchingForAllRuns);
7678
const locationPath = useLocationPath();
7779

7880
return (
Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,53 @@
11
/* eslint-disable */
22

3+
import axios from 'axios';
34
import { useEffect } from 'react';
5+
import { source } from '../../../../api/fetchApi';
6+
// import { cancel } from '../../../../api/fetchApi';
7+
48
import {
59
pipelinePagesActions,
610
runsActions,
711
pipelinesActions,
12+
runPagesActions,
813
} from '../../../../redux/actions';
914

1015
import { useDispatch } from '../../../hooks';
1116

1217
interface ServiceInterface {
13-
setFetching: (arg: boolean) => void;
18+
setFetchingForPipeline: (arg: boolean) => void;
19+
setFetchingForAllRuns: (arg: boolean) => void;
1420
}
1521

1622
export const useService = (): ServiceInterface => {
1723
const dispatch = useDispatch();
1824

1925
useEffect(() => {
20-
setFetching(true);
21-
26+
setFetchingForPipeline(true);
27+
setFetchingForAllRuns(true);
2228
dispatch(
2329
runsActions.allRuns({
24-
onSuccess: () => setFetching(false),
25-
onFailure: () => setFetching(false),
30+
onSuccess: () => setFetchingForAllRuns(false),
31+
onFailure: () => setFetchingForAllRuns(false),
2632
}),
2733
);
2834
dispatch(
2935
pipelinesActions.getMy({
30-
onSuccess: () => setFetching(false),
31-
onFailure: () => setFetching(false),
36+
onSuccess: () => setFetchingForPipeline(false),
37+
onFailure: () => setFetchingForPipeline(false),
3238
}),
3339
);
3440
}, []);
3541

36-
const setFetching = (fetching: boolean) => {
42+
const setFetchingForPipeline = (fetching: boolean) => {
3743
dispatch(pipelinePagesActions.setFetching({ fetching }));
3844
};
45+
const setFetchingForAllRuns = (fetching: boolean) => {
46+
dispatch(runPagesActions.setFetching({ fetching }));
47+
};
3948

4049
return {
41-
setFetching,
50+
setFetchingForPipeline,
51+
setFetchingForAllRuns,
4252
};
4353
};

src/ui/layouts/pipelines/RunsTable/useService.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { pipelinePagesActions } from '../../../../redux/actions';
88
import { useDispatch, useSelector } from '../../../hooks';
99
import { runSelectors } from '../../../../redux/selectors';
1010
import { getFilteredDataForTable } from '../../../../utils/tableFilters';
11+
import { source } from '../../../../api/fetchApi';
1112

1213
interface ServiceInterface {
1314
sortedRuns: TRun[];
@@ -67,6 +68,14 @@ export const useService = ({
6768
setSortedRuns(orderedRuns);
6869
}, [filter, runIds]);
6970

71+
useEffect(() => {
72+
return () => {
73+
source.cancel.forEach((element: any) => {
74+
element();
75+
});
76+
};
77+
}, []);
78+
7079
const setSelectedRunIds = (runIds: TId[]) => {
7180
dispatch(pipelinePagesActions.setSelectedRunIds({ runIds }));
7281
};

src/ui/layouts/runs/RunsTable/useService.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Sorting, SortingDirection } from './types';
77
import { stackPagesActions } from '../../../../redux/actions';
88
import { useDispatch, useSelector } from '../../../hooks';
99
import { runSelectors } from '../../../../redux/selectors';
10+
import { source } from '../../../../api/fetchApi';
1011

1112
interface ServiceInterface {
1213
sortedRuns: TRun[];
@@ -40,6 +41,13 @@ export const useService = ({ runIds }: { runIds: TId[] }): ServiceInterface => {
4041

4142
setSortedRuns(orderedRuns as TRun[]);
4243
}, []);
44+
useEffect(() => {
45+
return () => {
46+
source.cancel.forEach((element: any) => {
47+
element();
48+
});
49+
};
50+
}, []);
4351

4452
const setSelectedRunIds = (runIds: TId[]) => {
4553
dispatch(stackPagesActions.setSelectedRunIds({ runIds }));

src/ui/layouts/stackComponents/RunsTable/useService.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { stackPagesActions } from '../../../../redux/actions';
88
import { useDispatch, useSelector } from '../../../hooks';
99
import { runSelectors } from '../../../../redux/selectors';
1010
import { getFilteredDataForTable } from '../../../../utils/tableFilters';
11+
import { source } from '../../../../api/fetchApi';
1112

1213
interface ServiceInterface {
1314
sortedRuns: TRun[];
@@ -60,6 +61,13 @@ export const useService = ({
6061
}
6162
setSortedRuns(orderedRuns as TRun[]);
6263
}, [filter]);
64+
useEffect(() => {
65+
return () => {
66+
source.cancel.forEach((element: any) => {
67+
element();
68+
});
69+
};
70+
}, []);
6371

6472
const setSelectedRunIds = (runIds: TId[]) => {
6573
dispatch(stackPagesActions.setSelectedRunIds({ runIds }));

src/ui/layouts/stacks/RunsTable/useService.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { stackPagesActions } from '../../../../redux/actions';
88
import { useDispatch, useSelector } from '../../../hooks';
99
import { runSelectors } from '../../../../redux/selectors';
1010
import { getFilteredDataForTable } from '../../../../utils/tableFilters';
11+
import { source } from '../../../../api/fetchApi';
1112

1213
interface ServiceInterface {
1314
sortedRuns: TRun[];
@@ -60,7 +61,13 @@ export const useService = ({
6061
}
6162
setSortedRuns(orderedRuns as TRun[]);
6263
}, [filter, runIds]);
63-
64+
useEffect(() => {
65+
return () => {
66+
source.cancel.forEach((element: any) => {
67+
element();
68+
});
69+
};
70+
}, []);
6471
const setSelectedRunIds = (runIds: TId[]) => {
6572
dispatch(stackPagesActions.setSelectedRunIds({ runIds }));
6673
};

0 commit comments

Comments
 (0)