/* behavioural layer for the `network_modify.html.tera` template - intercept button click for modify (form submission of credentials) - perform json api call - update the dom methods: PEACH_NETWORK.modify(); */ var PEACH_NETWORK = {}; // catch click of 'Save' button and make POST request PEACH_NETWORK.modify = function() { document.addEventListener('DOMContentLoaded', function() { document.body.addEventListener('submit', function(e) { // prevent redirect on button press (default behavior) e.preventDefault(); // capture form data var formElement = document.querySelector("form"); // create form data object from the wifiModify form element var formData = new FormData(formElement); var object = {}; // assign ssid and pass from form formData.forEach(function(value, key){ object[key] = value; }); // perform json serialization var jsonData = JSON.stringify(object); // write in-progress status message to ui PEACH.flashMsg("info", "Updating WiFi password..."); // send new_password POST request fetch("/api/v1/network/wifi/modify", { method: "post", headers: { 'Content-Type': 'application/json', }, body: jsonData }) .then( (response) => { return response.json() }) .then( (jsonData) => { // write json response message to ui PEACH.flashMsg(jsonData.status, jsonData.msg); }) }, false); }); } var modifyInstance = PEACH_NETWORK; modifyInstance.modify();