mirror of
https://github.com/inventree/InvenTree.git
synced 2025-06-18 04:55:44 +00:00
Fix callback processing
This commit is contained in:
@ -70,21 +70,8 @@ export function ScanInputHandler({
|
|||||||
const [processing, setProcessing] = useState<boolean>(false);
|
const [processing, setProcessing] = useState<boolean>(false);
|
||||||
const user = useUserState();
|
const user = useUserState();
|
||||||
|
|
||||||
const defaultScan = useCallback((barcode: string) => {
|
const defaultScan = useCallback(
|
||||||
if (!barcode || barcode.length === 0) {
|
(data: any) => {
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
setProcessing(true);
|
|
||||||
|
|
||||||
api
|
|
||||||
.post(apiUrl(ApiEndpoints.barcode), {
|
|
||||||
barcode: barcode
|
|
||||||
})
|
|
||||||
.then((response) => {
|
|
||||||
setError('');
|
|
||||||
|
|
||||||
const data = response.data ?? {};
|
|
||||||
let match = false;
|
let match = false;
|
||||||
|
|
||||||
// Find the matching model type
|
// Find the matching model type
|
||||||
@ -106,6 +93,43 @@ export function ScanInputHandler({
|
|||||||
if (!match) {
|
if (!match) {
|
||||||
setError(t`No matching item found`);
|
setError(t`No matching item found`);
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
[navigate, onClose, user]
|
||||||
|
);
|
||||||
|
|
||||||
|
const onScan = useCallback(
|
||||||
|
(barcode: string) => {
|
||||||
|
if (!barcode || barcode.length === 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
setProcessing(true);
|
||||||
|
setError('');
|
||||||
|
|
||||||
|
api
|
||||||
|
.post(apiUrl(ApiEndpoints.barcode), {
|
||||||
|
barcode: barcode
|
||||||
|
})
|
||||||
|
.then((response: any) => {
|
||||||
|
const data = response.data ?? {};
|
||||||
|
|
||||||
|
if (callback && data.success && response.status === 200) {
|
||||||
|
callback(barcode, data)
|
||||||
|
.then((result: BarcodeScanResult) => {
|
||||||
|
if (result.success) {
|
||||||
|
onClose();
|
||||||
|
} else {
|
||||||
|
setError(result.error);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.finally(() => {
|
||||||
|
setProcessing(false);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
// If no callback is provided, use the default scan function
|
||||||
|
defaultScan(data);
|
||||||
|
setProcessing(false);
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
const _error = extractErrorMessage({
|
const _error = extractErrorMessage({
|
||||||
@ -119,30 +143,6 @@ export function ScanInputHandler({
|
|||||||
.finally(() => {
|
.finally(() => {
|
||||||
setProcessing(false);
|
setProcessing(false);
|
||||||
});
|
});
|
||||||
}, []);
|
|
||||||
|
|
||||||
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]
|
[callback, defaultScan]
|
||||||
);
|
);
|
||||||
|
Reference in New Issue
Block a user