Embedded barcode and qu-conversion with page reload on change

This commit is contained in:
Kurt Riddlesperger 2020-05-01 00:59:41 -05:00
parent c3f77591ee
commit 410a0f0094
4 changed files with 22 additions and 92 deletions

View File

@ -10,8 +10,6 @@
Grocy.Api.Post('objects/product_barcodes', jsonData, Grocy.Api.Post('objects/product_barcodes', jsonData,
function(result) function(result)
{ {
Grocy.EditObjectId = result.created_object_id;
window.location.href = U("/product/" + GetUriParam("product"));
}, },
function(xhr) function(xhr)
{ {
@ -25,7 +23,6 @@
Grocy.Api.Put('objects/product_barcodes/' + Grocy.EditObjectId, jsonData, Grocy.Api.Put('objects/product_barcodes/' + Grocy.EditObjectId, jsonData,
function(result) function(result)
{ {
window.location.href = U("/product/" + GetUriParam("product"));
}, },
function(xhr) function(xhr)
{ {
@ -35,38 +32,8 @@
); );
} }
Grocy.Api.Get('stock/products/' + jsonData.product_id, window.parent.postMessage(WindowMessageBag("ProductBarcodesChanged"), U("/product/" + GetUriParam("product")));
function(productDetails) window.parent.postMessage(WindowMessageBag("CloseAllModals"), U("/product/" + GetUriParam("product")));
{
var existingBarcodes = productDetails.product.barcode || '';
if (existingBarcodes.length === 0)
{
productDetails.product.barcode = jsonData.barcode;
}
else
{
productDetails.product.barcode += ',' + jsonData.barcode;
}
var jsonDataProduct = {};
jsonDataProduct.barcode = productDetails.product.barcode;
Grocy.Api.Put('objects/products/' + jsonData.product_id, jsonDataProduct,
function(result)
{
},
function(xhr)
{
Grocy.FrontendHelpers.EndUiBusy("barcode-form");
Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response)
}
);
},
function(xhr)
{
Grocy.FrontendHelpers.EndUiBusy("barcode-form");
Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response);
}
);
}); });
$('#barcode').on('change', function(e) $('#barcode').on('change', function(e)

View File

@ -381,7 +381,6 @@ var barcodeTable = $('#barcode-table').DataTable({
$('#barcode-table tbody').removeClass("d-none"); $('#barcode-table tbody').removeClass("d-none");
barcodeTable.columns.adjust().draw(); barcodeTable.columns.adjust().draw();
Grocy.Components.UserfieldsForm.Load(); Grocy.Components.UserfieldsForm.Load();
$("#name").trigger("keyup"); $("#name").trigger("keyup");
$('#name').focus(); $('#name').focus();
@ -429,19 +428,6 @@ $(document).on('click', '.qu-conversion-delete-button', function(e)
}); });
}); });
$(document).on('click', '.qu-conversion-edit-button', function (e)
{
var id = $(e.currentTarget).attr('data-qu-conversion-id');
Grocy.ProductEditFormRedirectUri = U("/quantityunitconversion/" + id.toString() + "?product=editobjectid");
$('#save-product-button').click();
});
$("#qu-conversion-add-button").on("click", function(e)
{
Grocy.ProductEditFormRedirectUri = U("/quantityunitconversion/new?product=editobjectid");
$('#save-product-button').click();
});
$(document).on('click', '.barcode-delete-button', function(e) $(document).on('click', '.barcode-delete-button', function(e)
{ {
var objectId = $(e.currentTarget).attr('data-barcode-id'); var objectId = $(e.currentTarget).attr('data-barcode-id');
@ -477,47 +463,11 @@ $(document).on('click', '.barcode-delete-button', function(e)
console.error(xhr); console.error(xhr);
} }
); );
var newBarcode = '';
productBarcode.split(',').forEach(function(item)
{
if(barcode != item)
{
newBarcode += ',' + item;
}
});
var jsonDataProduct = {};
jsonDataProduct.barcode = newBarcode;
Grocy.Api.Put('objects/products/' + productId, jsonDataProduct,
function(result)
{
},
function(xhr)
{
Grocy.FrontendHelpers.EndUiBusy("product-form");
Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response)
}
);
} }
} }
}); });
}); });
$(document).on('click', '.barcode-edit-button', function (e)
{
var id = $(e.currentTarget).attr('data-barcode-id');
Grocy.ProductEditFormRedirectUri = U("/productbarcodes/" + id.toString() + "?product=editobjectid");
$('#save-product-button').click();
});
$("#barcode-add-button").on("click", function(e)
{
Grocy.ProductEditFormRedirectUri = U("/productbarcodes/new?product=editobjectid");
$('#save-product-button').click();
});
$('#qu_id_purchase').blur(function(e) $('#qu_id_purchase').blur(function(e)
{ {
// Preset the stock quantity unit with the purchase quantity unit, if the stock quantity unit is unset. // Preset the stock quantity unit with the purchase quantity unit, if the stock quantity unit is unset.
@ -528,3 +478,13 @@ $('#qu_id_purchase').blur(function(e)
Grocy.FrontendHelpers.ValidateForm('product-form'); Grocy.FrontendHelpers.ValidateForm('product-form');
} }
}); });
$(window).on("message", function(e)
{
var data = e.originalEvent.data;
if (data.Message === "ProductBarcodesChanged" || data.Message === "ProductQUConversionChanged")
{
window.location.reload();
}
});

View File

@ -25,7 +25,8 @@
} }
else else
{ {
window.location.href = U("/product/" + GetUriParam("product")); window.parent.postMessage(WindowMessageBag("ProductQUConversionChanged"), U("/product/" + GetUriParam("product")));
window.parent.postMessage(WindowMessageBag("CloseAllModals"), U("/product/" + GetUriParam("product")));
} }
}); });
}, },
@ -54,7 +55,8 @@
} }
else else
{ {
window.location.href = U("/product/" + GetUriParam("product")); window.parent.postMessage(WindowMessageBag("ProductQUConversionChanged"), U("/product/" + GetUriParam("product")));
window.parent.postMessage(WindowMessageBag("CloseAllModals"), U("/product/" + GetUriParam("product")));
} }
}); });
}, },
@ -79,7 +81,8 @@
} }
else else
{ {
window.location.href = U("/product/" + GetUriParam("product")); window.parent.postMessage(WindowMessageBag("ProductQUConversionChanged"), U("/product/" + GetUriParam("product")));
window.parent.postMessage(WindowMessageBag("CloseAllModals"), U("/product/" + GetUriParam("product")));
} }
}); });
}, },

View File

@ -294,7 +294,7 @@
<div class="col-lg-6 col-xs-12"> <div class="col-lg-6 col-xs-12">
<h2> <h2>
{{ $__t('QU conversions') }} {{ $__t('QU conversions') }}
<a id="qu-conversion-add-button" class="btn btn-outline-dark" href="#"> <a class="btn btn-outline-dark show-as-dialog-link" type="button" href="{{ $U('/quantityunitconversion/new?embedded&product=' . $product->id ) }}">
<i class="fas fa-plus"></i> {{ $__t('Add') }} <i class="fas fa-plus"></i> {{ $__t('Add') }}
</a> </a>
</h2> </h2>
@ -315,7 +315,7 @@
@if($quConversion->product_id == $product->id || $quConversion->product_id == null) @if($quConversion->product_id == $product->id || $quConversion->product_id == null)
<tr> <tr>
<td class="fit-content border-right"> <td class="fit-content border-right">
<a class="btn btn-sm btn-info qu-conversion-edit-button @if($quConversion->product_id == null) disabled @endif" href="#" data-qu-conversion-id="{{ $quConversion->id }}"> <a class="btn btn-sm btn-info show-as-dialog-link @if($quConversion->product_id == null) disabled @endif" href="{{ $U('/quantityunitconversion/' . $quConversion->id . '?embedded&product=' . $product->id ) }}">
<i class="fas fa-edit"></i> <i class="fas fa-edit"></i>
</a> </a>
<a class="btn btn-sm btn-danger qu-conversion-delete-button @if($quConversion->product_id == null) disabled @endif" href="#" data-qu-conversion-id="{{ $quConversion->id }}"> <a class="btn btn-sm btn-danger qu-conversion-delete-button @if($quConversion->product_id == null) disabled @endif" href="#" data-qu-conversion-id="{{ $quConversion->id }}">
@ -347,7 +347,7 @@
<h2> <h2>
{{ $__t('Barcode Details') }} {{ $__t('Barcode Details') }}
<a id="barcode-add-button" class="btn btn-outline-dark" href="#"> <a class="btn btn-outline-dark show-as-dialog-link" type="button" href="{{ $U('/productbarcodes/new?embedded&product=' . $product->id ) }}">
<i class="fas fa-plus"></i> {{ $__t('Add') }} <i class="fas fa-plus"></i> {{ $__t('Add') }}
</a> </a>
</h2> </h2>
@ -367,7 +367,7 @@
@if($barcode->product_id == $product->id || $barcode->product_id == null) @if($barcode->product_id == $product->id || $barcode->product_id == null)
<tr> <tr>
<td class="fit-content border-right"> <td class="fit-content border-right">
<a class="btn btn-sm btn-info barcode-edit-button @if($barcode->product_id == null) disabled @endif" href="#" data-barcode-id="{{ $barcode->id }}"> <a class="btn btn-sm btn-info show-as-dialog-link @if($barcode->product_id == null) disabled @endif" href="{{ $U('/productbarcodes/' . $barcode->id . '?embedded&product=' . $product->id ) }}">
<i class="fas fa-edit"></i> <i class="fas fa-edit"></i>
</a> </a>
<a class="btn btn-sm btn-danger barcode-delete-button @if($barcode->product_id == null) disabled @endif" href="#" data-barcode-id="{{ $barcode->id }}" data-barcode="{{ $barcode->barcode }}" data-product-barcode="{{ $product->barcode }}" data-product-id="{{ $product->id }}"> <a class="btn btn-sm btn-danger barcode-delete-button @if($barcode->product_id == null) disabled @endif" href="#" data-barcode-id="{{ $barcode->id }}" data-barcode="{{ $barcode->barcode }}" data-product-barcode="{{ $product->barcode }}" data-product-id="{{ $product->id }}">