1
1
import { createSlice , PayloadAction } from '@reduxjs/toolkit'
2
2
3
- import { AxiosError } from 'axios'
3
+ import axios , { AxiosError } from 'axios'
4
4
import { ApiEndpoints , BulkActionsType , MAX_BULK_ACTION_ERRORS_LENGTH } from 'uiSrc/constants'
5
5
import { apiService } from 'uiSrc/services'
6
- import { getApiErrorMessage , getUrl , isStatusSuccessful } from 'uiSrc/utils'
6
+ import { getApiErrorMessage , getUrl , isStatusSuccessful , Maybe , Nullable } from 'uiSrc/utils'
7
7
8
8
import { addErrorNotification } from 'uiSrc/slices/app/notifications'
9
9
import { AppDispatch , RootState } from '../store'
@@ -37,11 +37,7 @@ const bulkActionsSlice = createSlice({
37
37
name : 'bulkActions' ,
38
38
initialState,
39
39
reducers : {
40
- setBulkActionsInitialState : ( state ) => {
41
- state . bulkUpload ?. abortController ?. abort ( )
42
-
43
- return initialState
44
- } ,
40
+ setBulkActionsInitialState : ( ) => initialState ,
45
41
46
42
setBulkDeleteStartAgain : ( state ) => {
47
43
state . bulkDelete = initialState . bulkDelete
@@ -107,10 +103,9 @@ const bulkActionsSlice = createSlice({
107
103
state . bulkDelete . loading = false
108
104
} ,
109
105
110
- bulkUpload : ( state , { payload } : PayloadAction < { abortController : AbortController } > ) => {
106
+ bulkUpload : ( state ) => {
111
107
state . bulkUpload . loading = true
112
108
state . bulkUpload . error = ''
113
- state . bulkUpload . abortController = payload . abortController
114
109
} ,
115
110
116
111
bulkUploadSuccess : ( state , { payload } : PayloadAction < { data : IBulkActionOverview , fileName ?: string } > ) => {
@@ -119,9 +114,12 @@ const bulkActionsSlice = createSlice({
119
114
state . bulkUpload . fileName = payload . fileName
120
115
} ,
121
116
122
- bulkUploadFailed : ( state , { payload } ) => {
117
+ bulkUploadFailed : ( state , { payload } : PayloadAction < Maybe < string > > ) => {
123
118
state . bulkUpload . loading = false
124
- state . bulkUpload . error = payload
119
+
120
+ if ( payload ) {
121
+ state . bulkUpload . error = payload
122
+ }
125
123
} ,
126
124
} ,
127
125
} )
@@ -162,6 +160,9 @@ export const bulkActionsUploadSummarySelector = (state: RootState) =>
162
160
// The reducer
163
161
export default bulkActionsSlice . reducer
164
162
163
+ // eslint-disable-next-line import/no-mutable-exports
164
+ export let uploadController : Nullable < AbortController > = null
165
+
165
166
// Thunk actions
166
167
// Asynchronous thunk action
167
168
export function bulkUploadDataAction (
@@ -171,11 +172,12 @@ export function bulkUploadDataAction(
171
172
onFailAction ?: ( ) => void
172
173
) {
173
174
return async ( dispatch : AppDispatch ) => {
174
- const abortController = new AbortController ( )
175
-
176
- dispatch ( bulkUpload ( { abortController } ) )
175
+ dispatch ( bulkUpload ( ) )
177
176
178
177
try {
178
+ uploadController ?. abort ( )
179
+ uploadController = new AbortController ( )
180
+
179
181
const { status, data } = await apiService . post (
180
182
getUrl (
181
183
id ,
@@ -187,22 +189,33 @@ export function bulkUploadDataAction(
187
189
Accept : 'application/json' ,
188
190
'Content-Type' : 'multipart/form-data'
189
191
} ,
190
- signal : abortController . signal
192
+ signal : uploadController . signal
191
193
}
192
194
)
193
195
196
+ uploadController = null
197
+
194
198
if ( isStatusSuccessful ( status ) ) {
195
199
dispatch ( bulkUploadSuccess ( { data, fileName : uploadFile . fileName } ) )
196
200
onSuccessAction ?.( )
197
201
}
198
202
} catch ( error ) {
199
- // show error when request wasn't aborted only
200
- if ( ! abortController . signal . aborted ) {
203
+ // show error when request wasn't aborted
204
+ if ( ! axios . isCancel ( error ) ) {
201
205
const errorMessage = getApiErrorMessage ( error as AxiosError )
202
206
dispatch ( addErrorNotification ( error as AxiosError ) )
203
207
dispatch ( bulkUploadFailed ( errorMessage ) )
204
208
onFailAction ?.( )
209
+ } else {
210
+ dispatch ( bulkUploadFailed ( ) )
205
211
}
206
212
}
207
213
}
208
214
}
215
+
216
+ export function resetBulkActions ( ) {
217
+ return async ( dispatch : AppDispatch ) => {
218
+ uploadController ?. abort ( )
219
+ dispatch ( setBulkActionsInitialState ( ) )
220
+ }
221
+ }
0 commit comments