-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathvote_cfp.js
More file actions
70 lines (64 loc) · 2.04 KB
/
vote_cfp.js
File metadata and controls
70 lines (64 loc) · 2.04 KB
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
getVotedIds = function() {
let votedIds = [];
document.cookie.split(';').forEach(function(cookie) {
if (cookie.includes('voted_id=')) {
votedIds = JSON.parse(cookie.split('=')[1]);
}
})
return votedIds;
}
setVotedId = function(id) {
let arr = getVotedIds();
arr.push(id);
document.cookie = "voted_id=" + JSON.stringify(arr);
}
toggleVoted = function() {
let btn = document.getElementById('vote');
btn.disabled = true;
btn.innerHTML = "投票済み";
}
document.getElementById('vote').addEventListener('click', function() {
function print_vote_alert () {
alert("投票を受け付けられませんでした。\nしばらく時間をおいてから再度の投票をお願いします。");
}
const voteUrl = document.getElementById('vote').getAttribute('vote_url');
const eventAbbr = document.getElementById('vote').getAttribute('event_name');
const talkId = parseInt(document.getElementById('vote').getAttribute('talk_id'));
const method = 'POST'
const query = `mutation {
vote(input: {
confName: ${eventAbbr}
talkId: ${talkId}
})
}`;
const body = JSON.stringify({ query });
const headers = { 'Content-Type': 'application/json' };
try {
fetch(voteUrl,{ method, headers, body})
.then(r=> {
if (!r.ok) {
throw new Error();
}
return r.json();
})
.then(data => {
if (data.hasOwnProperty('errors')) {
throw new Error();
} else {
setVotedId(talkId);
toggleVoted();
}
})
.catch(e => {
print_vote_alert();
})
} catch(e) {
print_vote_alert();
}
return false;
});
window.addEventListener('DOMContentLoaded', function() {
if (getVotedIds().includes(parseInt(document.getElementById('vote').getAttribute('talk_id')))) {
toggleVoted();
}
})