Fixed API call and added error message parsing

This commit is contained in:
Marc Ole Bulling 2021-01-15 12:16:22 +01:00
parent 8e0b995808
commit 5aa562b5e9
No known key found for this signature in database
GPG Key ID: C126AFC2A47B06FF

View File

@ -428,55 +428,72 @@ $(document).on("click", "#print-shopping-list-button", function(e)
</label> \ </label> \
</div>'; </div>';
var sizePrintDialog = 'medium';
var printButtons = { var printButtons = {
cancel: { cancel: {
label: __t('Cancel'), label: __t('Cancel'),
className: 'btn-secondary', className: 'btn-secondary',
callback: function() callback: function () {
{
bootbox.hideAll(); bootbox.hideAll();
} }
}, },
printtp: { printtp: {
label: __t('Thermal printer'), label: __t('Thermal printer'),
className: 'btn-secondary', className: 'btn-secondary',
callback: function() callback: function () {
{ bootbox.hideAll();
Grocy.Api.Get('print/shoppinglist/thermal', { "list": $("#selected-shopping-list").val(), var thermalPrintDialog = bootbox.dialog({
"printHeader": $("#print-show-header").prop("checked") }, title: __t('Printing'),
function(result) message: '<p><i class="fa fa-spin fa-spinner"></i> ' + __t('Connecting to printer...') + '</p>'
{ });
bootbox.hideAll(); //Delaying for one second so that the alert can be closed
}, setTimeout(function ()
function(xhr) {
{ Grocy.Api.Get('print/shoppinglist/thermal?list=' + $("#selected-shopping-list").val() + '&printHeader=' + $("#print-show-header").prop("checked"),
console.error(xhr); function (result)
} {
); bootbox.hideAll();
},
function (xhr)
{
console.error(xhr);
var validResponse = true;
try
{
var jsonError = JSON.parse(xhr.responseText);
} catch (e)
{
validResponse = false;
}
if (validResponse)
{
thermalPrintDialog.find('.bootbox-body').html(__t('Unable to print: ') + jsonError.error_message);
} else {
thermalPrintDialog.find('.bootbox-body').html(__t('Unable to print: ') + xhr.responseText);
}
}
);
}, 1000);
} }
}, },
ok: { ok: {
label: __t('Print'), label: __t('Print'),
className: 'btn-primary responsive-button', className: 'btn-primary responsive-button',
callback: function() callback: function ()
{ {
bootbox.hideAll(); bootbox.hideAll();
$(".print-timestamp").text(moment().format("l LT")); $(".print-timestamp").text(moment().format("l LT"));
$("#description-for-print").html($("#description").val()); $("#description-for-print").html($("#description").val());
if ($("#description").text().isEmpty()) if ($("#description").text().isEmpty()) {
{
$("#description-for-print").parent().addClass("d-print-none"); $("#description-for-print").parent().addClass("d-print-none");
} }
if (!$("#print-show-header").prop("checked")) if (!$("#print-show-header").prop("checked")) {
{
$("#print-header").addClass("d-none"); $("#print-header").addClass("d-none");
} }
if (!$("#print-group-by-product-group").prop("checked")) if (!$("#print-group-by-product-group").prop("checked")) {
{
shoppingListPrintShadowTable.rowGroup().enable(false); shoppingListPrintShadowTable.rowGroup().enable(false);
shoppingListPrintShadowTable.order.fixed({}); shoppingListPrintShadowTable.order.fixed({});
shoppingListPrintShadowTable.draw(); shoppingListPrintShadowTable.draw();
@ -492,11 +509,12 @@ $(document).on("click", "#print-shopping-list-button", function(e)
if (!Grocy.FeatureFlags["GROCY_FEATURE_FLAG_THERMAL_PRINTER"]) if (!Grocy.FeatureFlags["GROCY_FEATURE_FLAG_THERMAL_PRINTER"])
{ {
delete printButtons['printtp']; delete printButtons['printtp'];
sizePrintDialog = 'small';
} }
bootbox.dialog({ bootbox.dialog({
message: dialogHtml, message: dialogHtml,
size: 'medium', size: sizePrintDialog,
backdrop: true, backdrop: true,
closeButton: false, closeButton: false,
className: "d-print-none", className: "d-print-none",