@@ -325,9 +325,9 @@ import { userAPI } from './userAPI'
325
325
326
326
const fetchUserById = createAsyncThunk (
327
327
' users/fetchByIdStatus' ,
328
- async (userId , { getState }) => {
329
- const { loading } = getState ().users
330
- if (loading !== ' idle ' ) {
328
+ async (userId , { getState , requestId }) => {
329
+ const { currentRequestId, loading } = getState ().users
330
+ if (loading !== ' pending ' || requestId !== currentRequestId ) {
331
331
return
332
332
}
333
333
const response = await userAPI .fetchById (userId )
@@ -340,25 +340,31 @@ const usersSlice = createSlice({
340
340
initialState: {
341
341
entities: [],
342
342
loading: ' idle' ,
343
+ currentRequestId: undefined ,
343
344
error: null
344
345
},
345
346
reducers: {},
346
347
extraReducers: {
347
348
[fetchUserById .pending ]: (state , action ) => {
348
349
if (state .loading === ' idle' ) {
349
350
state .loading = ' pending'
351
+ state .currentRequestId = action .meta .requestId
350
352
}
351
353
},
352
354
[fetchUserById .fulfilled ]: (state , action ) => {
353
- if (state .loading === ' pending' ) {
355
+ const { requestId } = action .meta
356
+ if (state .loading === ' pending' && state .currentRequestId === requestId ) {
354
357
state .loading = ' idle'
355
- state .push (action .payload )
358
+ state .entities .push (action .payload )
359
+ state .currentRequestId = undefined
356
360
}
357
361
},
358
362
[fetchUserById .rejected ]: (state , action ) => {
359
- if (state .loading === ' pending' ) {
363
+ const { requestId } = action .meta
364
+ if (state .loading === ' pending' && state .currentRequestId === requestId ) {
360
365
state .loading = ' idle'
361
366
state .error = action .error
367
+ state .currentRequestId = undefined
362
368
}
363
369
}
364
370
}
0 commit comments