@@ -21,7 +21,22 @@ type CounterAction =
21
21
| { type : 'DECREMENT' }
22
22
| { type : 'DOUBLE' }
23
23
| { type : 'RESET' }
24
- | { type : 'ADD' ; payload : number } ;
24
+ | { type : 'ADD' ; payload : number }
25
+ | { type : 'SET_STATE' ; payload : CounterState } ;
26
+
27
+ type SecondaryCounterState = {
28
+ count : number ;
29
+ multiplier : number ;
30
+ lastOperation : string ;
31
+ history : number [ ] ;
32
+ } ;
33
+
34
+ type SecondaryCounterAction =
35
+ | { type : 'MULTIPLY' }
36
+ | { type : 'DIVIDE' }
37
+ | { type : 'SET_MULTIPLIER' ; payload : number }
38
+ | { type : 'RESET' }
39
+ | { type : 'SET_STATE' ; payload : SecondaryCounterState } ;
25
40
26
41
function counterReducer ( state : CounterState , action : CounterAction , step : number ) : CounterState {
27
42
switch ( action . type ) {
@@ -59,6 +74,54 @@ function counterReducer(state: CounterState, action: CounterAction, step: number
59
74
history : [ ...state . history , state . count + action . payload ] ,
60
75
lastAction : `ADD ${ action . payload } ` ,
61
76
} ;
77
+ case 'SET_STATE' :
78
+ return {
79
+ ...action . payload ,
80
+ lastAction : 'SET_STATE' ,
81
+ } ;
82
+ default :
83
+ return state ;
84
+ }
85
+ }
86
+
87
+ function secondaryCounterReducer (
88
+ state : SecondaryCounterState ,
89
+ action : SecondaryCounterAction ,
90
+ ) : SecondaryCounterState {
91
+ switch ( action . type ) {
92
+ case 'MULTIPLY' :
93
+ return {
94
+ ...state ,
95
+ count : state . count * state . multiplier ,
96
+ history : [ ...state . history , state . count * state . multiplier ] ,
97
+ lastOperation : `Multiplied by ${ state . multiplier } ` ,
98
+ } ;
99
+ case 'DIVIDE' :
100
+ return {
101
+ ...state ,
102
+ count : state . count / state . multiplier ,
103
+ history : [ ...state . history , state . count / state . multiplier ] ,
104
+ lastOperation : `Divided by ${ state . multiplier } ` ,
105
+ } ;
106
+ case 'SET_MULTIPLIER' :
107
+ return {
108
+ ...state ,
109
+ multiplier : action . payload ,
110
+ history : [ ...state . history ] ,
111
+ lastOperation : `Set multiplier to ${ action . payload } ` ,
112
+ } ;
113
+ case 'RESET' :
114
+ return {
115
+ count : 0 ,
116
+ multiplier : 2 ,
117
+ history : [ ] ,
118
+ lastOperation : 'Reset' ,
119
+ } ;
120
+ case 'SET_STATE' :
121
+ return {
122
+ ...action . payload ,
123
+ lastOperation : 'SET_STATE' ,
124
+ } ;
62
125
default :
63
126
return state ;
64
127
}
@@ -76,6 +139,7 @@ function FunctionalReducerCounter({
76
139
const [ clickCount , setClickCount ] = useState ( 0 ) ;
77
140
const [ lastClickTime , setLastClickTime ] = useState < Date | null > ( null ) ;
78
141
const [ averageTimeBetweenClicks , setAverageTimeBetweenClicks ] = useState < number > ( 0 ) ;
142
+
79
143
const [ state , dispatch ] = useReducer (
80
144
( state : CounterState , action : CounterAction ) => counterReducer ( state , action , step ) ,
81
145
{
@@ -85,6 +149,13 @@ function FunctionalReducerCounter({
85
149
} ,
86
150
) ;
87
151
152
+ const [ secondaryState , secondaryDispatch ] = useReducer ( secondaryCounterReducer , {
153
+ count : initialCount ,
154
+ multiplier : 2 ,
155
+ history : [ ] ,
156
+ lastOperation : 'none' ,
157
+ } ) ;
158
+
88
159
return (
89
160
< div
90
161
className = 'reducer-counter'
@@ -94,8 +165,9 @@ function FunctionalReducerCounter({
94
165
} }
95
166
>
96
167
< h2 > { title } </ h2 >
168
+
97
169
< div className = 'counter-value' >
98
- < h3 > Current Count : { state . count } </ h3 >
170
+ < h3 > Primary Counter : { state . count } </ h3 >
99
171
</ div >
100
172
101
173
< div className = 'counter-buttons' >
@@ -107,7 +179,6 @@ function FunctionalReducerCounter({
107
179
</ div >
108
180
109
181
< div className = 'counter-info' >
110
- < h4 > Last Action: { state . lastAction } </ h4 >
111
182
< h4 > History:</ h4 >
112
183
< div className = 'history-list' >
113
184
{ state . history . map ( ( value , index ) => (
@@ -118,7 +189,43 @@ function FunctionalReducerCounter({
118
189
) ) }
119
190
</ div >
120
191
</ div >
192
+
193
+ < div
194
+ className = 'secondary-counter'
195
+ style = { { marginTop : '2rem' , borderTop : '1px solid #ccc' , paddingTop : '1rem' } }
196
+ >
197
+ < h3 > Secondary Counter: { secondaryState . count } </ h3 >
198
+ < div className = 'counter-buttons' >
199
+ < button onClick = { ( ) => secondaryDispatch ( { type : 'MULTIPLY' } ) } >
200
+ Multiply by { secondaryState . multiplier }
201
+ </ button >
202
+ < button onClick = { ( ) => secondaryDispatch ( { type : 'DIVIDE' } ) } >
203
+ Divide by { secondaryState . multiplier }
204
+ </ button >
205
+ < button
206
+ onClick = { ( ) =>
207
+ secondaryDispatch ( { type : 'SET_MULTIPLIER' , payload : secondaryState . multiplier + 1 } )
208
+ }
209
+ >
210
+ Increase Multiplier
211
+ </ button >
212
+ < button onClick = { ( ) => secondaryDispatch ( { type : 'RESET' } ) } > Reset</ button >
213
+ </ div >
214
+ < div className = 'counter-info' >
215
+ < h4 > Current Multiplier: { secondaryState . multiplier } </ h4 >
216
+ < h4 > History:</ h4 >
217
+ < div className = 'history-list' >
218
+ { secondaryState . history . map ( ( value , index ) => (
219
+ < span key = { index } >
220
+ { value }
221
+ { index < secondaryState . history . length - 1 ? ' → ' : '' }
222
+ </ span >
223
+ ) ) }
224
+ </ div >
225
+ </ div >
226
+ </ div >
121
227
</ div >
122
228
) ;
123
229
}
230
+
124
231
export default FunctionalReducerCounter ;
0 commit comments