Send device model through the methods

This commit is contained in:
RubenPX 2022-04-28 18:29:18 +02:00
parent 8d8ce63402
commit c824110b00
1 changed files with 21 additions and 27 deletions

View File

@ -196,9 +196,9 @@ async function processSelectedDevices() {
* Manage the actions that will be performed when applying the changes * Manage the actions that will be performed when applying the changes
* @param {EventSource} ev event (Should be a checkbox type) * @param {EventSource} ev event (Should be a checkbox type)
* @param {Lot} lot lot id * @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(); event.preventDefault();
const lotID = lot.id; const lotID = lot.id;
const srcElement = event.srcElement.parentElement.children[0] const srcElement = event.srcElement.parentElement.children[0]
@ -210,12 +210,12 @@ async function processSelectedDevices() {
if (found && found.type == "Remove") { if (found && found.type == "Remove") {
found.type = "Add"; found.type = "Add";
} else { } else {
this.list.push({ type: "Add", lot, devices: deviceListID}); this.list.push({ type: "Add", lot, devices: selectedDevices });
} }
} else if (found && found.type == "Add") { } else if (found && found.type == "Add") {
found.type = "Remove"; found.type = "Remove";
} else { } else {
this.list.push({ type: "Remove", lot, devices: deviceListID}); this.list.push({ type: "Remove", lot, devices: selectedDevices });
} }
if (this.list.length > 0) { if (this.list.length > 0) {
@ -257,14 +257,14 @@ async function processSelectedDevices() {
this.list.forEach(async action => { this.list.forEach(async action => {
if (action.type == "Add") { if (action.type == "Add") {
try { 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); this.notifyUser("Devices sucefully aded to selected lot/s", "", false);
} catch (error) { } catch (error) {
this.notifyUser("Failed to add devices to selected lot/s", error.responseJSON.message, true); this.notifyUser("Failed to add devices to selected lot/s", error.responseJSON.message, true);
} }
} else if (action.type == "Remove") { } else if (action.type == "Remove") {
try { 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); this.notifyUser("Devices sucefully removed from selected lot/s", "", false);
} catch (error) { } catch (error) {
this.notifyUser("Fail to remove devices from selected lot/s", error.responseJSON.message, true); 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 * Generates a list item with a correspondient checkbox state
* @param {String} lotID * @param {Object} lot Lot model server
* @param {String} lotName * @param {Device[]} selectedDevices list selected devices
* @param {Array<number>} selectedDevicesIDs * @param {HTMLElement} elementTarget
* @param {HTMLElement} target * @param {Action[]} actions
*/ */
function templateLot(lot, elementTarget, actions) { function templateLot(lot, selectedDevices, elementTarget, actions) {
elementTarget.innerHTML = "" elementTarget.innerHTML = ""
const {id, name, state} = lot; const { id, name, state } = lot;
const htmlTemplate = `<input class="form-check-input" type="checkbox" id="${id}" style="width: 20px; height: 20px; margin-right: 7px;"> const htmlTemplate = `<input class="form-check-input" type="checkbox" id="${id}" style="width: 20px; height: 20px; margin-right: 7px;">
<label class="form-check-label" for="${id}">${name}</label>`; <label class="form-check-label" for="${id}">${name}</label>`;
@ -340,14 +340,9 @@ async function processSelectedDevices() {
const listHTML = $("#LotsSelector") const listHTML = $("#LotsSelector")
// Get selected devices // Get selected devices
const selectedDevices = table.rows().dt.activeRows.filter(item => item.querySelector("input").checked).map(item => { const selectedDevicesID = table.rows().dt.activeRows.filter(item => item.querySelector("input").checked).map(item => item.querySelector("input").attributes.data.value)
const child = item.childNodes[0].children[0]
const info = {}
Object.values(child.attributes).forEach(attrib => { info[attrib.nodeName] = attrib.nodeValue })
return info
})
if (selectedDevices.length <= 0) { if (selectedDevicesID.length <= 0) {
listHTML.html("<li style=\"color: red; text-align: center\">No devices selected</li>"); listHTML.html("<li style=\"color: red; text-align: center\">No devices selected</li>");
return; return;
} }
@ -362,11 +357,11 @@ async function processSelectedDevices() {
try { try {
listHTML.html("<li style=\"text-align: center\"><div class=\"spinner-border text-info\" style=\"margin: auto\" role=\"status\"></div></li>") listHTML.html("<li style=\"text-align: center\"><div class=\"spinner-border text-info\" style=\"margin: auto\" role=\"status\"></div></li>")
const devices = await Api.get_devices(selectedDevices.map(dev => dev.data)); const selectedDevices = await Api.get_devices(selectedDevicesID);
let lots = await Api.get_lots(); let lots = await Api.get_lots();
lots = lots.map(lot => { lots = lots.map(lot => {
lot.devices = devices lot.devices = selectedDevices
.filter(device => device.lots.filter(devicelot => devicelot.id == lot.id).length > 0) .filter(device => device.lots.filter(devicelot => devicelot.id == lot.id).length > 0)
.map(device => parseInt(device.id)); .map(device => parseInt(device.id));
@ -374,7 +369,7 @@ async function processSelectedDevices() {
case 0: case 0:
lot.state = "false"; lot.state = "false";
break; break;
case selectedDevices.length: case selectedDevicesID.length:
lot.state = "true"; lot.state = "true";
break; break;
default: default:
@ -385,7 +380,6 @@ async function processSelectedDevices() {
return lot; return lot;
}) })
let lotsList = []; 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 == "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 == "indetermined").sort((a,b) => a.name.localeCompare(b.name)));
@ -393,7 +387,7 @@ async function processSelectedDevices() {
lotsList = lotsList.flat(); // flat array lotsList = lotsList.flat(); // flat array
listHTML.html(""); listHTML.html("");
lotsList.forEach(lot => templateLot(lot, listHTML, actions)); lotsList.forEach(lot => templateLot(lot, selectedDevices, listHTML, actions));
} catch (error) { } catch (error) {
console.log(error); console.log(error);
listHTML.html("<li style=\"color: red; text-align: center\">Error feching devices and lots<br>(see console for more details)</li>"); listHTML.html("<li style=\"color: red; text-align: center\">Error feching devices and lots<br>(see console for more details)</li>");