From c824110b001f817ed51d1beb07c348ccc7d295c2 Mon Sep 17 00:00:00 2001 From: RubenPX Date: Thu, 28 Apr 2022 18:29:18 +0200 Subject: [PATCH 1/2] Send device model through the methods --- ereuse_devicehub/static/js/main_inventory.js | 48 +++++++++----------- 1 file changed, 21 insertions(+), 27 deletions(-) diff --git a/ereuse_devicehub/static/js/main_inventory.js b/ereuse_devicehub/static/js/main_inventory.js index 6cb71534..3859c731 100644 --- a/ereuse_devicehub/static/js/main_inventory.js +++ b/ereuse_devicehub/static/js/main_inventory.js @@ -196,9 +196,9 @@ async function processSelectedDevices() { * Manage the actions that will be performed when applying the changes * @param {EventSource} ev event (Should be a checkbox type) * @param {Lot} lot lot id - * @param {Device[]} deviceList device id + * @param {Device[]} selectedDevices device id */ - manage(event, lot, deviceListID) { + manage(event, lot, selectedDevices) { event.preventDefault(); const lotID = lot.id; const srcElement = event.srcElement.parentElement.children[0] @@ -210,13 +210,13 @@ async function processSelectedDevices() { if (found && found.type == "Remove") { found.type = "Add"; } else { - this.list.push({ type: "Add", lot, devices: deviceListID}); + this.list.push({ type: "Add", lot, devices: selectedDevices }); } } else if (found && found.type == "Add") { - found.type = "Remove"; - } else { - this.list.push({ type: "Remove", lot, devices: deviceListID}); - } + found.type = "Remove"; + } else { + this.list.push({ type: "Remove", lot, devices: selectedDevices }); + } if (this.list.length > 0) { document.getElementById("ApplyDeviceLots").classList.remove("disabled"); @@ -257,14 +257,14 @@ async function processSelectedDevices() { this.list.forEach(async action => { if (action.type == "Add") { try { - await Api.devices_add(action.lot.id, action.devices.map(dev => dev.data)); + await Api.devices_add(action.lot.id, action.devices.map(dev => dev.id)); this.notifyUser("Devices sucefully aded to selected lot/s", "", false); } catch (error) { this.notifyUser("Failed to add devices to selected lot/s", error.responseJSON.message, true); } } else if (action.type == "Remove") { try { - await Api.devices_remove(action.lot.id, action.devices.map(dev => dev.data)); + await Api.devices_remove(action.lot.id, action.devices.map(dev => dev.id)); this.notifyUser("Devices sucefully removed from selected lot/s", "", false); } catch (error) { this.notifyUser("Fail to remove devices from selected lot/s", error.responseJSON.message, true); @@ -302,14 +302,14 @@ async function processSelectedDevices() { /** * Generates a list item with a correspondient checkbox state - * @param {String} lotID - * @param {String} lotName - * @param {Array} selectedDevicesIDs - * @param {HTMLElement} target + * @param {Object} lot Lot model server + * @param {Device[]} selectedDevices list selected devices + * @param {HTMLElement} elementTarget + * @param {Action[]} actions */ - function templateLot(lot, elementTarget, actions) { + function templateLot(lot, selectedDevices, elementTarget, actions) { elementTarget.innerHTML = "" - const {id, name, state} = lot; + const { id, name, state } = lot; const htmlTemplate = ` `; @@ -340,14 +340,9 @@ async function processSelectedDevices() { const listHTML = $("#LotsSelector") // Get selected devices - const selectedDevices = table.rows().dt.activeRows.filter(item => item.querySelector("input").checked).map(item => { - const child = item.childNodes[0].children[0] - const info = {} - Object.values(child.attributes).forEach(attrib => { info[attrib.nodeName] = attrib.nodeValue }) - return info - }) + const selectedDevicesID = table.rows().dt.activeRows.filter(item => item.querySelector("input").checked).map(item => item.querySelector("input").attributes.data.value) - if (selectedDevices.length <= 0) { + if (selectedDevicesID.length <= 0) { listHTML.html("
  • No devices selected
  • "); return; } @@ -362,11 +357,11 @@ async function processSelectedDevices() { try { listHTML.html("
  • ") - const devices = await Api.get_devices(selectedDevices.map(dev => dev.data)); + const selectedDevices = await Api.get_devices(selectedDevicesID); let lots = await Api.get_lots(); lots = lots.map(lot => { - lot.devices = devices + lot.devices = selectedDevices .filter(device => device.lots.filter(devicelot => devicelot.id == lot.id).length > 0) .map(device => parseInt(device.id)); @@ -374,7 +369,7 @@ async function processSelectedDevices() { case 0: lot.state = "false"; break; - case selectedDevices.length: + case selectedDevicesID.length: lot.state = "true"; break; default: @@ -385,7 +380,6 @@ async function processSelectedDevices() { return lot; }) - let lotsList = []; lotsList.push(lots.filter(lot => lot.state == "true").sort((a,b) => a.name.localeCompare(b.name))); lotsList.push(lots.filter(lot => lot.state == "indetermined").sort((a,b) => a.name.localeCompare(b.name))); @@ -393,7 +387,7 @@ async function processSelectedDevices() { lotsList = lotsList.flat(); // flat array listHTML.html(""); - lotsList.forEach(lot => templateLot(lot, listHTML, actions)); + lotsList.forEach(lot => templateLot(lot, selectedDevices, listHTML, actions)); } catch (error) { console.log(error); listHTML.html("
  • Error feching devices and lots
    (see console for more details)
  • "); From 962f7cb94cc20da6fca4aea62174fb4455ec41bd Mon Sep 17 00:00:00 2001 From: RubenPX Date: Thu, 28 Apr 2022 18:29:39 +0200 Subject: [PATCH 2/2] lint --- ereuse_devicehub/static/js/main_inventory.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/ereuse_devicehub/static/js/main_inventory.js b/ereuse_devicehub/static/js/main_inventory.js index 3859c731..380bb962 100644 --- a/ereuse_devicehub/static/js/main_inventory.js +++ b/ereuse_devicehub/static/js/main_inventory.js @@ -163,7 +163,7 @@ function get_device_list() { if (computer[typ]) { typ = computer[typ]; }; - return `${typ } ${ manuf } ${ dhid}`; + return `${typ} ${manuf} ${dhid}`; }); description = $.map(list_devices, (x) => x).join(", "); @@ -173,7 +173,7 @@ function get_device_list() { function export_file(type_file) { const devices = $(".deviceSelect").filter(":checked"); const devices_id = $.map(devices, (x) => $(x).attr("data-device-dhid")).join(","); - if (devices_id){ + if (devices_id) { const url = `/inventory/export/${type_file}/?ids=${devices_id}`; window.location.href = url; } else { @@ -233,7 +233,7 @@ async function processSelectedDevices() { */ notifyUser(title, toastText, isError) { const toast = document.createElement("div"); - toast.classList = `alert alert-dismissible fade show ${ isError ? "alert-danger" : "alert-success"}`; + toast.classList = `alert alert-dismissible fade show ${isError ? "alert-danger" : "alert-success"}`; toast.attributes["data-autohide"] = !isError; toast.attributes.role = "alert"; toast.style = "margin-left: auto; width: fit-content;"; @@ -381,9 +381,9 @@ async function processSelectedDevices() { }) let lotsList = []; - lotsList.push(lots.filter(lot => lot.state == "true").sort((a,b) => a.name.localeCompare(b.name))); - lotsList.push(lots.filter(lot => lot.state == "indetermined").sort((a,b) => a.name.localeCompare(b.name))); - lotsList.push(lots.filter(lot => lot.state == "false").sort((a,b) => a.name.localeCompare(b.name))); + lotsList.push(lots.filter(lot => lot.state == "true").sort((a, b) => a.name.localeCompare(b.name))); + lotsList.push(lots.filter(lot => lot.state == "indetermined").sort((a, b) => a.name.localeCompare(b.name))); + lotsList.push(lots.filter(lot => lot.state == "false").sort((a, b) => a.name.localeCompare(b.name))); lotsList = lotsList.flat(); // flat array listHTML.html("");