diff --git a/src/frontend/src/components/barcodes/BarcodeScanDialog.tsx b/src/frontend/src/components/barcodes/BarcodeScanDialog.tsx index ea4911f336..8ce74cccbc 100644 --- a/src/frontend/src/components/barcodes/BarcodeScanDialog.tsx +++ b/src/frontend/src/components/barcodes/BarcodeScanDialog.tsx @@ -13,13 +13,27 @@ import { useUserState } from '../../states/UserState'; import { StylishText } from '../items/StylishText'; import { BarcodeInput } from './BarcodeInput'; +export type BarcodeScanResult = { + success: boolean; + error: string; +}; + +// Callback function for handling a barcode scan +// This function should return true if the barcode was handled successfully +export type BarcodeScanCallback = ( + barcode: string, + response: any +) => Promise; + export default function BarcodeScanDialog({ title, opened, + callback, onClose }: Readonly<{ title?: string; opened: boolean; + callback?: BarcodeScanCallback; onClose: () => void; }>) { const navigate = useNavigate(); @@ -38,15 +52,21 @@ export default function BarcodeScanDialog({ ); } + export function ScanInputHandler({ + callback, onClose, navigate -}: Readonly<{ onClose: () => void; navigate: NavigateFunction }>) { +}: Readonly<{ + callback?: BarcodeScanCallback; + onClose: () => void; + navigate: NavigateFunction; +}>) { const [error, setError] = useState(''); const [processing, setProcessing] = useState(false); const user = useUserState(); - const onScan = useCallback((barcode: string) => { + const defaultScan = useCallback((barcode: string) => { if (!barcode || barcode.length === 0) { return; } @@ -97,5 +117,31 @@ export function ScanInputHandler({ }); }, []); + const onScan = useCallback( + (barcode: string) => { + if (callback) { + // If a callback is provided, use it to handle the scan + setProcessing(true); + setError(''); + + callback(barcode, {}) + .then((result) => { + if (result.success) { + onClose(); + } else { + setError(result.error); + } + }) + .finally(() => { + setProcessing(false); + }); + } else { + // If no callback is provided, use the default scan function + defaultScan(barcode); + } + }, + [callback, defaultScan] + ); + return ; }