@@ -31,38 +31,159 @@ node ./bin/cli.js createReducerBuilder path/of/files/ or/some**/*glob.js
31
31
32
32
## <!-- FIXTURES_CONTENT_START-->
33
33
34
+ ---
35
+
34
36
<a id =" basic-ts " >** basic-ts** </a >
35
37
36
38
** Input** (<small >[ basic-ts.input.ts] ( transforms\createReducerBuilder__testfixtures__\basic-ts.input.ts ) </small >):
37
39
38
40
``` ts
39
- createReducer (initialState , {
40
- [todoAdded ]: (state : SliceState , action : PayloadAction <string >) => {
41
+ import type { PayloadAction } from ' @reduxjs/toolkit'
42
+ import { createEntityAdapter , createReducer } from ' @reduxjs/toolkit'
43
+
44
+ export interface Todo {
45
+ id: string
46
+ title: string
47
+ }
48
+
49
+ export const todoAdapter = createEntityAdapter <Todo >()
50
+
51
+ const todoInitialState = todoAdapter .getInitialState ()
52
+
53
+ export type TodoSliceState = typeof todoInitialState
54
+
55
+ const { addOne } = todoAdapter
56
+
57
+ createReducer (todoInitialState , {
58
+ [todoAdded1a ]: (state : TodoSliceState , action : PayloadAction <string >) => {
41
59
// stuff
42
60
},
43
- });
61
+ [todoAdded1b ]: (state : TodoSliceState , action : PayloadAction <string >) =>
62
+ action .payload ,
63
+ [todoAdded1c + ' test' ]: (
64
+ state : TodoSliceState ,
65
+ action : PayloadAction <string >
66
+ ) => {
67
+ // stuff
68
+ },
69
+ [todoAdded1d ](state : TodoSliceState , action : PayloadAction <string >) {
70
+ // stuff
71
+ },
72
+ [todoAdded1e ]: function (
73
+ state : TodoSliceState ,
74
+ action : PayloadAction <string >
75
+ ) {
76
+ // stuff
77
+ },
78
+ todoAdded1f : (state : TodoSliceState , action : PayloadAction <string >) => {
79
+ // stuff
80
+ },
81
+ [todoAdded1g ]: addOne ,
82
+ todoAdded1h: todoAdapter .addOne
83
+ })
44
84
45
- createReducer (initialState , {
46
- [todoAdded ] (state : SliceState , action : PayloadAction <string >) {
85
+ createReducer (todoInitialState , {
86
+ [todoAdded2a ]: (state : TodoSliceState , action : PayloadAction <string >) => {
47
87
// stuff
48
88
},
49
- });
89
+ [todoAdded2b ](state : TodoSliceState , action : PayloadAction <string >) {
90
+ // stuff
91
+ },
92
+ [todoAdded2c ]: function (
93
+ state : TodoSliceState ,
94
+ action : PayloadAction <string >
95
+ ) {
96
+ // stuff
97
+ }
98
+ })
50
99
```
51
100
52
101
** Output** (<small >[ basic-ts.output.ts] ( transforms\createReducerBuilder__testfixtures__\basic-ts.output.ts ) </small >):
53
102
54
103
``` ts
55
- createReducer (initialState , (builder ) => {
56
- builder .addCase (todoAdded , (state : SliceState , action : PayloadAction <string >) => {
57
- // stuff
58
- });
59
- });
60
-
61
- createReducer (initialState , (builder ) => {
62
- builder .addCase (todoAdded , (state : SliceState , action : PayloadAction <string >) => {
63
- // stuff
64
- });
65
- });
104
+ import type { PayloadAction } from ' @reduxjs/toolkit'
105
+ import { createEntityAdapter , createReducer } from ' @reduxjs/toolkit'
106
+
107
+ export interface Todo {
108
+ id: string
109
+ title: string
110
+ }
111
+
112
+ export const todoAdapter = createEntityAdapter <Todo >()
113
+
114
+ const todoInitialState = todoAdapter .getInitialState ()
115
+
116
+ export type TodoSliceState = typeof todoInitialState
117
+
118
+ const { addOne } = todoAdapter
119
+
120
+ createReducer (todoInitialState , (builder ) => {
121
+ builder .addCase (
122
+ todoAdded1a ,
123
+ (state : TodoSliceState , action : PayloadAction <string >) => {
124
+ // stuff
125
+ }
126
+ )
127
+
128
+ builder .addCase (
129
+ todoAdded1b ,
130
+ (state : TodoSliceState , action : PayloadAction <string >) => action .payload
131
+ )
132
+
133
+ builder .addCase (
134
+ todoAdded1c + ' test' ,
135
+ (state : TodoSliceState , action : PayloadAction <string >) => {
136
+ // stuff
137
+ }
138
+ )
139
+
140
+ builder .addCase (
141
+ todoAdded1d ,
142
+ (state : TodoSliceState , action : PayloadAction <string >) => {
143
+ // stuff
144
+ }
145
+ )
146
+
147
+ builder .addCase (
148
+ todoAdded1e ,
149
+ (state : TodoSliceState , action : PayloadAction <string >) => {
150
+ // stuff
151
+ }
152
+ )
153
+
154
+ builder .addCase (
155
+ todoAdded1f ,
156
+ (state : TodoSliceState , action : PayloadAction <string >) => {
157
+ // stuff
158
+ }
159
+ )
160
+
161
+ builder .addCase (todoAdded1g , addOne )
162
+ builder .addCase (todoAdded1h , todoAdapter .addOne )
163
+ })
164
+
165
+ createReducer (todoInitialState , (builder ) => {
166
+ builder .addCase (
167
+ todoAdded2a ,
168
+ (state : TodoSliceState , action : PayloadAction <string >) => {
169
+ // stuff
170
+ }
171
+ )
172
+
173
+ builder .addCase (
174
+ todoAdded2b ,
175
+ (state : TodoSliceState , action : PayloadAction <string >) => {
176
+ // stuff
177
+ }
178
+ )
179
+
180
+ builder .addCase (
181
+ todoAdded2c ,
182
+ (state : TodoSliceState , action : PayloadAction <string >) => {
183
+ // stuff
184
+ }
185
+ )
186
+ })
66
187
```
67
188
68
189
---
@@ -72,7 +193,15 @@ createReducer(initialState, (builder) => {
72
193
** Input** (<small >[ basic.input.js] ( transforms\createReducerBuilder__testfixtures__\basic.input.js ) </small >):
73
194
74
195
``` js
75
- createReducer (initialState, {
196
+ import { createEntityAdapter , createReducer } from ' @reduxjs/toolkit'
197
+
198
+ export const todoAdapter = createEntityAdapter ()
199
+
200
+ const todoInitialState = todoAdapter .getInitialState ()
201
+
202
+ const { addOne } = todoAdapter
203
+
204
+ createReducer (todoInitialState, {
76
205
[todoAdded1a]: (state , action ) => {
77
206
// stuff
78
207
},
@@ -89,9 +218,11 @@ createReducer(initialState, {
89
218
todoAdded1f : (state , action ) => {
90
219
// stuff
91
220
},
92
- });
221
+ [todoAdded1g]: addOne,
222
+ todoAdded1h: todoAdapter .addOne
223
+ })
93
224
94
- createReducer (initialState , {
225
+ createReducer (todoInitialState , {
95
226
[todoAdded2a]: (state , action ) => {
96
227
// stuff
97
228
},
@@ -100,50 +231,61 @@ createReducer(initialState, {
100
231
},
101
232
[todoAdded2c]: function (state , action ) {
102
233
// stuff
103
- },
104
- });
234
+ }
235
+ })
105
236
```
106
237
107
238
** Output** (<small >[ basic.output.js] ( transforms\createReducerBuilder__testfixtures__\basic.output.js ) </small >):
108
239
109
240
``` js
110
- createReducer (initialState, (builder ) => {
241
+ import { createEntityAdapter , createReducer } from ' @reduxjs/toolkit'
242
+
243
+ export const todoAdapter = createEntityAdapter ()
244
+
245
+ const todoInitialState = todoAdapter .getInitialState ()
246
+
247
+ const { addOne } = todoAdapter
248
+
249
+ createReducer (todoInitialState, (builder ) => {
111
250
builder .addCase (todoAdded1a, (state , action ) => {
112
251
// stuff
113
- });
252
+ })
114
253
115
- builder .addCase (todoAdded1b, (state , action ) => action .payload );
254
+ builder .addCase (todoAdded1b, (state , action ) => action .payload )
116
255
117
256
builder .addCase (todoAdded1c + ' test' , (state , action ) => {
118
257
// stuff
119
- });
258
+ })
120
259
121
260
builder .addCase (todoAdded1d, (state , action ) => {
122
261
// stuff
123
- });
262
+ })
124
263
125
264
builder .addCase (todoAdded1e, (state , action ) => {
126
265
// stuff
127
- });
266
+ })
128
267
129
268
builder .addCase (todoAdded1f, (state , action ) => {
130
269
// stuff
131
- });
132
- });
270
+ })
271
+
272
+ builder .addCase (todoAdded1g, addOne)
273
+ builder .addCase (todoAdded1h, todoAdapter .addOne )
274
+ })
133
275
134
- createReducer (initialState , (builder ) => {
276
+ createReducer (todoInitialState , (builder ) => {
135
277
builder .addCase (todoAdded2a, (state , action ) => {
136
278
// stuff
137
- });
279
+ })
138
280
139
281
builder .addCase (todoAdded2b, (state , action ) => {
140
282
// stuff
141
- });
283
+ })
142
284
143
285
builder .addCase (todoAdded2c, (state , action ) => {
144
286
// stuff
145
- });
146
- });
287
+ })
288
+ })
147
289
```
148
290
149
291
<!-- FIXTURES_CONTENT_END-->
0 commit comments