@@ -23,6 +23,19 @@ type CounterAction =
2323 | { type : 'RESET' }
2424 | { type : 'ADD' ; payload : number } ;
2525
26+ // New secondary reducer state and action types
27+ type SecondaryCounterState = {
28+ count : number ;
29+ multiplier : number ;
30+ lastOperation : string ;
31+ } ;
32+
33+ type SecondaryCounterAction =
34+ | { type : 'MULTIPLY' }
35+ | { type : 'DIVIDE' }
36+ | { type : 'SET_MULTIPLIER' ; payload : number }
37+ | { type : 'RESET' } ;
38+
2639function counterReducer ( state : CounterState , action : CounterAction , step : number ) : CounterState {
2740 switch ( action . type ) {
2841 case 'INCREMENT' :
@@ -64,6 +77,41 @@ function counterReducer(state: CounterState, action: CounterAction, step: number
6477 }
6578}
6679
80+ // New secondary reducer function
81+ function secondaryCounterReducer (
82+ state : SecondaryCounterState ,
83+ action : SecondaryCounterAction ,
84+ ) : SecondaryCounterState {
85+ switch ( action . type ) {
86+ case 'MULTIPLY' :
87+ return {
88+ ...state ,
89+ count : state . count * state . multiplier ,
90+ lastOperation : `Multiplied by ${ state . multiplier } ` ,
91+ } ;
92+ case 'DIVIDE' :
93+ return {
94+ ...state ,
95+ count : state . count / state . multiplier ,
96+ lastOperation : `Divided by ${ state . multiplier } ` ,
97+ } ;
98+ case 'SET_MULTIPLIER' :
99+ return {
100+ ...state ,
101+ multiplier : action . payload ,
102+ lastOperation : `Set multiplier to ${ action . payload } ` ,
103+ } ;
104+ case 'RESET' :
105+ return {
106+ count : 0 ,
107+ multiplier : 2 ,
108+ lastOperation : 'Reset' ,
109+ } ;
110+ default :
111+ return state ;
112+ }
113+ }
114+
67115function FunctionalReducerCounter ( {
68116 initialCount = 0 ,
69117 step = 1 ,
@@ -76,6 +124,8 @@ function FunctionalReducerCounter({
76124 const [ clickCount , setClickCount ] = useState ( 0 ) ;
77125 const [ lastClickTime , setLastClickTime ] = useState < Date | null > ( null ) ;
78126 const [ averageTimeBetweenClicks , setAverageTimeBetweenClicks ] = useState < number > ( 0 ) ;
127+
128+ // First reducer
79129 const [ state , dispatch ] = useReducer (
80130 ( state : CounterState , action : CounterAction ) => counterReducer ( state , action , step ) ,
81131 {
@@ -85,6 +135,13 @@ function FunctionalReducerCounter({
85135 } ,
86136 ) ;
87137
138+ // Second reducer
139+ const [ secondaryState , secondaryDispatch ] = useReducer ( secondaryCounterReducer , {
140+ count : initialCount ,
141+ multiplier : 2 ,
142+ lastOperation : 'none' ,
143+ } ) ;
144+
88145 return (
89146 < div
90147 className = 'reducer-counter'
@@ -94,8 +151,10 @@ function FunctionalReducerCounter({
94151 } }
95152 >
96153 < h2 > { title } </ h2 >
154+
155+ { /* Primary Counter Section */ }
97156 < div className = 'counter-value' >
98- < h3 > Current Count : { state . count } </ h3 >
157+ < h3 > Primary Counter : { state . count } </ h3 >
99158 </ div >
100159
101160 < div className = 'counter-buttons' >
@@ -118,7 +177,36 @@ function FunctionalReducerCounter({
118177 ) ) }
119178 </ div >
120179 </ div >
180+
181+ { /* Secondary Counter Section */ }
182+ < div
183+ className = 'secondary-counter'
184+ style = { { marginTop : '2rem' , borderTop : '1px solid #ccc' , paddingTop : '1rem' } }
185+ >
186+ < h3 > Secondary Counter: { secondaryState . count } </ h3 >
187+ < div className = 'counter-buttons' >
188+ < button onClick = { ( ) => secondaryDispatch ( { type : 'MULTIPLY' } ) } >
189+ Multiply by { secondaryState . multiplier }
190+ </ button >
191+ < button onClick = { ( ) => secondaryDispatch ( { type : 'DIVIDE' } ) } >
192+ Divide by { secondaryState . multiplier }
193+ </ button >
194+ < button
195+ onClick = { ( ) =>
196+ secondaryDispatch ( { type : 'SET_MULTIPLIER' , payload : secondaryState . multiplier + 1 } )
197+ }
198+ >
199+ Increase Multiplier
200+ </ button >
201+ < button onClick = { ( ) => secondaryDispatch ( { type : 'RESET' } ) } > Reset</ button >
202+ </ div >
203+ < div className = 'counter-info' >
204+ < h4 > Last Operation: { secondaryState . lastOperation } </ h4 >
205+ < h4 > Current Multiplier: { secondaryState . multiplier } </ h4 >
206+ </ div >
207+ </ div >
121208 </ div >
122209 ) ;
123210}
211+
124212export default FunctionalReducerCounter ;
0 commit comments