peach-workspace/peach-web/static/js/power_menu.js

84 lines
2.8 KiB
JavaScript

/*
behavioural layer for the `power.html.tera` template,
corresponding to the web route `/power`
- intercept button clicks for reboot & shutdown
- perform json api calls
- update the dom
methods:
PEACH_DEVICE.reboot();
PEACH_DEVICE.shutdown();
*/
var PEACH_DEVICE = {};
// catch click of 'Reboot' button and make POST request
PEACH_DEVICE.reboot = function() {
document.addEventListener('DOMContentLoaded', function() {
var rebootDevice = document.getElementById('rebootBtn');
if (rebootDevice) {
rebootDevice.addEventListener('click', function(e) {
// prevent redirect on button press (default behavior)
e.preventDefault();
// write reboot flash message
PEACH.flashMsg("success", "Rebooting the device...");
// send reboot_device POST request
fetch("/api/v1/admin/reboot", {
method: "post",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
})
.then( (response) => {
return response.json()
})
.then( (jsonData) => {
console.log(jsonData.msg);
// write json response message to ui
PEACH.flashMsg(jsonData.status, jsonData.msg);
})
}, false);
}
});
}
// catch click of 'Shutdown' button and make POST request
PEACH_DEVICE.shutdown = function() {
document.addEventListener('DOMContentLoaded', function() {
var shutdownDevice = document.getElementById('shutdownBtn');
if (shutdownDevice) {
shutdownDevice.addEventListener('click', function(e) {
// prevent form submission (default behavior)
e.preventDefault();
// write shutdown flash message
PEACH.flashMsg("success", "Shutting down the device...");
// send shutdown_device POST request
fetch("/api/v1/shutdown", {
method: "post",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
})
.then( (response) => {
return response.json()
})
.then( (jsonData) => {
console.log(jsonData.msg);
// write json response message to ui
PEACH.flashMsg(jsonData.status, jsonData.msg);
})
}, false);
}
});
}
var deviceInstance = PEACH_DEVICE;
deviceInstance.reboot();
deviceInstance.shutdown();