import React, { useEffect, useState } from 'react'; import { CBadge, CCardBody, CDataTable, CButton, CLink, CCard, CCardHeader, CRow, CCol, CPopover, } from '@coreui/react'; import ReactPaginate from 'react-paginate'; import Select from 'react-select'; import { cilSync, cilInfo } from '@coreui/icons'; import CIcon from '@coreui/icons-react'; import { getToken } from '../utils/authHelper'; import axiosInstance from '../utils/axiosInstance'; import { cleanBytesString, cropStringWithEllipsis } from '../utils/helper'; import iotIcon from '../assets/icons/iot.png'; import internetSwitch from '../assets/icons/networkswitch.png'; const DeviceList = () => { const [loadedSerials, setLoadedSerials] = useState(false); const [serialNumbers, setSerialNumbers] = useState([]); const [page, setPage] = useState(0); const [pageCount, setPageCount] = useState(0); const [devicesPerPage, setDevicesPerPage] = useState(10); const [devices, setDevices] = useState([]); const [loading, setLoading] = useState(true); const getSerialNumbers = () => { const token = getToken(); setLoading(true); const headers = { Accept: 'application/json', Authorization: `Bearer ${token}`, }; axiosInstance .get('/devices?serialOnly=true', { headers, }) .then((response) => { setSerialNumbers(response.data.serialNumbers); setLoadedSerials(true); }) .catch((error) => { setLoading(false); console.log(error.response); }); }; const getDeviceInformation = () => { const token = getToken(); setLoading(true); const headers = { Accept: 'application/json', Authorization: `Bearer ${token}`, }; const startIndex = page * devicesPerPage; const endIndex = parseInt(startIndex, 10) + parseInt(devicesPerPage, 10); const serialsToGet = serialNumbers.slice(startIndex, endIndex).join(','); axiosInstance .get(`/devices?deviceWithStatus=true&select=${serialsToGet}`, { headers, }) .then((response) => { setDevices(response.data.devicesWithStatus); setLoading(false); }) .catch((error) => { setLoading(false); console.log(error.response); }); }; const updateDevicesPerPage = (value) => { setDevicesPerPage(value); }; const updatePageCount = ({ selected: selectedPage }) => { setPage(selectedPage); }; // Initial load useEffect(() => { getSerialNumbers(); }, []); // Updating the devices only if serial numbers, page number or devices per page changes useEffect(() => { if (loadedSerials) getDeviceInformation(); }, [serialNumbers, page, devicesPerPage, loadedSerials]); useEffect(() => { if (loadedSerials) { const count = Math.ceil(serialNumbers.length / devicesPerPage); setPageCount(count); } }, [devicesPerPage, loadedSerials]); return ( ); }; const DeviceListDisplay = ({ devices, loading, updateDevicesPerPage, pageCount, updatePage }) => { const columns = [ { key: 'deviceType', label: '', filter: false, sorter: false, _style: { width: '5%' } }, { key: 'serialNumber', _style: { width: '5%' } }, { key: 'UUID', label: 'Config Id', _style: { width: '5%' } }, { key: 'firmware', filter: false, _style: { width: '20%' } }, { key: 'manufacturer', filter: false, _style: { width: '20%' } }, { key: 'txBytes', label: 'Tx', filter: false, _style: { width: '10%' } }, { key: 'rxBytes', label: 'Rx', filter: false, _style: { width: '10%' } }, { key: 'ipAddress', _style: { width: '20%' } }, { key: 'show_details', label: '', _style: { width: '3%' }, sorter: false, filter: false, }, { key: 'refresh', label: '', _style: { width: '2%' }, sorter: false, filter: false, }, ]; const selectOptions = [ { value: '10', label: '10' }, { value: '25', label: '25' }, { value: '50', label: '50' }, ]; const getDeviceIcon = (deviceType) => { if (deviceType === 'AP_Default') { return ; } if (deviceType === 'IOT') { return IOT; } if (deviceType === 'SWITCH') { return SWITCH; } return null; }; const getStatusBadge = (status) => { if (status) { return 'success'; } return 'danger'; }; return ( <>