Fetch actual data from the database and create prototype scanner layout.

This commit is contained in:
Dominik Pantůček 2025-04-09 19:03:41 +02:00
parent 186fe2d0ec
commit ae5b9e2775
2 changed files with 57 additions and 86 deletions

View file

@ -4,11 +4,3 @@
min-height: 100vh; min-height: 100vh;
width: 100%; width: 100%;
} }
.card {
padding: 2em;
}
.read-the-docs {
color: #888;
}

View file

@ -1,99 +1,78 @@
import { useState } from 'react'; import { useState } from 'react';
import BarcodeScannerComponent from 'react-qr-barcode-scanner'; import BarcodeScannerComponent from 'react-qr-barcode-scanner';
import toast, { Toaster } from 'react-hot-toast';
import { Container, Row, Col } from 'react-bootstrap'; import { Container, Row, Col } from 'react-bootstrap';
import Table from 'react-bootstrap/Table';
import Alert from 'react-bootstrap/Alert';
// Styles
import './App.css'; import './App.css';
import 'bootstrap/dist/css/bootstrap.min.css'; import 'bootstrap/dist/css/bootstrap.min.css';
function App() { function App() {
// State to store the latest scanned data const [reqAccount, setReqAccount] = useState('');
const [currentData, setCurrentData] = useState('No result'); const [actAccount, setActAccount] = useState('');
const [balance, setBalance] = useState(-1);
// State to store an array of scanned results for history tracking const [statusMsg, setStatusMsg] = useState('');
const [scanHistory, setScanHistory] = useState([]); const [msgType, setMsgType] = useState('info');
/** const handleScan = async (err, result) => {
* Handles the scanning result. if (result) {
* @param {Error} err - Error, if any, from the scanner. const ra = result.text;
* @param {Object} result - The scanning result object. setReqAccount(ra);
*/ setStatusMsg('Loading');
const handleScan = (err, result) => { setMsgType('primary');
if (result) { try {
// Update current data and add it to the scan history const resp = await fetch('/api/barcode/' + ra);
setCurrentData(result.text); if (resp.ok) {
setScanHistory(prevHistory => [...prevHistory, result.text]); const json = await resp.json();
setActAccount(ra);
// Show a success notification using toast setBalance(json.amount);
toast.success(`Product scanned: ${result.text}`); setStatusMsg('OK');
} else { setMsgType('success');
setCurrentData('No result'); // Set message if no result }
} } catch(ex) {
}; setMsgType('danger');
setStatusMsg(ex.message);
}
}
};
return ( return (
<Container> <Container>
<Row> <Row>
<Col> <Col>
{/* Notification toaster for displaying scan success messages */} <BarcodeScannerComponent
<Toaster position="top-right" reverseOrder={false} /> width={"100%"}
height={"auto"}
onUpdate={handleScan}
delay={1500}
/>
</Col> </Col>
</Row> </Row>
<Row> <Row>
<Col> <Col>
<Table>
{/* Scanner title */} <tr>
<div className="title">BrmInv</div> <th>Requested:</th>
</Col> <td>{reqAccount}</td>
</Row>
<Row>
<Col>
{/* Barcode scanner component */}
<div className="scan" style={{border: '1px solid black'}}>
<BarcodeScannerComponent
width={"100%"}
height={"auto"}
onUpdate={handleScan}
delay={1500} // 1.5 seconds delay between scans
/>
</div>
</Col>
</Row>
<Row>
<Col>
{/* Display the current scanned data */}
<p>Scanned Data: {currentData}</p>
</Col>
</Row>
<Row>
<Col>
{/* Table to display scan history */}
<h2>Scan History</h2>
<table>
<thead>
<tr>
<th>#</th>
<th>Scanned Result</th>
</tr>
</thead>
<tbody>
{scanHistory.map((result, index) => (
<tr key={index}>
<td>{index + 1}</td>
<td>{result}</td>
</tr> </tr>
))} <tr>
</tbody> <th>Loaded:</th>
</table> <td>{actAccount}</td>
</tr>
<tr>
<th>Balance:</th>
<td>{balance}</td>
</tr>
</Table>
</Col> </Col>
</Row> </Row>
</Container> <Row>
); <Col>
} <Alert variant={msgType}>{statusMsg}</Alert>
</Col>
</Row>
</Container>
);
}
export default App; export default App;