Created
March 30, 2019 09:11
-
-
Save chaitanyaGitHub1/c3d3f9282b0774488f57fae14d7e3d83 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const form = document.getElementById('vote-form'); | |
var event; | |
function setCookie(cname,cvalue,exdays) { | |
var d = new Date(); | |
d.setTime(d.getTime() + (exdays*24*60*60*1000)); | |
var expires = "expires=" + d.toGMTString(); | |
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/"; | |
} | |
function getCookie(cname) { | |
var name = cname + "="; | |
var decodedCookie = decodeURIComponent(document.cookie); | |
var ca = decodedCookie.split(';'); | |
for(var i = 0; i < ca.length; i++) { | |
var c = ca[i]; | |
while (c.charAt(0) == ' ') { | |
c = c.substring(1); | |
} | |
if (c.indexOf(name) == 0) { | |
return c.substring(name.length, c.length); | |
} | |
} | |
return ""; | |
} | |
form.addEventListener('submit', e=>{ | |
var user=getCookie("username"); | |
if (user != "") { | |
alert("Sorry, you can only vote one time"); | |
document.getElementById("myBtn").disabled = true; | |
} else { | |
user = prompt("Please enter your name:",""); | |
const choice = document.querySelector('input[name=os]:checked').value; | |
const data = {os: choice}; | |
fetch('http://localhost:3000/poll',{ | |
method: 'post', | |
body: JSON.stringify(data), | |
headers: new Headers({ | |
'Content-Type': 'application/json' | |
}) | |
}).then(res => res.json()) | |
.catch(err => console.log(err)); | |
if (user != "" && user != null) { | |
setCookie("username", user, 30); | |
} | |
e.preventDefault(); | |
} | |
}); | |
fetch("http://localhost:3000/poll") | |
.then(res => res.json()) | |
.then(data => { | |
let votes = data.votes; | |
let totalVotes = votes.length; | |
document.querySelector('#chartTitle').textContent = `Total Votes: ${totalVotes}`; | |
let voteCounts = { | |
Windows: 0, | |
MacOS: 0, | |
Linux: 0, | |
Other: 0 | |
}; | |
voteCounts = votes.reduce((acc, vote) => ( | |
(acc[vote.os] = (acc[vote.os] || 0) + parseInt(vote.points)), acc), | |
{} | |
); | |
let dataPoints = [ | |
{ label: 'Windows', y: voteCounts.Windows }, | |
{ label: 'MacOS', y: voteCounts.MacOS }, | |
{ label: 'Linux', y: voteCounts.Linux }, | |
{ label: 'Other', y: voteCounts.Other } | |
]; | |
const chartContainer = document.querySelector('#chartContainer'); | |
if(chartContainer){ | |
// Listen for the event. | |
document.addEventListener('votesAdded', function (e) { | |
document.querySelector('#chartTitle').textContent = `Total Votes: ${e.detail.totalVotes}`; | |
}); | |
const chart = new CanvasJS.Chart('chartContainer', { | |
animationEnabled: true, | |
theme: 'theme1', | |
data:[ | |
{ | |
type: 'column', | |
dataPoints: dataPoints | |
} | |
] | |
}); | |
chart.render(); | |
// Enable pusher logging - don't include this in production | |
Pusher.logToConsole = true; | |
var pusher = new Pusher('196d848ff103f7c25b5c', { | |
cluster: 'ap2', | |
encrypted: true | |
}); | |
var channel = pusher.subscribe('os-poll'); | |
channel.bind('os-vote', function(data) { | |
dataPoints.forEach((point)=>{ | |
if(point.label==data.os) | |
{ | |
point.y+=data.points; | |
totalVotes+=data.points; | |
event = new CustomEvent('votesAdded',{detail:{totalVotes:totalVotes}}); | |
// Dispatch the event. | |
document.dispatchEvent(event); | |
} | |
}); | |
chart.render(); | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment