import React, { Component } from "react"; import { API } from "aws-amplify"; import { Button, FormControl, Grid, Row, Col, FormGroup, ControlLabel, ProgressBar, Alert, Table, Modal } from 'react-bootstrap'; import { Card } from "components/Card/Card.jsx"; class Devices extends Component { constructor(props) { super(props); this.handleRegisterDevice = this.handleRegisterDevice.bind(this); this.handleDevice = this.handleDevice.bind(this); this.handleDelete = this.handleDelete.bind(this); this.handleDeleteClose = this.handleDeleteClose.bind(this); this.handleFilter = this.handleFilter.bind(this); this.handleOrderChange = this.handleOrderChange.bind(this); // Sets up initial state this.state = { devices: [], error: false, isLoading: false, show: false, deviceName: '', deviceId: '', isDeleting: false, title: '', }; } componentDidMount() { this.setState({ title: 'My Devices', }); this.getDevices(); } // Registers a device handleRegisterDevice() { this.props.history.push('/devices/registration'); } // Gets a device detail handleDevice(deviceId) { this.props.history.push(`/devices/${deviceId}`); } // Handles to delete a device handleDelete = (deviceId, deviceName) => { this.setState({ deviceName: deviceName, deviceId: deviceId, show: true, }); } // Delets a device deleteDevice = async (deviceId) => { if (!this.state.isDeleting) { this.setState({ isDeleting: true }); let token = await this.props.getToken(); let apiName = 'smart-product-api'; let path = `devices/${deviceId}`; let params = { headers: { 'Authorization': token, }, response: true, }; API.del(apiName, path, params) .then(_response => { this.props.handleNotification('Device was deleted successfully', 'success', 'pe-7s-close-circle', 5); let updatedDevices = this.state.devices.filter(device => device.thingName !== deviceId); this.setState({ devices: updatedDevices, title: `My Devices (${updatedDevices.length})` }); }) .catch(error => { this.props.handleNotification('Error occurred while deleting the device', 'error', 'pe-7s-close-circle', 5); }) .finally(() => { this.setState({ isDeleting: false, show: false, }); }); } else { this.props.handleNotification('Device is still deleting', 'warning', 'pe-7s-close-circle', 5); } } handleDeleteClose = () => { this.setState({ show: false }); } // Handles input changes handleFilter = () => { // Gets element value directly due to the stale state let connected = document.getElementById("status").value; let keyword = document.getElementById("keyword").value; let devices = this.state.devices; for (let i = 0; i < devices.length; i++) { let deviceName = devices[i].attributes.deviceName; let modelNumber = devices[i].attributes.modelNumber; let thingName = devices[i].thingName; let connectivity = devices[i].connectivity.connected ? 'connected' : 'disconnected'; if (keyword === '' && connected === '') { // Empty keyword and All status devices[i].visible = true; } else if (keyword === '') { // Empty keyword and certain status if (connectivity === connected) { devices[i].visible = true; } else { devices[i].visible = false; } } else if (connected === '') { // Some keyword and All status and if (deviceName.indexOf(keyword) > -1 || modelNumber.indexOf(keyword) > -1 || thingName.indexOf(keyword) > -1) { devices[i].visible = true; } else { devices[i].visible = false; } } else { // Some keyword and certain status if (deviceName.indexOf(keyword) > -1 || modelNumber.indexOf(keyword) > -1 || thingName.indexOf(keyword) > -1) { if (connectivity === connected) { devices[i].visible = true; } else { devices[i].visible = false; } } else { devices[i].visible = false; } } } this.setState({ devices: devices }); } handleOrderChange = (event) => { let order = event.target.value; this.sortDevices(order); }; // Sorts devices sortDevices = (order) => { let devices = this.state.devices; if (order === 'asc') { devices.sort((a, b) => a.attributes.deviceName.localeCompare(b.attributes.deviceName)); } else if (order === 'desc') { devices.sort((a, b) => b.attributes.deviceName.localeCompare(a.attributes.deviceName)); } this.setState({ devices: devices }); }; // Gets devices getDevices = async () => { this.setState({ isLoading: true }); let token = await this.props.getToken(); let apiName = 'smart-product-api'; let path = 'devices'; let params = { headers: { 'Authorization': token, }, response: true, }; API.get(apiName, path, params) .then(response => { let devices = response.data; // Adds visible key/value for filter for (let i = 0; i < devices.length; i++) { devices[i]['visible'] = true; } // Sorts initially devices.sort((a, b) => a.attributes.deviceName.localeCompare(b.attributes.deviceName)); this.setState({ devices: devices, title: `My Devices (${devices.length})`, }); }) .catch(error => { let message = error.response; if (message === undefined) { message = error.message; } else { message = error.response.data.message; } this.setState({ error: message, }); }) .finally(() => { this.setState({ isLoading: false, }); }); }; render() { const { isLoading, isDeleting, error, devices, deviceName, title } = this.state; return (
Serial Number |
{device.thingName} |
Model Number |
{device.attributes.modelNumber} |
Connected | {device.connectivity.connected ? "Connected" : "Disconnected"} |