Skip to content

Instantly share code, notes, and snippets.

@felipe3dfx
Last active August 11, 2016 19:16
Show Gist options
  • Save felipe3dfx/8be91849b544976cc34cc3b1695114c5 to your computer and use it in GitHub Desktop.
Save felipe3dfx/8be91849b544976cc34cc3b1695114c5 to your computer and use it in GitHub Desktop.
Alert bar to show information from the closest incident using cachet API
document.addEventListener( 'DOMContentLoaded', function () {
function cachetAlert(cachetUrl) {
var createAlert = function(incident) {
var divElement = document.createElement('div');
divElement.id = 'cachet-alert';
divElement.setAttribute('style', 'background-color: #FF3D2E; text-align: center; font-family: sans-serif;');
divElement.innerHTML = '<a style="color: #fff; display: block; padding: 5px;" href="'+ cachetUrl +'" target="_blank">'+ incident.name +'</a>';
document.body.insertBefore(divElement, document.body.children[0]);
};
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == XMLHttpRequest.DONE) {
if (xmlhttp.status == 200) {
incident = JSON.parse(xmlhttp.responseText).data[0];
if (incident.status !== 4 && incident.status !== 0) {
createAlert(incident);
} else {
var xmlhttp2 = new XMLHttpRequest();
xmlhttp2.onreadystatechange = function() {
if (xmlhttp2.readyState == XMLHttpRequest.DONE) {
if (xmlhttp2.status == 200) {
scheduledIncident = JSON.parse(xmlhttp2.responseText).data[0];
if (new Date(scheduledIncident.scheduled_at) > new Date()) {
createAlert(scheduledIncident);
}
}
}
};
xmlhttp2.open('GET', cachetUrl + '/api/v1/incidents/?sort=id&order=desc&per_page=1&status=0', true);
xmlhttp2.send();
}
}
}
};
xmlhttp.open('GET', cachetUrl + '/api/v1/incidents/?sort=id&order=desc&per_page=1', true);
xmlhttp.send();
}
}, false );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment