-
Notifications
You must be signed in to change notification settings - Fork 48
/
index.js
213 lines (202 loc) · 6.81 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// @flow
import React from 'react';
import Button from 'app/components/Button';
import styles from './Poll.css';
import type { PollEntity, OptionEntity } from 'app/reducers/polls';
import { sortBy } from 'lodash';
import { Link } from 'react-router-dom';
import Icon from 'app/components/Icon';
import { Flex } from 'app/components/Layout';
import Tooltip from 'app/components/Tooltip';
import cx from 'classnames';
type Props = {
poll: PollEntity,
handleVote: (pollId: number, optionId: number) => Promise<*>,
backgroundLight?: boolean,
truncate?: number,
details?: boolean,
};
type OptionEntityRatio = OptionEntity & {
ratio: number,
};
type State = {
truncateOptions: boolean,
shuffledOptions: Array<OptionEntityRatio>,
expanded: boolean,
};
class Poll extends React.Component<Props, State> {
constructor(props: Props) {
super(props);
const options = this.optionsWithPerfectRatios(props.poll.options);
const shuffledOptions = this.shuffle(options);
if (props.truncate && options.length > props.truncate) {
this.state = {
truncateOptions: true,
shuffledOptions: shuffledOptions,
expanded: false,
};
} else {
this.state = {
truncateOptions: false,
shuffledOptions: shuffledOptions,
expanded: true,
};
}
}
toggleTruncate = () => {
this.setState({
expanded: !this.state.expanded,
});
};
optionsWithPerfectRatios = (options: Array<OptionEntity>) => {
const totalVotes = options.reduce((a, option) => a + option.votes, 0);
const ratios = options.map((option) => {
return { ...option, ratio: (option.votes / totalVotes) * 100 };
});
return this.perfectRatios(ratios);
};
// As described in: https://stackoverflow.com/questions/13483430/how-to-make-rounded-percentages-add-up-to-100
perfectRatios = (options: Array<OptionEntityRatio>) => {
const off =
100 - options.reduce((a, option) => a + Math.floor(option.ratio), 0);
return sortBy(options, (o) => Math.floor(o.ratio) - o.ratio)
.map((option, index) => {
return {
...option,
ratio: Math.floor(option.ratio) + (index < off ? 1 : 0),
};
})
.sort((a, b) => b.ratio - a.ratio);
};
shuffle = (array: Array<OptionEntityRatio>) => {
const oldArray = array.slice(0);
const newArray = [];
for (let i = 0; i < array.length; i++) {
const randIndex = Math.floor(Math.random() * oldArray.length);
newArray[i] = oldArray[randIndex];
oldArray.splice(randIndex, 1);
}
return newArray;
};
render() {
const { poll, handleVote, backgroundLight, details, truncate } = this.props;
const { truncateOptions, expanded, shuffledOptions } = this.state;
const { id, title, description, hasAnswered, totalVotes } = poll;
const options = this.optionsWithPerfectRatios(this.props.poll.options);
const orderedOptions = hasAnswered ? options : shuffledOptions;
const optionsToShow = expanded
? orderedOptions
: orderedOptions.slice(0, truncate);
return (
<div className={cx(styles.poll, backgroundLight ? styles.pollLight : '')}>
<Flex>
<Link to={`/polls/${id}`} style={{ flex: 1 }}>
<Icon name="stats" />
<span className={styles.pollHeader}>{title}</span>
</Link>
<Tooltip content="Avstemningen er anonym." renderDirection="left">
<Icon
name="information-circle-outline"
size={20}
style={{ cursor: 'pointer' }}
/>
</Tooltip>
</Flex>
{details && (
<div>
<p>{description}</p>
</div>
)}
{hasAnswered && (
<Flex column className={styles.optionWrapper}>
<table className={styles.pollTable}>
<tbody>
{optionsToShow.map(({ id, name, votes, ratio }) => {
return (
<tr key={id}>
<td className={styles.textColumn}>{name}</td>
<td className={styles.graphColumn}>
{votes === 0 ? (
<span className={styles.noVotes}>Ingen stemmer</span>
) : (
<div className={styles.fullGraph}>
<div
style={{
width: `${ratio}%`,
}}
>
<div className={styles.pollGraph}>
{ratio >= 18 && <span>{`${ratio}%`}</span>}
</div>
</div>
{ratio < 18 && (
<span style={{ marginLeft: '2px' }}>
{`${ratio}%`}
</span>
)}
</div>
)}
</td>
</tr>
);
})}
</tbody>
</table>
</Flex>
)}
{!hasAnswered && (
<Flex column className={styles.optionWrapper}>
{!expanded && (
<Flex
className={styles.blurContainer}
onClick={this.toggleTruncate}
>
<p className={styles.blurOverlay}>
Klikk her for å se alle alternativene.
</p>
<Icon
className={cx(styles.blurOverlay, styles.blurArrow)}
size={60}
name={expanded ? 'arrow-up' : 'arrow-down'}
/>
</Flex>
)}
{options &&
optionsToShow.map((option) => (
<Flex
className={cx(
styles.alignItems,
expanded ? '' : styles.blurEffect
)}
key={option.id}
>
<Button
className={styles.voteButton}
onClick={() => handleVote(poll.id, option.id)}
>
{option.name}
</Button>
</Flex>
))}
</Flex>
)}
<div style={{ height: '29px' }}>
<div className={styles.moreOptionsLink}>
<span>{`Stemmer: ${totalVotes}`}</span>
{truncateOptions && (
<div className={styles.alignItems}>
<Icon
onClick={this.toggleTruncate}
className={styles.arrow}
size={20}
name={expanded ? 'arrow-up' : 'arrow-down'}
/>
</div>
)}
</div>
</div>
</div>
);
}
}
export default Poll;