diff --git a/app/static/css/styles.css b/app/static/css/styles.css index d3466ee..f40e4d4 100644 --- a/app/static/css/styles.css +++ b/app/static/css/styles.css @@ -8,3 +8,19 @@ height: 100%; } } + +.editable-field span { + padding-left: 10px; + padding-right: 10px; + padding-top: 5px; + padding-bottom: 5px; +} + +.editable-field i.edit { + opacity: 0; +} + +.editable-field:hover i.edit, .editable-field:focus i.edit { + opacity: 1; + transition: 0.3s; +} diff --git a/app/static/js/devices.js b/app/static/js/devices.js index 1d9e7af..ffdf999 100644 --- a/app/static/js/devices.js +++ b/app/static/js/devices.js @@ -45,7 +45,8 @@ function generateAccordionItems(devices) {

@@ -119,6 +120,33 @@ function addDevice(deviceName, isDeviceTool) { }); } +function updateDevice(deviceId, deviceName) { + // Prepare device data + const deviceData = { + name: deviceName, + }; + // Make API call to update device + fetch(`/api/devices/${deviceId}`, { + method: "PUT", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(deviceData), + }) + .then((response) => { + // Check if response status code is 200 (OK) + if (response.status === 200) { + alert("Device updated successfully!"); + } else { + alert("Error updating device. Please try again later."); + } + }) + .catch((error) => { + console.error("Error updating device:", error); + alert("Error updating device. Please try again later."); + }); +} + function removeDevice(deviceId) { fetch(`/api/devices/${deviceId}`, { method: "DELETE", @@ -143,3 +171,27 @@ function refreshDevicesList() { .then((data) => generateAccordionItems(data)) .catch((error) => console.error("Error fetching data:", error)); } + +function makeEditable(event, deviceId, isEditIconClicked = false) { + const element = document.getElementById(`device-${deviceId}`); + if (isEditIconClicked) { + element === document.activeElement ? element.blur() : element.focus(); + } + let originalContent = element.textContent; + + element.addEventListener("keydown", function (event) { + if (event.key === "Enter") { + this.blur(); + } + }); + + element.onblur = async function () { + if (element.textContent === "" || element.textContent === originalContent) { + // if the new name is empty, revert to the original name + // TODO: add validation + element.textContent = originalContent; + return; + } + await updateDevice(deviceId, element.textContent); + }; +} diff --git a/app/templates/prismo/settings.html b/app/templates/prismo/settings.html index 3542e35..020e0f8 100644 --- a/app/templates/prismo/settings.html +++ b/app/templates/prismo/settings.html @@ -6,7 +6,7 @@

Settings

- +