mirror of
https://github.com/inventree/InvenTree.git
synced 2025-06-16 20:15:44 +00:00
Provide callback function for barcode scan dialog
This commit is contained in:
@ -13,13 +13,27 @@ import { useUserState } from '../../states/UserState';
|
|||||||
import { StylishText } from '../items/StylishText';
|
import { StylishText } from '../items/StylishText';
|
||||||
import { BarcodeInput } from './BarcodeInput';
|
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<BarcodeScanResult>;
|
||||||
|
|
||||||
export default function BarcodeScanDialog({
|
export default function BarcodeScanDialog({
|
||||||
title,
|
title,
|
||||||
opened,
|
opened,
|
||||||
|
callback,
|
||||||
onClose
|
onClose
|
||||||
}: Readonly<{
|
}: Readonly<{
|
||||||
title?: string;
|
title?: string;
|
||||||
opened: boolean;
|
opened: boolean;
|
||||||
|
callback?: BarcodeScanCallback;
|
||||||
onClose: () => void;
|
onClose: () => void;
|
||||||
}>) {
|
}>) {
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
@ -38,15 +52,21 @@ export default function BarcodeScanDialog({
|
|||||||
</Modal>
|
</Modal>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function ScanInputHandler({
|
export function ScanInputHandler({
|
||||||
|
callback,
|
||||||
onClose,
|
onClose,
|
||||||
navigate
|
navigate
|
||||||
}: Readonly<{ onClose: () => void; navigate: NavigateFunction }>) {
|
}: Readonly<{
|
||||||
|
callback?: BarcodeScanCallback;
|
||||||
|
onClose: () => void;
|
||||||
|
navigate: NavigateFunction;
|
||||||
|
}>) {
|
||||||
const [error, setError] = useState<string>('');
|
const [error, setError] = useState<string>('');
|
||||||
const [processing, setProcessing] = useState<boolean>(false);
|
const [processing, setProcessing] = useState<boolean>(false);
|
||||||
const user = useUserState();
|
const user = useUserState();
|
||||||
|
|
||||||
const onScan = useCallback((barcode: string) => {
|
const defaultScan = useCallback((barcode: string) => {
|
||||||
if (!barcode || barcode.length === 0) {
|
if (!barcode || barcode.length === 0) {
|
||||||
return;
|
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 <BarcodeInput onScan={onScan} error={error} processing={processing} />;
|
return <BarcodeInput onScan={onScan} error={error} processing={processing} />;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user