(function () {
    "use strict";
    var updateInterval = 10 * 1000;  // 10s
    var status = null;

    // .templeture_text
    // .button.hot
    // .button.cold

    function getCurrentStatus(success, error) {
        var xhr = new XMLHttpRequest();
        xhr.open('GET', '/api/v1/status');
        xhr.responseType = 'json';
        xhr.onload = function () {
            if (xhr.status === 200 || xhr.status === 302) {
                status = xhr.response;
                success(status);
            } else {
                error();
            }
        };
        xhr.send();
    }

    function vote(hotOrCold, success, error) {
        var params = new FormData();
        params.append('vote', hotOrCold);

        var xhr = new XMLHttpRequest();
        xhr.open('POST', '/api/v1/status');
        xhr.responseType = 'json';
        xhr.onload = function () {
            if (xhr.status === 200 || xhr.status === 302) {
                status = xhr.response;
                success(status);
            } else {
                error();
            }
        };
        xhr.send(params);
    }

    function showErrorMessage() {
        alert('error');
    }

    function update() {
        document.querySelector('.temperature_text').innerText = status.templature;
        document.querySelector('.counter.hot').innerText = status.hot;
        document.querySelector('.counter.cold').innerText = status.cold;
    }

    getCurrentStatus(update, showErrorMessage);
    setInterval(function () {
        getCurrentStatus(update, showErrorMessage);
    }, updateInterval);

    document.querySelector('.button.hot').addEventListener('click', function () {
        vote('hot', update, showErrorMessage);
    });
    document.querySelector('.button.cold').addEventListener('click', function () {
        vote('cold', update, showErrorMessage);
    });
    for(var button of document.querySelectorAll('.button')) {
        button.onclick = function() {return false;};
    }
})();

@@ -1,24 +1,80 @@
+/*%0A * %E6%B8%A9%E5%BA%A6%E3%81%A8%E6%8A%95%E7%A5%A8%E6%95%B0%E3%82%92%E4%B8%80%E5%AE%9A%E9%96%93%E9%9A%94%E3%81%A7%E8%87%AA%E5%8B%95%E6%9B%B4%E6%96%B0%E3%81%99%E3%82%8B%E3%80%82%0A * %E6%9A%91%E3%81%84%E3%83%BB%E5%AF%92%E3%81%84%E3%83%9C%E3%82%BF%E3%83%B3%E3%82%92%E6%8A%BC%E3%81%97%E3%81%9F%E3%81%A8%E3%81%8D%E3%81%AB%E3%80%81%E6%8A%95%E7%A5%A8%E3%81%99%E3%82%8B%E3%80%82%0A */%0A%0A
 (function () %7B%0A    %22use 

add comment