]> cat aescling's git repositories - mastodon.git/blob - app/javascript/mastodon/components/poll.js
Fix poll visibility on public pages (#10817)
[mastodon.git] / app / javascript / mastodon / components / poll.js
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 import escapeTextContentForBrowser from 'escape-html';
11 import emojify from 'mastodon/features/emoji/emoji';
12 import RelativeTimestamp from './relative_timestamp';
13
14 const messages = defineMessages({
15 closed: { id: 'poll.closed', defaultMessage: 'Closed' },
16 });
17
18 const makeEmojiMap = record => record.get('emojis').reduce((obj, emoji) => {
19 obj[`:${emoji.get('shortcode')}:`] = emoji.toJS();
20 return obj;
21 }, {});
22
23 export default @injectIntl
24 class Poll extends ImmutablePureComponent {
25
26 static propTypes = {
27 poll: ImmutablePropTypes.map,
28 intl: PropTypes.object.isRequired,
29 dispatch: PropTypes.func,
30 disabled: PropTypes.bool,
31 };
32
33 state = {
34 selected: {},
35 };
36
37 handleOptionChange = e => {
38 const { target: { value } } = e;
39
40 if (this.props.poll.get('multiple')) {
41 const tmp = { ...this.state.selected };
42 if (tmp[value]) {
43 delete tmp[value];
44 } else {
45 tmp[value] = true;
46 }
47 this.setState({ selected: tmp });
48 } else {
49 const tmp = {};
50 tmp[value] = true;
51 this.setState({ selected: tmp });
52 }
53 };
54
55 handleVote = () => {
56 if (this.props.disabled) {
57 return;
58 }
59
60 this.props.dispatch(vote(this.props.poll.get('id'), Object.keys(this.state.selected)));
61 };
62
63 handleRefresh = () => {
64 if (this.props.disabled) {
65 return;
66 }
67
68 this.props.dispatch(fetchPoll(this.props.poll.get('id')));
69 };
70
71 renderOption (option, optionIndex) {
72 const { poll, disabled } = this.props;
73 const percent = poll.get('votes_count') === 0 ? 0 : (option.get('votes_count') / poll.get('votes_count')) * 100;
74 const leading = poll.get('options').filterNot(other => other.get('title') === option.get('title')).every(other => option.get('votes_count') > other.get('votes_count'));
75 const active = !!this.state.selected[`${optionIndex}`];
76 const showResults = poll.get('voted') || poll.get('expired');
77
78 let titleEmojified = option.get('title_emojified');
79 if (!titleEmojified) {
80 const emojiMap = makeEmojiMap(poll);
81 titleEmojified = emojify(escapeTextContentForBrowser(option.get('title')), emojiMap);
82 }
83
84 return (
85 <li key={option.get('title')}>
86 {showResults && (
87 <Motion defaultStyle={{ width: 0 }} style={{ width: spring(percent, { stiffness: 180, damping: 12 }) }}>
88 {({ width }) =>
89 <span className={classNames('poll__chart', { leading })} style={{ width: `${width}%` }} />
90 }
91 </Motion>
92 )}
93
94 <label className={classNames('poll__text', { selectable: !showResults })}>
95 <input
96 name='vote-options'
97 type={poll.get('multiple') ? 'checkbox' : 'radio'}
98 value={optionIndex}
99 checked={active}
100 onChange={this.handleOptionChange}
101 disabled={disabled}
102 />
103
104 {!showResults && <span className={classNames('poll__input', { checkbox: poll.get('multiple'), active })} />}
105 {showResults && <span className='poll__number'>{Math.round(percent)}%</span>}
106
107 <span dangerouslySetInnerHTML={{ __html: titleEmojified }} />
108 </label>
109 </li>
110 );
111 }
112
113 render () {
114 const { poll, intl } = this.props;
115
116 if (!poll) {
117 return null;
118 }
119
120 const timeRemaining = poll.get('expired') ? intl.formatMessage(messages.closed) : <RelativeTimestamp timestamp={poll.get('expires_at')} futureDate />;
121 const showResults = poll.get('voted') || poll.get('expired');
122 const disabled = this.props.disabled || Object.entries(this.state.selected).every(item => !item);
123
124 return (
125 <div className='poll'>
126 <ul>
127 {poll.get('options').map((option, i) => this.renderOption(option, i))}
128 </ul>
129
130 <div className='poll__footer'>
131 {!showResults && <button className='button button-secondary' disabled={disabled} onClick={this.handleVote}><FormattedMessage id='poll.vote' defaultMessage='Vote' /></button>}
132 {showResults && !this.props.disabled && <span><button className='poll__link' onClick={this.handleRefresh}><FormattedMessage id='poll.refresh' defaultMessage='Refresh' /></button> · </span>}
133 <FormattedMessage id='poll.total_votes' defaultMessage='{count, plural, one {# vote} other {# votes}}' values={{ count: poll.get('votes_count') }} />
134 {poll.get('expires_at') && <span> · {timeRemaining}</span>}
135 </div>
136 </div>
137 );
138 }
139
140 }
This page took 0.107279 seconds and 4 git commands to generate.