@@ -2,7 +2,7 @@ import React from 'react';
2
2
import SidebarItem from '../sidebar-item/sidebar-item' ;
3
3
import * as api from "./api" ;
4
4
import './app-style' ;
5
- import VoteSlider from './slider/slider ' ;
5
+ import VoteButton from './button/button ' ;
6
6
7
7
function updateByProperty ( array , property , propertyValue , update ) {
8
8
return array . map ( item => {
@@ -65,14 +65,14 @@ export default class VoteApp extends React.Component {
65
65
this . setState ( {
66
66
isFetchingSelf : true
67
67
} ) ;
68
- api . getSelf ( voteAppToken ) . then ( result => {
68
+ api . getSelf ( voteAppToken ) . catch ( e => {
69
69
this . setState ( {
70
- selfInfo : result ,
70
+ selfInfo : null ,
71
71
isFetchingSelf : false
72
72
} ) ;
73
- } ) . catch ( e => {
73
+ } ) . then ( result => {
74
74
this . setState ( {
75
- selfInfo : null ,
75
+ selfInfo : result ,
76
76
isFetchingSelf : false
77
77
} ) ;
78
78
} ) ;
@@ -85,14 +85,14 @@ export default class VoteApp extends React.Component {
85
85
this . setState ( {
86
86
isFetchingList : true
87
87
} ) ;
88
- api . getList ( voteAppToken , name ) . then ( result => {
88
+ api . getList ( voteAppToken , name ) . catch ( e => {
89
89
this . setState ( {
90
- listInfo : result ,
90
+ listInfo : null ,
91
91
isFetchingList : false
92
92
} ) ;
93
- } ) . catch ( e => {
93
+ } ) . then ( result => {
94
94
this . setState ( {
95
- listInfo : null ,
95
+ listInfo : result ,
96
96
isFetchingList : false
97
97
} ) ;
98
98
} ) ;
@@ -186,44 +186,37 @@ export default class VoteApp extends React.Component {
186
186
< div > { listInfo . description } </ div >
187
187
< ul className = "vote-app__items-list" >
188
188
{ listInfo . items . map ( item => < li key = { item . id } >
189
- < span className = "vote-app__item-title" > { item . title } </ span >
190
- < span > { item . description } </ span > < br />
191
- < ul className = "vote-app__vote-list" >
192
- { listInfo . possibleVotes . map ( ( voteSettings , idx ) => {
193
- let vote = item . votes [ idx ] ;
194
- let userVote = item . userVotes && item . userVotes [ idx ] ;
195
- let currencyInfo = selfInfo && voteSettings . currency && this . findByName ( selfInfo . currencies , voteSettings . currency ) ;
196
- let maximum = voteSettings . maximum || 1000 ; // infinity
197
- let minimum = voteSettings . minimum || 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 ) ;
201
-
202
- return < li className = { "vote-app__vote-" + voteSettings . name } key = { voteSettings . name } title = { userVote ? "You voted " + userVote . votes + "." : "Login to see your votes." } >
203
- < div className = "vote-app__vote-value" >
204
- { vote . votes > 0 && voteSettings . minimum < 0 ? "+" + vote . votes : vote . votes }
205
- { userVote && userVote . votes ? " (You: " + ( userVote . votes > 0 && voteSettings . minimum < 0 ? "+" + userVote . votes : userVote . votes ) + ")" : "" }
206
- </ div >
207
- { selfInfo &&
208
- < VoteSlider minValue = { minimum } maxValue = { maximum } visibleMaxValue = { visibleMaxValue }
209
- value = { value } step = { this . getStep ( visibleMaxValue ) } color = { this . getColor ( voteSettings . name ) }
210
- valueChanged = { ( v ) => {
211
- let diff = v ;
212
-
213
- if ( ( userVote && userVote . votes ) ) {
214
- diff = v - userVote . votes ;
215
- }
216
-
217
- this . vote ( item . id , voteSettings . name , diff , voteSettings . currency , voteSettings . score * diff ) ;
218
- } }
219
- />
220
- }
221
- </ li > ;
222
- } ) }
223
- < li className = "vote-app__vote-score" key = "score" >
224
- Score { item . score }
225
- </ li >
226
- </ ul >
189
+ < table className = "vote-app__item-table" >
190
+ < tbody >
191
+ < tr >
192
+ < td className = "vote-app__item-score" >
193
+ { item . score }
194
+ </ td >
195
+ { listInfo . possibleVotes . map ( ( voteSettings , idx ) => {
196
+ let vote = item . votes [ idx ] ;
197
+ let userVote = item . userVotes && item . userVotes [ idx ] ;
198
+ let currencyInfo = selfInfo && voteSettings . currency && this . findByName ( selfInfo . currencies , voteSettings . currency ) ;
199
+ let maximum = voteSettings . maximum || 1000 ; // infinity
200
+ let minimum = voteSettings . minimum || 0 ;
201
+ let value = ( userVote && userVote . votes ) ? userVote . votes : 0 ;
202
+ if ( currencyInfo && currencyInfo . remaining + value < maximum ) maximum = currencyInfo . remaining + value ;
203
+ return < td >
204
+ < VoteButton
205
+ className = { "vote-app__vote-" + voteSettings . name }
206
+ value = { vote . votes } myValue = { userVote . votes }
207
+ maxUp = { maximum - userVote . votes } maxDown = { userVote . votes - minimum }
208
+ color = { this . getColor ( voteSettings . name ) } onVote = { ( diffValue ) => {
209
+ this . vote ( item . id , voteSettings . name , diffValue , voteSettings . currency , voteSettings . score ) ;
210
+ } } />
211
+ </ td > ;
212
+ } ) }
213
+ < td className = "vote-app__item-content" >
214
+ < span className = "vote-app__item-title" > { item . title } </ span >
215
+ < span > { item . description } </ span >
216
+ </ td >
217
+ </ tr >
218
+ </ tbody >
219
+ </ table >
227
220
</ li > ) }
228
221
{ listInfo . isAdmin && < li className = "vote-app__admin" >
229
222
< div > < input type = "text" value = { this . state . newTitle } disabled = { inProgress } onChange = { e => this . setState ( { newTitle : e . target . value } ) } /> </ div >
@@ -258,7 +251,7 @@ export default class VoteApp extends React.Component {
258
251
}
259
252
260
253
renderSelf ( ) {
261
- let { selfInfo, isFetchingSelf } = this . state ;
254
+ let { listInfo , selfInfo, isFetchingSelf } = this . state ;
262
255
if ( ! selfInfo ) {
263
256
if ( isFetchingSelf ) {
264
257
return < div className = "vote-app__self-info" > Loading user info...</ div > ;
@@ -272,11 +265,13 @@ export default class VoteApp extends React.Component {
272
265
delete window . localStorage . voteAppToken ;
273
266
window . location . reload ( ) ;
274
267
} } > Log out</ button >
275
- < ul className = "vote-app__currency-list" >
276
- { selfInfo . currencies . map ( currency => < li className = { "vote-app__currency-" + currency . name } title = { `${ currency . description } \nYou used ${ currency . used } of a total of ${ currency . value } ${ currency . displayName } .` } >
268
+ { listInfo && < ul className = "vote-app__currency-list" >
269
+ { selfInfo . currencies
270
+ . filter ( currency => listInfo . possibleVotes . some ( voteSettings => voteSettings . currency === currency . name ) )
271
+ . map ( currency => < li className = { "vote-app__currency-" + currency . name } title = { `${ currency . description } \nYou used ${ currency . used } of a total of ${ currency . value } ${ currency . displayName } .` } >
277
272
{ currency . remaining } { currency . displayName }
278
273
</ li > ) }
279
- </ ul >
274
+ </ ul > }
280
275
</ div > ;
281
276
}
282
277
}
0 commit comments