1- import { createReducer } from "@reduxjs/toolkit" ;
2- import { VotingsState } from "./types" ;
3- import { initializeBoard } from "../board" ;
4- import { createdVoting , updatedVoting } from "./actions" ;
1+ import { createReducer } from "@reduxjs/toolkit" ;
2+ import { VotingsState } from "./types" ;
3+ import { initializeBoard } from "../board" ;
4+ import { createdVoting , updatedVoting } from "./actions" ;
55
6- const initialState : VotingsState = { open : undefined , past : [ ] } ;
6+ const initialState : VotingsState = { open : undefined , past : [ ] } ;
77
88export const votingsReducer = createReducer ( initialState , ( builder ) =>
99 builder
@@ -17,15 +17,17 @@ export const votingsReducer = createReducer(initialState, (builder) =>
1717 }
1818 return acc ;
1919 } ,
20- { open : undefined , past : [ ] }
20+ { open : undefined , past : [ ] }
2121 )
2222 )
2323 . addCase ( createdVoting , ( state , action ) => {
2424 state . open = action . payload ;
25- state . past = [ ] ;
2625 } )
2726 . addCase ( updatedVoting , ( state , action ) => {
2827 state . open = undefined ;
29- state . past . push ( action . payload . voting ) ;
28+ const incoming = action . payload . voting ;
29+ const lastKnown = state . past [ 0 ] ;
30+ const votingToPush = incoming . votes ? incoming : { ...incoming , votes : lastKnown ?. votes } ;
31+ state . past . unshift ( votingToPush ) ;
3032 } )
3133) ;
0 commit comments