@@ -21,7 +21,22 @@ type CounterAction =
2121 | { type : 'DECREMENT' }
2222 | { type : 'DOUBLE' }
2323 | { 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 } ;
2540
2641function counterReducer ( state : CounterState , action : CounterAction , step : number ) : CounterState {
2742 switch ( action . type ) {
@@ -59,6 +74,54 @@ function counterReducer(state: CounterState, action: CounterAction, step: number
5974 history : [ ...state . history , state . count + action . payload ] ,
6075 lastAction : `ADD ${ action . payload } ` ,
6176 } ;
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+ } ;
62125 default :
63126 return state ;
64127 }
@@ -76,6 +139,7 @@ function FunctionalReducerCounter({
76139 const [ clickCount , setClickCount ] = useState ( 0 ) ;
77140 const [ lastClickTime , setLastClickTime ] = useState < Date | null > ( null ) ;
78141 const [ averageTimeBetweenClicks , setAverageTimeBetweenClicks ] = useState < number > ( 0 ) ;
142+
79143 const [ state , dispatch ] = useReducer (
80144 ( state : CounterState , action : CounterAction ) => counterReducer ( state , action , step ) ,
81145 {
@@ -85,6 +149,13 @@ function FunctionalReducerCounter({
85149 } ,
86150 ) ;
87151
152+ const [ secondaryState , secondaryDispatch ] = useReducer ( secondaryCounterReducer , {
153+ count : initialCount ,
154+ multiplier : 2 ,
155+ history : [ ] ,
156+ lastOperation : 'none' ,
157+ } ) ;
158+
88159 return (
89160 < div
90161 className = 'reducer-counter'
@@ -94,8 +165,9 @@ function FunctionalReducerCounter({
94165 } }
95166 >
96167 < h2 > { title } </ h2 >
168+
97169 < div className = 'counter-value' >
98- < h3 > Current Count : { state . count } </ h3 >
170+ < h3 > Primary Counter : { state . count } </ h3 >
99171 </ div >
100172
101173 < div className = 'counter-buttons' >
@@ -107,7 +179,6 @@ function FunctionalReducerCounter({
107179 </ div >
108180
109181 < div className = 'counter-info' >
110- < h4 > Last Action: { state . lastAction } </ h4 >
111182 < h4 > History:</ h4 >
112183 < div className = 'history-list' >
113184 { state . history . map ( ( value , index ) => (
@@ -118,7 +189,43 @@ function FunctionalReducerCounter({
118189 ) ) }
119190 </ div >
120191 </ 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 >
121227 </ div >
122228 ) ;
123229}
230+
124231export default FunctionalReducerCounter ;
0 commit comments