-
Notifications
You must be signed in to change notification settings - Fork 153
/
creep-vote.jsx
183 lines (162 loc) · 5.67 KB
/
creep-vote.jsx
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
import React from 'react';
import Creepometer from '../creepometer/creepometer.jsx';
import CreepChart from '../creepiness-chart/creepiness-chart.jsx';
import LikelyhoodChart from '../likelyhood-chart/likelyhood-chart.jsx';
export default class CreepVote extends React.Component {
constructor(props) {
super(props);
this.state = this.getInitialState();
}
getInitialState() {
// let conf = this.props.votes.confidence;
let totalVotes = this.props.votes.total;
return {
totalVotes,
creepiness: 50,
confidence: undefined,
didVote: false
};
}
showVoteResult() {
if (this.state.creepinessSubmitted && this.state.confidenceSubmitted) {
this.setState({ didVote: true });
}
}
sendVoteFor(payload) {
let attribute = payload.attribute;
let url = `/privacynotincluded/vote`;
let method = `POST`;
let credentials = `same-origin`;
let headers = {
"X-CSRFToken": this.props.csrf,
"Content-Type": `application/json`
};
fetch(url, {
method,
credentials,
headers,
body: JSON.stringify(payload)
})
.then(() => {
let update = {};
update[`${attribute}Submitted`] = true;
this.setState(update, () => {
this.showVoteResult();
});
})
.catch(e => {
console.warn(e);
this.setState({ disableVoteButton: false });
});
}
submitVote(evt) {
evt.preventDefault();
let confidence = this.state.confidence;
if (confidence === undefined) {
return;
}
this.setState({ disableVoteButton: true });
let productID = this.props.productID;
this.sendVoteFor({
attribute: `confidence`,
productID,
value: confidence,
});
this.sendVoteFor({
attribute: `creepiness`,
productID,
value: this.state.creepiness
});
}
setCreepiness(creepiness) {
this.setState({ creepiness });
}
setConfidence(confidence) {
this.setState({ confidence });
}
/**
* @returns {jsx} What users see when they haven't voted on this product yet.
*/
renderVoteAsk() {
return (<form method="post" id="creep-vote" onSubmit={evt => this.submitVote(evt)}>
<div className="row mb-5">
<div className="col-12 col-md-6">
<div className="mb-4 text-center">
<h3 className="h5-heading mb-2">How creepy is this product?</h3>
<p className="help-text">Majority of voters think it is super creepy</p>
</div>
<Creepometer initialValue={this.state.creepiness} onChange={value => this.setCreepiness(value)}></Creepometer>
</div>
<div className="col-12 col-md-6">
<div className="mb-4 text-center">
<h3 className="h5-heading mb-2">How likely are you to buy it?</h3>
<p className="help-text">Majority of voters are not likely to buy it</p>
</div>
<div className="text-center">
<div class="btn-group btn-group-toggle mt-5" data-toggle="buttons">
<label for="likely">
<input type="radio" name="wouldbuy" id="likely" autocomplete="off" required/>
<span class="likely btn" onClick={() => this.setConfidence(true)}><img alt="thumb up" src="/_images/buyers-guide/icon-thumb-up-black.svg" /> Likely</span>
</label>
<label for="unlikely">
<input type="radio" name="wouldbuy" id="unlikely" autocomplete="off" required/>
<span class="unlikely btn" onClick={() => this.setConfidence(false)}><img alt="thumb down" src="/_images/buyers-guide/icon-thumb-down-black.svg" /> Not likely</span>
</label>
</div>
</div>
</div>
</div>
<div className="row">
<div className="col-12 text-center">
<button type="submit" className="btn btn-ghost mb-2" disabled={this.state.confidence===undefined}>Vote & See Results</button>
<p>{this.state.totalVotes} votes</p>
</div>
</div>
</form>);
}
/**
* @returns {jsx} What users see when they have voted on this product.
*/
renderDidVote(){
return(
<div>
<div className="mb-5">
<div className="col-12 text-center">
<h3 className="h5-heading mb-1">Thanks for voting! Here are the results so far:</h3>
<div className="text-muted">{this.state.totalVotes + 1} Votes</div>
</div>
<div className="row mt-3">
<div className="col">
<CreepChart userVoteGroup={Math.floor(this.state.creepiness/20)} values={this.props.votes.creepiness.vote_breakdown} />
</div>
<div className="col likelyhood-chart p-5">
<LikelyhoodChart values={this.props.votes.confidence} />
</div>
</div>
</div>
<div className="text-center">
<div><a className="share-results" href="#coral_talk_stream">View comments</a> or share your results</div>
{/* TODO: Make these share links work */}
<div class="social d-flex justify-content-center mt-3">
<a class="social-button social-button-fb" href=""><span class="sr-only">Facebook</span></a>
<a class="social-button social-button-twitter" href=""><span class="sr-only">Twitter</span></a>
<a class="social-button social-button-email" href=""><span class="sr-only">Email</span></a>
</div>
</div>
</div>
);
}
render() {
let voteContent;
if(this.state.didVote){
voteContent = this.renderDidVote();
} else {
voteContent = this.renderVoteAsk();
}
return (
<div className="creep-vote py-5">
{ voteContent }
</div>
);
}
}