Files
hpos-web/src/components/Devices.js

150 lines
6.0 KiB
JavaScript
Raw Normal View History

import React, { useEffect, useState } from 'react';
import { query, collection, onSnapshot, getDocs } from 'firebase/firestore';
import * as _ from 'lodash';
import Dropdown from 'react-dropdown';
import 'react-dropdown/style.css';
import moment from 'moment';
2023-07-22 15:14:14 +05:30
import Select from 'react-select';
import DeviceGraph from "./DeviceGraph";
import db from '../firebase';
import './Devices.css';
2023-07-22 15:14:14 +05:30
const styles = {
menuList: (base) => ({
...base,
"::-webkit-scrollbar": {
width: "4px",
height: "0px",
},
"::-webkit-scrollbar-track": {
background: "#f1f1f1"
},
"::-webkit-scrollbar-thumb": {
background: "#888"
},
"::-webkit-scrollbar-thumb:hover": {
background: "#555"
}
})
};
const Devices = () => {
const [device, setDevice] = useState("Select a device");
const [testDate, setTestDate] = useState("Select a test date");
const [curTest, setCurTest] = useState("Select a test");
const [curTestData, setCurTestData] = useState(null);
const [devices, setDevices] = useState([]);
const [days, setDays] = useState([]);
const [tests, setTests] = useState([]);
const [allTests, setAllTests] = useState(null);
2023-07-22 13:51:20 +05:30
const [showGraph, setShowGraph] = useState(false);
const changeDevice = ({ value }) => {
console.log(value);
2023-07-22 14:33:57 +05:30
// console.log(event);
setDevice(value);
setDays([...new Set(allTests
.filter((x) => {
2023-07-22 14:33:57 +05:30
if (x.deviceSerialNumber === value) return x;
})
2023-07-22 13:51:20 +05:30
.map(x => moment(x.testTime).format("YYYY-MM-DD")))]
.sort()
.reverse());
}
const changeTestDate = ({ value }) => {
setTestDate(value);
setCurTest(null);
setCurTestData(null);
2023-07-22 13:51:20 +05:30
setShowGraph(false);
setTests(allTests
.filter((x) => {
2023-07-22 14:33:57 +05:30
if (x.deviceSerialNumber === device && moment(x.testTime).isBetween(moment(value).startOf('day'), moment(value).endOf('day'))) return x;
})
2023-07-22 13:51:20 +05:30
.map(x => x._id)
.sort());
}
const changeTest = ({ value }) => {
2023-07-22 13:51:20 +05:30
setCurTest(null); setCurTestData(null); setShowGraph(false);
if (curTest) alert('Click clear button first');
setCurTest(value);
setCurTestData(allTests.find(x => x._id === value));
2023-07-22 13:51:20 +05:30
setShowGraph(true);
}
2023-07-22 15:14:14 +05:30
const menuPortalTarget = document.getElementById('root');
useEffect(() => {
2023-07-22 14:33:57 +05:30
if (!allTests) {
const q = query(collection(db, "testData"));
2023-07-22 14:33:57 +05:30
const unsub = onSnapshot(q, (querySnapshot) => {
const testDevices = new Set();
const hposTests = [];
querySnapshot.forEach((document) => {
const hposTestData = document.data();
hposTests.push(hposTestData);
const { deviceSerialNumber } = hposTestData;
testDevices.add(deviceSerialNumber);
// console.log(document.data());
});
const groups = [...testDevices];
// console.log(groups);
setDevices(groups);
setAllTests(hposTests);
});
2023-07-22 14:33:57 +05:30
return () => unsub;
}
}, [curTestData, curTest, device, testDate]);
return (
<div>
<div className='title'>Insights</div>
<div className='dropdown-group'>
2023-07-22 13:51:20 +05:30
<Dropdown className='device-dropdown' options={devices} onChange={changeDevice} value={device} placeholder="Select an device" />
<Dropdown className='day-dropdown' options={days} onChange={changeTestDate} value={testDate} placeholder="Select an test date" />
<Dropdown className='test-dropdown' options={tests} onChange={changeTest} value={curTest} placeholder="Select an test" />
2023-07-22 15:14:14 +05:30
{/* <Select className='device-dropdown' options={devices.map(x => { return { value: x, label: x }; })} menuPlacement='auto' maxMenuHeight={250} menuShouldScrollIntoView={true} onChange={changeDevice} />
<Select className='day-dropdown' options={days.map(x => { return { value: x, label: x }; })} menuPlacement='auto' maxMenuHeight={250} menuShouldScrollIntoView={true} onChange={changeTestDate} />
<Select className='test-dropdown' options={tests.map(x => { return { value: x, label: x }; })} menuPlacement='auto' maxMenuHeight={220} menuShouldScrollIntoView={true} onChange={changeTestDate} /> */}
<button className='clear-button' onClick={() => { setCurTest(null); setCurTestData(null); }}>Clear</button>
</div>
<div className='graph-person-conatiner'>
{!curTestData && <div className='refresh-graph-message'>Select test device, date and person to get the detailed graph. Then, refresh or click clear button to render another entry.</div>}
2023-07-22 13:51:20 +05:30
{curTestData?.csvPath && <DeviceGraph url={curTestData?.csvPath} show={showGraph} />}
{curTestData && <div className='person-details'>
<table>
<tr>
<th>key</th>
<th>value</th>
</tr>
<tr>
2023-07-22 13:51:20 +05:30
<td>device</td> <td>{curTestData?.deviceId}</td>
</tr>
<tr>
2023-07-22 13:51:20 +05:30
<td>kit</td> <td>{curTestData?.kitSerial}</td>
</tr>
<tr>
<td>test time</td> <td>{curTestData?.testTime}</td>
</tr>
<tr>
<td>result</td> <td>{curTestData?.result}</td>
</tr>
</table>
{/* <img src={curTestData?.userImageURL} width={100} height={150} /> */}
</div>}
</div>
</div>
)
}
export default Devices;