1
- import { Draft , PayloadAction , createAsyncThunk , createSlice } from '@reduxjs/toolkit' ;
1
+ import {
2
+ Draft ,
3
+ PayloadAction ,
4
+ SerializedError ,
5
+ createAsyncThunk ,
6
+ createSlice ,
7
+ } from '@reduxjs/toolkit' ;
2
8
import * as z from 'zod' ;
3
9
4
10
import { jsonGet , jsonPost , routes } from '../../api' ;
@@ -22,6 +28,7 @@ interface State {
22
28
channel ?: Channel ;
23
29
mode ?: Mode ;
24
30
edition ?: Edition ;
31
+ error ?: string ;
25
32
}
26
33
27
34
interface SuccessProps {
@@ -81,6 +88,7 @@ export const performGistSave = createAsyncThunk<SuccessProps, void, { state: Roo
81
88
) ;
82
89
83
90
const pending = ( state : Draft < State > ) => {
91
+ delete state . error ;
84
92
state . requestsInProgress += 1 ;
85
93
} ;
86
94
@@ -89,6 +97,14 @@ const fulfilled = (state: Draft<State>, action: PayloadAction<SuccessProps>) =>
89
97
Object . assign ( state , action . payload ) ;
90
98
} ;
91
99
100
+ const rejected = (
101
+ state : Draft < State > ,
102
+ action : PayloadAction < unknown , string , unknown , SerializedError > ,
103
+ ) => {
104
+ state . requestsInProgress -= 1 ;
105
+ state . error = action . error . message ;
106
+ } ;
107
+
92
108
const slice = createSlice ( {
93
109
name : sliceName ,
94
110
initialState,
@@ -97,8 +113,10 @@ const slice = createSlice({
97
113
builder
98
114
. addCase ( performGistLoad . pending , pending )
99
115
. addCase ( performGistLoad . fulfilled , fulfilled )
116
+ . addCase ( performGistLoad . rejected , rejected )
100
117
. addCase ( performGistSave . pending , pending )
101
- . addCase ( performGistSave . fulfilled , fulfilled ) ;
118
+ . addCase ( performGistSave . fulfilled , fulfilled )
119
+ . addCase ( performGistSave . rejected , rejected ) ;
102
120
} ,
103
121
} ) ;
104
122
0 commit comments