@@ -454,20 +454,26 @@ interface MyKnownError {
454
454
errorMessage: string
455
455
// ...
456
456
}
457
+ interface UserAttributes {
458
+ id: string
459
+ first_name: string
460
+ last_name: string
461
+ email: string
462
+ }
457
463
458
- const updateUserById = createAsyncThunk <
464
+ const updateUser = createAsyncThunk <
459
465
// Return type of the payload creator
460
466
MyData ,
461
467
// First argument to the payload creator
462
- { id : string ; first_name : string ; last_name : string ; email : string } ,
468
+ UserAttributes ,
463
469
// Types for ThunkAPI
464
470
{
465
471
extra : {
466
472
jwt : string
467
473
}
468
474
rejectValue : MyKnownError
469
475
}
470
- > (' users/updateById ' , async (user , thunkApi ) => {
476
+ > (' users/update ' , async (user , thunkApi ) => {
471
477
const { id, ... userData } = user
472
478
const response = await fetch (` https://reqres.in/api/users/${id } ` , {
473
479
method: ' PUT' ,
@@ -499,12 +505,12 @@ const usersSlice = createSlice({
499
505
},
500
506
reducers: {},
501
507
extraReducers : builder => {
502
- builder .addCase (updateUserById .fulfilled , (state , { payload }) => {
508
+ builder .addCase (updateUser .fulfilled , (state , { payload }) => {
503
509
state .entities [payload .id ] = payload
504
510
})
505
- builder .addCase (updateUserById .rejected , (state , action ) => {
511
+ builder .addCase (updateUser .rejected , (state , action ) => {
506
512
if (action .payload ) {
507
- // Since we passed in `MyKnownError` to `rejectType` in `updateUserById `, the type information will be available here.
513
+ // Since we passed in `MyKnownError` to `rejectType` in `updateUser `, the type information will be available here.
508
514
state .error = action .payload .errorMessage
509
515
} else {
510
516
state .error = action .error
@@ -517,16 +523,16 @@ const usersSlice = createSlice({
517
523
- In a component
518
524
519
525
``` ts
520
- const updateUser = async userData => {
521
- const { id, ... userFields } = userData
522
- const resultAction = await dispatch (updateUserById (userFields ))
526
+ const handleUpdateUser = async userData => {
527
+ const resultAction = await dispatch (updateUser (userData ))
523
528
if (updateUser .fulfilled .match (resultAction )) {
524
529
const user = unwrapResult (resultAction )
525
530
showToast (' success' , ` Updated ${user .name } ` )
526
531
} else {
527
532
if (resultAction .payload ) {
528
- // Since we passed in `MyKnownError` to `rejectType` in `updateUserById`, the type information will be available here.
529
- showToast (' error' , ` Update failed: ${rersultAction .payload .errorMessage } ` )
533
+ // Since we passed in `MyKnownError` to `rejectType` in `updateUser`, the type information will be available here.
534
+ // Note: this would also be a good place to do any handling that relies on the `rejectedWithValue` payload, such as setting field errors
535
+ showToast (' error' , ` Update failed: ${resultAction .payload .errorMessage } ` )
530
536
} else {
531
537
showToast (' error' , ` Update failed: ${resultAction .error .message } ` )
532
538
}
0 commit comments