1
1
import { createSlice , PayloadAction } from '@reduxjs/toolkit'
2
- import { AxiosError } from 'axios'
2
+ import axios , { AxiosError } from 'axios'
3
3
import { apiService , } from 'uiSrc/services'
4
4
import { addErrorNotification } from 'uiSrc/slices/app/notifications'
5
- import { getApiErrorMessage , getRdiUrl , isStatusSuccessful , transformConnectionResults } from 'uiSrc/utils'
5
+ import {
6
+ getApiErrorMessage ,
7
+ getRdiUrl ,
8
+ isStatusSuccessful ,
9
+ Maybe ,
10
+ Nullable ,
11
+ transformConnectionResults
12
+ } from 'uiSrc/utils'
6
13
import {
7
14
IStateRdiTestConnections ,
8
15
TestConnectionsResponse ,
@@ -31,10 +38,13 @@ const rdiTestConnectionsSlice = createSlice({
31
38
state . results = payload
32
39
state . error = ''
33
40
} ,
34
- testConnectionsFailure : ( state , { payload } ) => {
41
+ testConnectionsFailure : ( state , { payload } : PayloadAction < Maybe < string > > ) => {
35
42
state . loading = false
36
- state . error = payload
37
43
state . results = null
44
+
45
+ if ( payload ) {
46
+ state . error = payload
47
+ }
38
48
} ,
39
49
}
40
50
} )
@@ -50,6 +60,9 @@ export const {
50
60
// The reducer
51
61
export default rdiTestConnectionsSlice . reducer
52
62
63
+ // eslint-disable-next-line import/no-mutable-exports
64
+ export let testConnectionsController : Nullable < AbortController > = null
65
+
53
66
// Asynchronous thunk action
54
67
export function testConnectionsAction (
55
68
rdiInstanceId : string ,
@@ -58,23 +71,36 @@ export function testConnectionsAction(
58
71
onFailAction ?: ( ) => void ,
59
72
) {
60
73
return async ( dispatch : AppDispatch ) => {
74
+ dispatch ( testConnections ( ) )
75
+
61
76
try {
62
- dispatch ( testConnections ( ) )
77
+ testConnectionsController ?. abort ( )
78
+ testConnectionsController = new AbortController ( )
79
+
63
80
const { status, data } = await apiService . post < TestConnectionsResponse > (
64
81
getRdiUrl ( rdiInstanceId , ApiEndpoints . RDI_TEST_CONNECTIONS ) ,
65
82
config ,
83
+ {
84
+ signal : testConnectionsController . signal
85
+ }
66
86
)
67
87
88
+ testConnectionsController = null
89
+
68
90
if ( isStatusSuccessful ( status ) ) {
69
91
dispatch ( testConnectionsSuccess ( transformConnectionResults ( data ?. sources ) ) )
70
92
onSuccessAction ?.( )
71
93
}
72
94
} catch ( _err ) {
73
- const error = _err as AxiosError
74
- const errorMessage = getApiErrorMessage ( error )
75
- dispatch ( addErrorNotification ( error ) )
76
- dispatch ( testConnectionsFailure ( errorMessage ) )
77
- onFailAction ?.( )
95
+ if ( ! axios . isCancel ( _err ) ) {
96
+ const error = _err as AxiosError
97
+ const errorMessage = getApiErrorMessage ( error )
98
+ dispatch ( addErrorNotification ( error ) )
99
+ dispatch ( testConnectionsFailure ( errorMessage ) )
100
+ onFailAction ?.( )
101
+ } else {
102
+ dispatch ( testConnectionsFailure ( ) )
103
+ }
78
104
}
79
105
}
80
106
}
0 commit comments