remove duplicate flashMsg code

This commit is contained in:
2021-11-22 15:29:40 +02:00
parent 554997a5c0
commit e54ff8829a
4 changed files with 16 additions and 158 deletions

View File

@ -1,7 +1,7 @@
/*
behavioural layer for the `network_detail.html.tera` template,
corresponding to the web route `/network/wifi?<ssid>`
corresponding to the web route `/settings/network/wifi?<ssid>`
- intercept button clicks for connect, disconnect and forget
- perform json api call
@ -12,7 +12,6 @@ methods:
PEACH_NETWORK.connect();
PEACH_NETWORK.disconnect();
PEACH_NETWORK.forget();
PEACH_NETWORK.flashMsg(status, msg);
*/
@ -33,7 +32,7 @@ PEACH_NETWORK.connect = function() {
// perform json serialization
var jsonData = JSON.stringify(ssidData);
// write in-progress status message to ui
PEACH_NETWORK.flashMsg("info", "Connecting to access point...");
PEACH.flashMsg("info", "Connecting to access point...");
// send add_wifi POST request
fetch("/api/v1/network/wifi/connect", {
method: "post",
@ -47,7 +46,7 @@ PEACH_NETWORK.connect = function() {
})
.then( (jsonData) => {
// write json response message to ui
PEACH_NETWORK.flashMsg(jsonData.status, jsonData.msg);
PEACH.flashMsg(jsonData.status, jsonData.msg);
})
}, false);
};
@ -69,7 +68,7 @@ PEACH_NETWORK.disconnect = function() {
// perform json serialization
var jsonData = JSON.stringify(ssidData);
// write in-progress status message to ui
PEACH_NETWORK.flashMsg("info", "Disconnecting from access point...");
PEACH.flashMsg("info", "Disconnecting from access point...");
// send disconnect_wifi POST request
fetch("/api/v1/network/wifi/disconnect", {
method: "post",
@ -83,7 +82,7 @@ PEACH_NETWORK.disconnect = function() {
})
.then( (jsonData) => {
// write json response message to ui
PEACH_NETWORK.flashMsg(jsonData.status, jsonData.msg);
PEACH.flashMsg(jsonData.status, jsonData.msg);
})
}, false);
};
@ -105,7 +104,7 @@ PEACH_NETWORK.forget = function() {
// perform json serialization
var jsonData = JSON.stringify(ssidData);
// write in-progress status message to ui
PEACH_NETWORK.flashMsg("info", "Removing credentials for access point...");
PEACH.flashMsg("info", "Removing credentials for access point...");
// send forget_ap POST request
fetch("/api/v1/network/wifi/forget", {
method: "post",
@ -119,48 +118,13 @@ PEACH_NETWORK.forget = function() {
})
.then( (jsonData) => {
// write json response message to ui
PEACH_NETWORK.flashMsg(jsonData.status, jsonData.msg);
PEACH.flashMsg(jsonData.status, jsonData.msg);
})
}, false);
};
});
}
// display a message by appending a paragraph element
PEACH_NETWORK.flashMsg = function(status, msg) {
// set the class of the element according to status
var elementClass;
if (status === "success") {
elementClass = "capsule center-text flash-message font-success";
} else if (status === "info") {
elementClass = "capsule center-text flash-message font-info";
} else {
elementClass = "capsule center-text flash-message font-failure";
};
var flashElement = document.getElementById("flashMsg");
// if flashElement exists, update the class & text
if (flashElement) {
flashElement.className = elementClass;
flashElement.innerText = msg;
// if flashElement does not exist, create it, set id, class, text & append
} else {
// create new div for flash message
var flashDiv = document.createElement("DIV");
// set div attributes
flashDiv.id = "flashMsg";
flashDiv.className = elementClass;
// add json response message to flash message div
var flashMsg = document.createTextNode(msg);
flashDiv.appendChild(flashMsg);
// insert the flash message div below the button div
var buttonDiv = document.getElementById("buttonDiv");
// flashDiv will be added to the end since buttonDiv is the last
// child within the parent element (card-container div)
buttonDiv.parentNode.insertBefore(flashDiv, buttonDiv.nextSibling);
}
}
var detailInstance = PEACH_NETWORK;
detailInstance.connect();
detailInstance.disconnect();