@@ -37,7 +37,11 @@ const bulkActionsSlice = createSlice({
37
37
name : 'bulkActions' ,
38
38
initialState,
39
39
reducers : {
40
- setBulkActionsInitialState : ( ) => initialState ,
40
+ setBulkActionsInitialState : ( state ) => {
41
+ state . bulkUpload ?. abortController ?. abort ( )
42
+
43
+ return initialState
44
+ } ,
41
45
42
46
setBulkDeleteStartAgain : ( state ) => {
43
47
state . bulkDelete = initialState . bulkDelete
@@ -103,9 +107,10 @@ const bulkActionsSlice = createSlice({
103
107
state . bulkDelete . loading = false
104
108
} ,
105
109
106
- bulkUpload : ( state ) => {
110
+ bulkUpload : ( state , { payload } : PayloadAction < { abortController : AbortController } > ) => {
107
111
state . bulkUpload . loading = true
108
112
state . bulkUpload . error = ''
113
+ state . bulkUpload . abortController = payload . abortController
109
114
} ,
110
115
111
116
bulkUploadSuccess : ( state , { payload } : PayloadAction < { data : IBulkActionOverview , fileName ?: string } > ) => {
@@ -166,7 +171,9 @@ export function bulkUploadDataAction(
166
171
onFailAction ?: ( ) => void
167
172
) {
168
173
return async ( dispatch : AppDispatch ) => {
169
- dispatch ( bulkUpload ( ) )
174
+ const abortController = new AbortController ( )
175
+
176
+ dispatch ( bulkUpload ( { abortController } ) )
170
177
171
178
try {
172
179
const { status, data } = await apiService . post (
@@ -179,7 +186,8 @@ export function bulkUploadDataAction(
179
186
headers : {
180
187
Accept : 'application/json' ,
181
188
'Content-Type' : 'multipart/form-data'
182
- }
189
+ } ,
190
+ signal : abortController . signal
183
191
}
184
192
)
185
193
@@ -188,10 +196,13 @@ export function bulkUploadDataAction(
188
196
onSuccessAction ?.( )
189
197
}
190
198
} catch ( error ) {
191
- const errorMessage = getApiErrorMessage ( error as AxiosError )
192
- dispatch ( addErrorNotification ( error as AxiosError ) )
193
- dispatch ( bulkUploadFailed ( errorMessage ) )
194
- onFailAction ?.( )
199
+ // show error when request wasn't aborted only
200
+ if ( ! abortController . signal . aborted ) {
201
+ const errorMessage = getApiErrorMessage ( error as AxiosError )
202
+ dispatch ( addErrorNotification ( error as AxiosError ) )
203
+ dispatch ( bulkUploadFailed ( errorMessage ) )
204
+ onFailAction ?.( )
205
+ }
195
206
}
196
207
}
197
208
}
0 commit comments