|
| 1 | +import React from 'react'; |
| 2 | +import PropTypes from 'prop-types'; |
| 3 | +import ImmutablePropTypes from 'react-immutable-proptypes'; |
| 4 | +import ImmutablePureComponent from 'react-immutable-pure-component'; |
| 5 | +import { defineMessages, injectIntl, FormattedMessage } from 'react-intl'; |
| 6 | +import classNames from 'classnames'; |
| 7 | +import { vote, fetchPoll } from 'mastodon/actions/polls'; |
| 8 | +import Motion from 'mastodon/features/ui/util/optional_motion'; |
| 9 | +import spring from 'react-motion/lib/spring'; |
| 10 | + |
| 11 | +const messages = defineMessages({ |
| 12 | + moments: { id: 'time_remaining.moments', defaultMessage: 'Moments remaining' }, |
| 13 | + seconds: { id: 'time_remaining.seconds', defaultMessage: '{number, plural, one {# second} other {# seconds}} left' }, |
| 14 | + minutes: { id: 'time_remaining.minutes', defaultMessage: '{number, plural, one {# minute} other {# minutes}} left' }, |
| 15 | + hours: { id: 'time_remaining.hours', defaultMessage: '{number, plural, one {# hour} other {# hours}} left' }, |
| 16 | + days: { id: 'time_remaining.days', defaultMessage: '{number, plural, one {# day} other {# days}} left' }, |
| 17 | +}); |
| 18 | + |
| 19 | +const SECOND = 1000; |
| 20 | +const MINUTE = 1000 * 60; |
| 21 | +const HOUR = 1000 * 60 * 60; |
| 22 | +const DAY = 1000 * 60 * 60 * 24; |
| 23 | + |
| 24 | +const timeRemainingString = (intl, date, now) => { |
| 25 | + const delta = date.getTime() - now; |
| 26 | + |
| 27 | + let relativeTime; |
| 28 | + |
| 29 | + if (delta < 10 * SECOND) { |
| 30 | + relativeTime = intl.formatMessage(messages.moments); |
| 31 | + } else if (delta < MINUTE) { |
| 32 | + relativeTime = intl.formatMessage(messages.seconds, { number: Math.floor(delta / SECOND) }); |
| 33 | + } else if (delta < HOUR) { |
| 34 | + relativeTime = intl.formatMessage(messages.minutes, { number: Math.floor(delta / MINUTE) }); |
| 35 | + } else if (delta < DAY) { |
| 36 | + relativeTime = intl.formatMessage(messages.hours, { number: Math.floor(delta / HOUR) }); |
| 37 | + } else { |
| 38 | + relativeTime = intl.formatMessage(messages.days, { number: Math.floor(delta / DAY) }); |
| 39 | + } |
| 40 | + |
| 41 | + return relativeTime; |
| 42 | +}; |
| 43 | + |
| 44 | +export default @injectIntl |
| 45 | +class Poll extends ImmutablePureComponent { |
| 46 | + |
| 47 | + static propTypes = { |
| 48 | + poll: ImmutablePropTypes.map.isRequired, |
| 49 | + intl: PropTypes.object.isRequired, |
| 50 | + dispatch: PropTypes.func, |
| 51 | + disabled: PropTypes.bool, |
| 52 | + }; |
| 53 | + |
| 54 | + state = { |
| 55 | + selected: {}, |
| 56 | + }; |
| 57 | + |
| 58 | + handleOptionChange = e => { |
| 59 | + const { target: { value } } = e; |
| 60 | + |
| 61 | + if (this.props.poll.get('multiple')) { |
| 62 | + const tmp = { ...this.state.selected }; |
| 63 | + tmp[value] = true; |
| 64 | + this.setState({ selected: tmp }); |
| 65 | + } else { |
| 66 | + const tmp = {}; |
| 67 | + tmp[value] = true; |
| 68 | + this.setState({ selected: tmp }); |
| 69 | + } |
| 70 | + }; |
| 71 | + |
| 72 | + handleVote = () => { |
| 73 | + if (this.props.disabled) { |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + this.props.dispatch(vote(this.props.poll.get('id'), Object.keys(this.state.selected))); |
| 78 | + }; |
| 79 | + |
| 80 | + handleRefresh = () => { |
| 81 | + if (this.props.disabled) { |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + this.props.dispatch(fetchPoll(this.props.poll.get('id'))); |
| 86 | + }; |
| 87 | + |
| 88 | + renderOption (option, optionIndex) { |
| 89 | + const { poll } = this.props; |
| 90 | + const percent = (option.get('votes_count') / poll.get('votes_count')) * 100; |
| 91 | + const leading = poll.get('options').filterNot(other => other.get('title') === option.get('title')).every(other => option.get('votes_count') > other.get('votes_count')); |
| 92 | + const active = !!this.state.selected[`${optionIndex}`]; |
| 93 | + const showResults = poll.get('voted') || poll.get('expired'); |
| 94 | + |
| 95 | + return ( |
| 96 | + <li key={option.get('title')}> |
| 97 | + {showResults && ( |
| 98 | + <Motion defaultStyle={{ width: 0 }} style={{ width: spring(percent, { stiffness: 180, damping: 12 }) }}> |
| 99 | + {({ width }) => |
| 100 | + <span className={classNames('poll__chart', { leading })} style={{ width: `${width}%` }} /> |
| 101 | + } |
| 102 | + </Motion> |
| 103 | + )} |
| 104 | + |
| 105 | + <label className={classNames('poll__text', { selectable: !showResults })}> |
| 106 | + <input |
| 107 | + name='vote-options' |
| 108 | + type={poll.get('multiple') ? 'checkbox' : 'radio'} |
| 109 | + value={optionIndex} |
| 110 | + checked={active} |
| 111 | + onChange={this.handleOptionChange} |
| 112 | + /> |
| 113 | + |
| 114 | + {!showResults && <span className={classNames('poll__input', { active })} />} |
| 115 | + {showResults && <span className='poll__number'>{Math.floor(percent)}%</span>} |
| 116 | + |
| 117 | + {option.get('title')} |
| 118 | + </label> |
| 119 | + </li> |
| 120 | + ); |
| 121 | + } |
| 122 | + |
| 123 | + render () { |
| 124 | + const { poll, intl } = this.props; |
| 125 | + const timeRemaining = timeRemainingString(intl, new Date(poll.get('expires_at')), intl.now()); |
| 126 | + const showResults = poll.get('voted') || poll.get('expired'); |
| 127 | + const disabled = this.props.disabled || Object.entries(this.state.selected).every(item => !item); |
| 128 | + |
| 129 | + return ( |
| 130 | + <div className='poll'> |
| 131 | + <ul> |
| 132 | + {poll.get('options').map((option, i) => this.renderOption(option, i))} |
| 133 | + </ul> |
| 134 | + |
| 135 | + <div className='poll__footer'> |
| 136 | + {!showResults && <button className='button button-secondary' disabled={disabled} onClick={this.handleVote}><FormattedMessage id='poll.vote' defaultMessage='Vote' /></button>} |
| 137 | + {showResults && !this.props.disabled && <span><button className='poll__link' onClick={this.handleRefresh}><FormattedMessage id='poll.refresh' defaultMessage='Refresh' /></button> · </span>} |
| 138 | + <FormattedMessage id='poll.total_votes' defaultMessage='{count, plural, one {# vote} other {# votes}}' values={{ count: poll.get('votes_count') }} /> · {timeRemaining} |
| 139 | + </div> |
| 140 | + </div> |
| 141 | + ); |
| 142 | + } |
| 143 | + |
| 144 | +} |
0 commit comments