@@ -52,6 +52,13 @@ export default class VoteApp extends React.Component {
52
52
}
53
53
}
54
54
55
+ componentWillReceiveProps ( props ) {
56
+ if ( ! this . isBrowserSupported ( ) )
57
+ return ;
58
+
59
+ this . updateList ( props ) ;
60
+ }
61
+
55
62
updateSelf ( ) {
56
63
let { voteAppToken } = localStorage ;
57
64
if ( voteAppToken ) {
@@ -72,8 +79,8 @@ export default class VoteApp extends React.Component {
72
79
}
73
80
}
74
81
75
- updateList ( ) {
76
- let { name } = this . props ;
82
+ updateList ( props = this . props ) {
83
+ let { name } = props ;
77
84
let { voteAppToken } = localStorage ;
78
85
this . setState ( {
79
86
isFetchingList : true
@@ -91,9 +98,8 @@ export default class VoteApp extends React.Component {
91
98
} ) ;
92
99
}
93
100
94
- vote ( itemId , voteName , value , diffValue , currencyName , score ) {
101
+ localVote ( itemId , voteName , diffValue , currencyName , score ) {
95
102
let { selfInfo, listInfo } = this . state ;
96
- let { voteAppToken } = localStorage ;
97
103
this . setState ( {
98
104
isVoting : this . state . isVoting + 1 ,
99
105
listInfo : listInfo && {
@@ -106,7 +112,7 @@ export default class VoteApp extends React.Component {
106
112
} ) ) ,
107
113
userVotes : updateByProperty ( item . userVotes , "name" , voteName , vote => ( {
108
114
...vote ,
109
- votes : value
115
+ votes : vote . votes + diffValue
110
116
} ) ) ,
111
117
score : item . score + score
112
118
} ) )
@@ -120,7 +126,20 @@ export default class VoteApp extends React.Component {
120
126
} ) )
121
127
}
122
128
} ) ;
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 ( ( ) => {
124
143
this . setState ( {
125
144
isVoting : this . state . isVoting - 1
126
145
} ) ;
@@ -143,6 +162,18 @@ export default class VoteApp extends React.Component {
143
162
144
163
const inProgress = isFetchingList || isFetchingSelf || isCreating || isVoting ;
145
164
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
+
146
177
return (
147
178
< div className = "vote-app" >
148
179
{ this . renderSelf ( ) }
@@ -154,34 +185,36 @@ export default class VoteApp extends React.Component {
154
185
< h1 > { listInfo . displayName } </ h1 >
155
186
< div > { listInfo . description } </ div >
156
187
< ul className = "vote-app__items-list" >
157
- { listInfo . items . map ( item => < li >
188
+ { listInfo . items . map ( item => < li key = { item . id } >
158
189
< span className = "vote-app__item-title" > { item . title } </ span >
159
190
< span > { item . description } </ span > < br />
160
191
< ul className = "vote-app__vote-list" >
161
192
{ listInfo . possibleVotes . map ( ( voteSettings , idx ) => {
162
193
let vote = item . votes [ idx ] ;
163
194
let userVote = item . userVotes && item . userVotes [ idx ] ;
164
195
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
166
197
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 ) ;
169
201
170
202
return < li className = { "vote-app__vote-" + voteSettings . name } key = { voteSettings . name } title = { userVote ? "You voted " + userVote . votes + "." : "Login to see your votes." } >
171
203
< div className = "vote-app__vote-value" >
172
204
{ vote . votes > 0 && voteSettings . minimum < 0 ? "+" + vote . votes : vote . votes }
173
205
{ userVote && userVote . votes ? " (You: " + ( userVote . votes > 0 && voteSettings . minimum < 0 ? "+" + userVote . votes : userVote . votes ) + ")" : "" }
174
206
</ div >
175
207
{ 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 ) }
177
210
valueChanged = { ( v ) => {
178
211
let diff = v ;
179
212
180
213
if ( ( userVote && userVote . votes ) ) {
181
214
diff = v - userVote . votes ;
182
215
}
183
216
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 ) ;
185
218
} }
186
219
/>
187
220
}
@@ -265,4 +298,17 @@ export default class VoteApp extends React.Component {
265
298
arr . push ( maximum ) ;
266
299
return arr ;
267
300
}
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
+ }
268
314
}
0 commit comments