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,22 +428,28 @@ $(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();
var thermalPrintDialog = bootbox.dialog({
title: __t('Printing'),
message: '<p><i class="fa fa-spin fa-spinner"></i> ' + __t('Connecting to printer...') + '</p>'
});
//Delaying for one second so that the alert can be closed
setTimeout(function ()
{ {
Grocy.Api.Get('print/shoppinglist/thermal', { "list": $("#selected-shopping-list").val(), Grocy.Api.Get('print/shoppinglist/thermal?list=' + $("#selected-shopping-list").val() + '&printHeader=' + $("#print-show-header").prop("checked"),
"printHeader": $("#print-show-header").prop("checked") },
function (result) function (result)
{ {
bootbox.hideAll(); bootbox.hideAll();
@ -451,8 +457,23 @@ $(document).on("click", "#print-shopping-list-button", function(e)
function (xhr) function (xhr)
{ {
console.error(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: {
@ -461,22 +482,18 @@ $(document).on("click", "#print-shopping-list-button", function(e)
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",