Newer
Older
smart-home-server / webclient / src / js / components / screens / devices / device-details-popup.js
export function deviceDetailsPopup(device, sh_api) {
	console.log(device);

	return Modals.create("device-popup", {
		title: `Device ${device.name}`,
		body: modal => {
			let deviceProperties = "";
			for(let field in device) {
				deviceProperties += `
					<tr class="table-row">
						<th>${field}: </th>
						<td>${device[field]}</td>
					</tr>
				`;
			}

			return `
				<div class="block">
					<table class="table" style="border: 0">
						<tbody class="table-body">
							${deviceProperties}
						</tbody>
					</table>
				</div>
			`;
		},
		actions: modal => {
			const buttonCancel = Helper.template.createElement("button", {
				class: "btn btn-primary"
			}, "Close");

			const buttonReboot = Helper.template.createElement("button", {
				class: "btn btn-warning with-icon"
			}, '<i class="ph ph-arrow-clockwise"></i> Reboot');

			const buttonRemove = Helper.template.createElement("button", {
				class: "btn btn-danger with-icon"
			}, '<i class="ph ph-trash"></i> Remove');

			buttonCancel.addEventListener("click", e => {
				modal.close();
			});

			buttonReboot.addEventListener("click", e => {
				if(buttonReboot.getAttribute("disabled")) {
					return ;
				}

				Helper.states.btnLoadingState(buttonReboot, true);
				sh_api.devices.reboot(
					device.id,
					(err, data, meta) => {
						Helper.states.btnLoadingState(buttonReboot, false);
						console.log("Reboot done");

						modal.close();

						setTimeout(() => {
							if(data) {
								Toasts.createSuccess(
									"Reboot successful",
									`Device: ${device.name}<br>
									Alias: <b>${device.alias}</b>`
								).show();
							} else {
								Toasts.createError(
									"Reboot failed",
									`Device: ${device.name}<br>
									Alias: <b>${device.alias}</b>`
								).show();
							}
						}, 300);
					}
				);
			});

			buttonRemove.addEventListener("click", e => {
				if(buttonRemove.getAttribute("disabled")) {
					return;
				}

				Helper.states.btnLoadingState(buttonRemove, true);
				confirmPopup(
					"Are you sure you want to remove this device?", 
					() => {
						sh_api.devices.remove(
							device.id,
							(err, data, meta) => {
								
								Helper.states.btnLoadingState(buttonRemove, false);
								console.log("Was removed");

								modal.close();

								setTimeout(() => {
									Toasts.createSuccess(
										"Removed",
										`
										Device: ${device.name}<br>
										Alias: <b>${device.alias}</b><br>
										IP: <b>${device.ip}</b>
										`
									).show();
								}, 300);
							}
						);
				}, 
				() => {
					Helper.states.btnLoadingState(buttonRemove, false);
					console.log("CANCELED");
				});
			});

			return [ buttonCancel, buttonReboot, buttonRemove ];
		}
	});
}