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
* @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,12 +210,12 @@ 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});
this.list.push({ type: "Remove", lot, devices: selectedDevices });
}
if (this.list.length > 0) {
@ -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<number>} 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 = `<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>`;
@ -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("<li style=\"color: red; text-align: center\">No devices selected</li>");
return;
}
@ -362,11 +357,11 @@ async function processSelectedDevices() {
try {
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();
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("<li style=\"color: red; text-align: center\">Error feching devices and lots<br>(see console for more details)</li>");