@@ -21,20 +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 } ;
2526
26- // New secondary reducer state and action types
2727type SecondaryCounterState = {
2828 count : number ;
2929 multiplier : number ;
3030 lastOperation : string ;
31+ history : number [ ] ;
3132} ;
3233
3334type SecondaryCounterAction =
3435 | { type : 'MULTIPLY' }
3536 | { type : 'DIVIDE' }
3637 | { type : 'SET_MULTIPLIER' ; payload : number }
37- | { type : 'RESET' } ;
38+ | { type : 'RESET' }
39+ | { type : 'SET_STATE' ; payload : SecondaryCounterState } ;
3840
3941function counterReducer ( state : CounterState , action : CounterAction , step : number ) : CounterState {
4042 switch ( action . type ) {
@@ -72,12 +74,16 @@ function counterReducer(state: CounterState, action: CounterAction, step: number
7274 history : [ ...state . history , state . count + action . payload ] ,
7375 lastAction : `ADD ${ action . payload } ` ,
7476 } ;
77+ case 'SET_STATE' :
78+ return {
79+ ...action . payload ,
80+ lastAction : 'SET_STATE' ,
81+ } ;
7582 default :
7683 return state ;
7784 }
7885}
7986
80- // New secondary reducer function
8187function secondaryCounterReducer (
8288 state : SecondaryCounterState ,
8389 action : SecondaryCounterAction ,
@@ -87,26 +93,35 @@ function secondaryCounterReducer(
8793 return {
8894 ...state ,
8995 count : state . count * state . multiplier ,
96+ history : [ ...state . history , state . count * state . multiplier ] ,
9097 lastOperation : `Multiplied by ${ state . multiplier } ` ,
9198 } ;
9299 case 'DIVIDE' :
93100 return {
94101 ...state ,
95102 count : state . count / state . multiplier ,
103+ history : [ ...state . history , state . count / state . multiplier ] ,
96104 lastOperation : `Divided by ${ state . multiplier } ` ,
97105 } ;
98106 case 'SET_MULTIPLIER' :
99107 return {
100108 ...state ,
101109 multiplier : action . payload ,
110+ history : [ ...state . history ] ,
102111 lastOperation : `Set multiplier to ${ action . payload } ` ,
103112 } ;
104113 case 'RESET' :
105114 return {
106115 count : 0 ,
107116 multiplier : 2 ,
117+ history : [ ] ,
108118 lastOperation : 'Reset' ,
109119 } ;
120+ case 'SET_STATE' :
121+ return {
122+ ...action . payload ,
123+ lastOperation : 'SET_STATE' ,
124+ } ;
110125 default :
111126 return state ;
112127 }
@@ -125,7 +140,6 @@ function FunctionalReducerCounter({
125140 const [ lastClickTime , setLastClickTime ] = useState < Date | null > ( null ) ;
126141 const [ averageTimeBetweenClicks , setAverageTimeBetweenClicks ] = useState < number > ( 0 ) ;
127142
128- // First reducer
129143 const [ state , dispatch ] = useReducer (
130144 ( state : CounterState , action : CounterAction ) => counterReducer ( state , action , step ) ,
131145 {
@@ -135,10 +149,10 @@ function FunctionalReducerCounter({
135149 } ,
136150 ) ;
137151
138- // Second reducer
139152 const [ secondaryState , secondaryDispatch ] = useReducer ( secondaryCounterReducer , {
140153 count : initialCount ,
141154 multiplier : 2 ,
155+ history : [ ] ,
142156 lastOperation : 'none' ,
143157 } ) ;
144158
@@ -152,7 +166,6 @@ function FunctionalReducerCounter({
152166 >
153167 < h2 > { title } </ h2 >
154168
155- { /* Primary Counter Section */ }
156169 < div className = 'counter-value' >
157170 < h3 > Primary Counter: { state . count } </ h3 >
158171 </ div >
@@ -166,7 +179,6 @@ function FunctionalReducerCounter({
166179 </ div >
167180
168181 < div className = 'counter-info' >
169- < h4 > Last Action: { state . lastAction } </ h4 >
170182 < h4 > History:</ h4 >
171183 < div className = 'history-list' >
172184 { state . history . map ( ( value , index ) => (
@@ -178,7 +190,6 @@ function FunctionalReducerCounter({
178190 </ div >
179191 </ div >
180192
181- { /* Secondary Counter Section */ }
182193 < div
183194 className = 'secondary-counter'
184195 style = { { marginTop : '2rem' , borderTop : '1px solid #ccc' , paddingTop : '1rem' } }
@@ -201,8 +212,16 @@ function FunctionalReducerCounter({
201212 < button onClick = { ( ) => secondaryDispatch ( { type : 'RESET' } ) } > Reset</ button >
202213 </ div >
203214 < div className = 'counter-info' >
204- < h4 > Last Operation: { secondaryState . lastOperation } </ h4 >
205215 < 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 >
206225 </ div >
207226 </ div >
208227 </ div >
0 commit comments