Skip to content

Commit b07c1ce

Browse files
Matin Gohar FarMatin Gohar Far
authored andcommitted
fixed bug: last voting results deleted
1 parent 5c5c96e commit b07c1ce

1 file changed

Lines changed: 10 additions & 8 deletions

File tree

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
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

88
export 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

Comments
 (0)