Skip to content

Commit 84c51e7

Browse files
committed
improved vote reset
1 parent 33262ce commit 84c51e7

File tree

3 files changed

+30
-13
lines changed

3 files changed

+30
-13
lines changed

‎src/components/Voting.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export const Voting = React.createClass({
1818
function mapStateToProps(state) {
1919
return {
2020
pair: state.getIn(['vote', 'pair']),
21-
hasVoted: state.get('hasVoted'),
21+
hasVoted: state.getIn(['myVote', 'entry']),
2222
winner: state.get('winner')
2323
};
2424
}

‎src/reducer.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,23 @@ function setState(state, newState) {
55
}
66

77
function vote(state, entry) {
8+
const currentRound = state.getIn(['vote', 'round']);
89
const currentPair = state.getIn(['vote', 'pair']);
910
if (currentPair && currentPair.includes(entry)) {
10-
return state.set('hasVoted', entry);
11+
return state.set('myVote', Map({
12+
round: currentRound,
13+
entry
14+
}));
1115
} else {
1216
return state;
1317
}
1418
}
1519

1620
function resetVote(state) {
17-
const hasVoted = state.get('hasVoted');
18-
const currentPair = state.getIn(['vote', 'pair'], List());
19-
if (hasVoted && !currentPair.includes(hasVoted)) {
20-
return state.remove('hasVoted');
21+
const votedForRound = state.getIn(['myVote', 'round']);
22+
const currentRound = state.getIn(['vote', 'round']);
23+
if (votedForRound !== currentRound) {
24+
return state.remove('myVote');
2125
} else {
2226
return state;
2327
}

‎test/reducer_spec.js

+20-7
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,10 @@ describe('reducer', () => {
6767
}));
6868
});
6969

70-
it('handles VOTE by setting hasVoted', () => {
70+
it('handles VOTE by setting myVote', () => {
7171
const state = fromJS({
7272
vote: {
73+
round: 42,
7374
pair: ['Trainspotting', '28 Days Later'],
7475
tally: {Trainspotting: 1}
7576
}
@@ -79,16 +80,21 @@ describe('reducer', () => {
7980

8081
expect(nextState).to.equal(fromJS({
8182
vote: {
83+
round: 42,
8284
pair: ['Trainspotting', '28 Days Later'],
8385
tally: {Trainspotting: 1}
8486
},
85-
hasVoted: 'Trainspotting'
87+
myVote: {
88+
round: 42,
89+
entry: 'Trainspotting'
90+
}
8691
}));
8792
});
8893

89-
it('does not set hasVoted for VOTE on invalid entry', () => {
94+
it('does not set myVote for VOTE on invalid entry', () => {
9095
const state = fromJS({
9196
vote: {
97+
round: 42,
9298
pair: ['Trainspotting', '28 Days Later'],
9399
tally: {Trainspotting: 1}
94100
}
@@ -98,33 +104,40 @@ describe('reducer', () => {
98104

99105
expect(nextState).to.equal(fromJS({
100106
vote: {
107+
round: 42,
101108
pair: ['Trainspotting', '28 Days Later'],
102109
tally: {Trainspotting: 1}
103110
}
104111
}));
105112
});
106113

107-
it('removes hasVoted on SET_STATE if pair changes', () => {
114+
it('removes myVote on SET_STATE if round has changed', () => {
108115
const initialState = fromJS({
109116
vote: {
117+
round: 42,
110118
pair: ['Trainspotting', '28 Days Later'],
111119
tally: {Trainspotting: 1}
112120
},
113-
hasVoted: 'Trainspotting'
121+
myVote: {
122+
round: 42,
123+
entry: 'Trainspotting'
124+
}
114125
});
115126
const action = {
116127
type: 'SET_STATE',
117128
state: {
118129
vote: {
119-
pair: ['Sunshine', 'Slumdog Millionaire']
130+
round: 43,
131+
pair: ['Sunshine', 'Trainspotting']
120132
}
121133
}
122134
};
123135
const nextState = reducer(initialState, action);
124136

125137
expect(nextState).to.equal(fromJS({
126138
vote: {
127-
pair: ['Sunshine', 'Slumdog Millionaire']
139+
round: 43,
140+
pair: ['Sunshine', 'Trainspotting']
128141
}
129142
}));
130143
});

0 commit comments

Comments
 (0)