2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-06-17 04:25:42 +00:00

Fix callback processing

This commit is contained in:
Oliver Walters
2025-04-19 10:01:23 +00:00
parent 2a72525d8e
commit a445ad0420

View File

@ -70,79 +70,79 @@ 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; let match = false;
}
setProcessing(true); // Find the matching model type
for (const model_type of Object.keys(ModelInformationDict)) {
api if (data[model_type]?.['pk']) {
.post(apiUrl(ApiEndpoints.barcode), { if (user.hasViewPermission(model_type as ModelType)) {
barcode: barcode const url = getDetailUrl(
}) model_type as ModelType,
.then((response) => { data[model_type]['pk']
setError(''); );
onClose();
const data = response.data ?? {}; navigate(url);
let match = false; match = true;
break;
// Find the matching model type
for (const model_type of Object.keys(ModelInformationDict)) {
if (data[model_type]?.['pk']) {
if (user.hasViewPermission(model_type as ModelType)) {
const url = getDetailUrl(
model_type as ModelType,
data[model_type]['pk']
);
onClose();
navigate(url);
match = true;
break;
}
} }
} }
}
if (!match) { if (!match) {
setError(t`No matching item found`); setError(t`No matching item found`);
} }
}) },
.catch((error) => { [navigate, onClose, user]
const _error = extractErrorMessage({ );
error: error,
field: 'error',
defaultMessage: t`Failed to scan barcode`
});
setError(_error);
})
.finally(() => {
setProcessing(false);
});
}, []);
const onScan = useCallback( const onScan = useCallback(
(barcode: string) => { (barcode: string) => {
if (callback) { if (!barcode || barcode.length === 0) {
// If a callback is provided, use it to handle the scan return;
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);
} }
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) => {
const _error = extractErrorMessage({
error: error,
field: 'error',
defaultMessage: t`Failed to scan barcode`
});
setError(_error);
})
.finally(() => {
setProcessing(false);
});
}, },
[callback, defaultScan] [callback, defaultScan]
); );