Refactor tractor

This commit is contained in:
Oliver Walters 2021-01-28 22:24:06 +11:00
parent 5e5bced0c7
commit c61631a380

View File

@ -45,6 +45,61 @@ function makeNotesField(options={}) {
} }
/*
* POST data to the server, and handle standard responses.
*/
function postBarcodeData(barcode_data, options={}) {
var modal = options.modal || '#modal-form';
var url = options.url || '/api/barcode/';
var data = options.data || {};
data.barcode = barcode_data;
inventreePut(
url,
data,
{
method: 'POST',
error: function() {
enableBarcodeInput(modal, true);
showBarcodeMessage(modal, '{% trans "Server error" %}');
},
success: function(response, status) {
modalEnable(modal, false);
enableBarcodeInput(modal, true);
if (status == 'success') {
if ('success' in response) {
if (options.onScan) {
options.onScan(response);
}
} else if ('error' in response) {
showBarcodeMessage(
modal,
response.error,
'warning'
);
} else {
showBarcodeMessage(
modal,
'{% trans "Unknown response from server" %}',
'warning'
);
}
} else {
// Invalid response returned from server
showInvalidResponseError(modal, response, status);
}
}
}
)
}
function showBarcodeMessage(modal, message, style='danger') { function showBarcodeMessage(modal, message, style='danger') {
var html = `<div class='alert alert-block alert-${style}'>`; var html = `<div class='alert alert-block alert-${style}'>`;
@ -106,9 +161,7 @@ function barcodeDialog(title, options={}) {
if (barcode && barcode.length > 0) { if (barcode && barcode.length > 0) {
if (options.onScan) { postBarcodeData(barcode, options);
options.onScan(barcode);
}
} }
} }
@ -208,40 +261,20 @@ function barcodeScanDialog() {
barcodeDialog( barcodeDialog(
"Scan Barcode", "Scan Barcode",
{ {
onScan: function(barcode) { onScan: function(response) {
enableBarcodeInput(modal, false);
inventreePut(
'/api/barcode/',
{
barcode: barcode,
},
{
method: 'POST',
success: function(response, status) {
enableBarcodeInput(modal, true);
if (status == 'success') {
if ('success' in response) {
if ('url' in response) { if ('url' in response) {
// Redirect to the URL!
$(modal).modal('hide'); $(modal).modal('hide');
window.location.href = response.url;
}
} else if ('error' in response) { // Redirect to the URL!
showBarcodeMessage(modal, response.error, 'warning'); window.location.href = response.url;
} else { } else {
showBarcodeMessage(modal, "{% trans 'Unknown response from server' %}", 'warning'); showBarcodeMessage(
} modal,
} else { '{% trans "No URL in response" %}',
showInvalidResponseError(modal, response, status); 'warning'
}
},
},
); );
}, }
}
}, },
); );
} }
@ -257,37 +290,14 @@ function linkBarcodeDialog(stockitem, options={}) {
barcodeDialog( barcodeDialog(
"{% trans 'Link Barcode to Stock Item' %}", "{% trans 'Link Barcode to Stock Item' %}",
{ {
onScan: function(barcode) { url: '/api/barcode/link/',
enableBarcodeInput(modal, false); data: {
inventreePut(
'/api/barcode/link/',
{
barcode: barcode,
stockitem: stockitem, stockitem: stockitem,
}, },
{ onScan: function(response) {
method: 'POST',
success: function(response, status) {
enableBarcodeInput(modal, true);
if (status == 'success') {
if ('success' in response) {
$(modal).modal('hide'); $(modal).modal('hide');
location.reload(); location.reload();
} else if ('error' in response) {
showBarcodeMessage(modal, response.error, 'warning');
} else {
showBarcodeMessage(modal, "{% trans 'Unknown response from server' %}", warning);
}
} else {
showInvalidResponseError(modal, response, status);
}
},
},
);
} }
} }
); );
@ -458,24 +468,7 @@ function barcodeCheckIn(location_id, options={}) {
} }
); );
}, },
onScan: function(barcode) { onScan: function(response) {
enableBarcodeInput(modal, false);
inventreePut(
'/api/barcode/',
{
barcode: barcode,
},
{
method: 'POST',
error: function() {
enableBarcodeInput(modal, true);
showBarcodeMessage(modal, '{% trans "Server error" %}');
},
success: function(response, status) {
enableBarcodeInput(modal, true);
if (status == 'success') {
if ('stockitem' in response) { if ('stockitem' in response) {
stockitem = response.stockitem; stockitem = response.stockitem;
@ -508,12 +501,6 @@ function barcodeCheckIn(location_id, options={}) {
// Barcode does not match a stock item // Barcode does not match a stock item
showBarcodeMessage(modal, '{% trans "Barcode does not match Stock Item" %}', "warning"); showBarcodeMessage(modal, '{% trans "Barcode does not match Stock Item" %}', "warning");
} }
} else {
showInvalidResponseError(modal, response, status);
}
},
},
);
}, },
} }
); );
@ -605,25 +592,8 @@ function scanItemsIntoLocation(item_id_list, options={}) {
} }
) )
}, },
onScan: function(barcode) { onScan: function(response) {
updateLocationInfo(null); updateLocationInfo(null);
enableBarcodeInput(modal, false);
inventreePut(
'/api/barcode/',
{
barcode: barcode,
},
{
method: 'POST',
error: function() {
enableBarcodeInput(modal, true);
showBarcodeMessage(modal, '{% trans "Server error" %}');
},
success: function(response, status) {
modalEnable(modal, false);
enableBarcodeInput(modal, true);
if (status == 'success') {
if ('stocklocation' in response) { if ('stocklocation' in response) {
// Barcode corresponds to a StockLocation // Barcode corresponds to a StockLocation
stock_location = response.stocklocation; stock_location = response.stocklocation;
@ -639,13 +609,6 @@ function scanItemsIntoLocation(item_id_list, options={}) {
"warning", "warning",
); );
} }
} else {
// Invalid response returned from server
showInvalidResponseError(modal, response, status);
}
}
}
)
} }
} }
) )