Skip to content

Commit df03eb4

Browse files
committed
Fixes and updates
1 parent 5688f05 commit df03eb4

File tree

3 files changed

+128
-82
lines changed

3 files changed

+128
-82
lines changed

components/vote/app-style.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
& > .vote-app__vote-score {
4646
flex: 1;
4747
width: auto;
48+
font-size: 150%;
4849
}
4950
}
5051

components/vote/app.jsx

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ export default class VoteApp extends React.Component {
5252
}
5353
}
5454

55+
componentWillReceiveProps(props) {
56+
if(!this.isBrowserSupported())
57+
return;
58+
59+
this.updateList(props);
60+
}
61+
5562
updateSelf() {
5663
let { voteAppToken } = localStorage;
5764
if(voteAppToken) {
@@ -72,8 +79,8 @@ export default class VoteApp extends React.Component {
7279
}
7380
}
7481

75-
updateList() {
76-
let { name } = this.props;
82+
updateList(props = this.props) {
83+
let { name } = props;
7784
let { voteAppToken } = localStorage;
7885
this.setState({
7986
isFetchingList: true
@@ -91,9 +98,8 @@ export default class VoteApp extends React.Component {
9198
});
9299
}
93100

94-
vote(itemId, voteName, value, diffValue, currencyName, score) {
101+
localVote(itemId, voteName, diffValue, currencyName, score) {
95102
let { selfInfo, listInfo } = this.state;
96-
let { voteAppToken } = localStorage;
97103
this.setState({
98104
isVoting: this.state.isVoting + 1,
99105
listInfo: listInfo && {
@@ -106,7 +112,7 @@ export default class VoteApp extends React.Component {
106112
})),
107113
userVotes: updateByProperty(item.userVotes, "name", voteName, vote => ({
108114
...vote,
109-
votes: value
115+
votes: vote.votes + diffValue
110116
})),
111117
score: item.score + score
112118
}))
@@ -120,7 +126,20 @@ export default class VoteApp extends React.Component {
120126
}))
121127
}
122128
});
123-
api.vote(voteAppToken, itemId, voteName, value).then(() => {
129+
}
130+
131+
vote(itemId, voteName, diffValue, currencyName, score) {
132+
if(!diffValue) return;
133+
this.localVote(itemId, voteName, diffValue, currencyName, score);
134+
let { voteAppToken } = localStorage;
135+
api.vote(voteAppToken, itemId, voteName, diffValue).catch(e => {
136+
console.error(e);
137+
// revert local vote
138+
this.localVote(itemId, voteName, -diffValue, currencyName, score);
139+
this.setState({
140+
isVoting: this.state.isVoting - 1
141+
});
142+
}).then(() => {
124143
this.setState({
125144
isVoting: this.state.isVoting - 1
126145
});
@@ -143,6 +162,18 @@ export default class VoteApp extends React.Component {
143162

144163
const inProgress = isFetchingList || isFetchingSelf || isCreating || isVoting;
145164

165+
let maxVoteInfo = listInfo && listInfo.possibleVotes.map(() => 0);
166+
167+
if(listInfo) listInfo.items.forEach(item => {
168+
if(item.userVotes) {
169+
maxVoteInfo.forEach((max, idx) => {
170+
let votes = item.userVotes[idx].votes;
171+
if(votes > max)
172+
maxVoteInfo[idx] = votes;
173+
});
174+
}
175+
});
176+
146177
return (
147178
<div className="vote-app">
148179
{this.renderSelf()}
@@ -154,34 +185,36 @@ export default class VoteApp extends React.Component {
154185
<h1>{listInfo.displayName}</h1>
155186
<div>{listInfo.description}</div>
156187
<ul className="vote-app__items-list">
157-
{ listInfo.items.map(item => <li>
188+
{ listInfo.items.map(item => <li key={item.id}>
158189
<span className="vote-app__item-title">{item.title}</span>
159190
<span>{item.description}</span><br />
160191
<ul className="vote-app__vote-list">
161192
{listInfo.possibleVotes.map((voteSettings, idx) => {
162193
let vote = item.votes[idx];
163194
let userVote = item.userVotes && item.userVotes[idx];
164195
let currencyInfo = selfInfo && voteSettings.currency && this.findByName(selfInfo.currencies, voteSettings.currency);
165-
let maximum = voteSettings.maximum || 100; // infinity
196+
let maximum = voteSettings.maximum || 1000; // infinity
166197
let minimum = voteSettings.minimum || 0;
167-
if(currencyInfo && currencyInfo.remaining < maximum) maximum = currencyInfo.remaining;
168-
let startValue = (userVote && userVote.votes) ? userVote.votes: 0;
198+
let value = (userVote && userVote.votes) ? userVote.votes: 0;
199+
if(currencyInfo && currencyInfo.remaining + value < maximum) maximum = currencyInfo.remaining + value;
200+
let visibleMaxValue = voteSettings.maximum || (maxVoteInfo[idx] + currencyInfo.remaining);
169201

170202
return <li className={"vote-app__vote-" + voteSettings.name} key={voteSettings.name} title={userVote ? "You voted " + userVote.votes + "." : "Login to see your votes."}>
171203
<div className="vote-app__vote-value">
172204
{vote.votes > 0 && voteSettings.minimum < 0 ? "+" + vote.votes : vote.votes}
173205
{userVote && userVote.votes ? " (You: " + (userVote.votes > 0 && voteSettings.minimum < 0 ? "+" + userVote.votes : userVote.votes) + ")" : ""}
174206
</div>
175207
{ selfInfo &&
176-
<VoteSlider minValue={minimum} maxValue={maximum} startValue={startValue} step={voteSettings.step} color={voteSettings.color}
208+
<VoteSlider minValue={minimum} maxValue={maximum} visibleMaxValue={visibleMaxValue}
209+
value={value} step={this.getStep(visibleMaxValue)} color={this.getColor(voteSettings.name)}
177210
valueChanged={(v) => {
178211
let diff = v;
179212

180213
if((userVote && userVote.votes)) {
181214
diff = v - userVote.votes;
182215
}
183216

184-
this.vote(item.id, voteSettings.name, v, diff, voteSettings.currency, voteSettings.score * diff);
217+
this.vote(item.id, voteSettings.name, diff, voteSettings.currency, voteSettings.score * diff);
185218
}}
186219
/>
187220
}
@@ -265,4 +298,17 @@ export default class VoteApp extends React.Component {
265298
arr.push(maximum);
266299
return arr;
267300
}
301+
302+
getStep(maximum) {
303+
return Math.floor(maximum / 20) * 2 || 1;
304+
}
305+
306+
getColor(name) {
307+
switch(name) {
308+
case "influence": return "blue";
309+
case "golden": return "#bfa203";
310+
case "thumb": return "#535353";
311+
}
312+
return undefined;
313+
}
268314
}

0 commit comments

Comments
 (0)