Newer
Older
smart-home-server / webclient / src / js / components / screens / devices / devices-list-screen.js
import { sidebarTemplate, rebootDeviceBtnHandler } from "./devices-funcs.js";
import { deviceDetailsPopup } from "./device-details-popup.js";
import deviceStatusComponent from "./device-status-component.js";

function prepareData(data) {
	const preparedData = [];
	for(let device of data.devices) {
		device = Helper.unification.deviceFieldsUnification(device);

		preparedData.push({
			deviceName: device.name,
			alias: device.alias,
			status: Helper.template.connectionStatusBadge(device.connection_state),
			state: `<div 
				class="device-state-container" 
				data-device-id="${device.id}"
				data-device-type="${device.type}"
			></div>`,
			ip: `<code class="code">${device.ip}</code>`,
			actions: `
				<div class="f-grid g-2">
					<button 
						class="btn btn-secondary btn-small details-btn" 
						data-device='${JSON.stringify(device)}'
						type="button"
					>Details</button>

					<button 
						class="btn btn-warning btn-small reboot-btn" 
						data-device-id="${device.id}" 
						data-device-name="${device.name}" 
						data-device-alias="${device.alias}" 
						type="button"
					>Reboot</button>
				</div>
			`
		});
	}

	return preparedData;
}

function renderingMainTable(scr, data, total) {
	scr.currentScreen.DOMObject.querySelector(".main-container").innerHTML = Helper.template.table(
		"Devices list", 
		{
			deviceName: "Device name", 
			status: "Status",
			state: "State",
			ip: "IP", 
			actions: "Actions"
		},
		data,
		`<span class="table-meta">Total: <span class="total">${total}</span> devices</span>`
	);
}

function initMainTableInteractiveElements(scr, sh_api) {
	scr.currentScreen.DOMObject.querySelectorAll(".reboot-btn").forEach(btn => {
		btn.addEventListener("click", e => {
			if(e.currentTarget.getAttribute("disabled")) {
				return ;
			}

			rebootDeviceBtnHandler(sh_api, e.currentTarget);
		});
	});

	scr.currentScreen.DOMObject.querySelectorAll(".details-btn").forEach(btn => {
		btn.addEventListener("click", e => {
			const device = {
				status: "",
				...JSON.parse(e.currentTarget.dataset.device)
			}
			deviceDetailsPopup(device, sh_api).show();
		});
	});
}

function list(sh_api) {
	return {
		alias: "devices",
		renderer: () => {
			return Helper.template.mainTemplate(sidebarTemplate("devices"));
		},

		initer: scr => {
			sh_api.devices.list((err, resp, meta) => {
				console.log("sh_api.devices.list", err, resp, meta);

				if(meta.status_code != 200) {
					return scr.error("Server API ERROR", "");
				}

				renderingMainTable(scr, prepareData(resp.data), resp.data.total);
				initMainTableInteractiveElements(scr, sh_api);

				scr.currentScreen.DOMObject.querySelectorAll(".device-state-container").forEach(container => {
					container.append(deviceStatusComponent(
						sh_api, 
						container.dataset.deviceId, 
						container.dataset.deviceType
					));
				});

				scr.ready();
			});
		}
	};
}

export {
	list
}