Optimized Userfield save event handling (fixes #2458)

This commit is contained in:
Bernd Bestel
2025-01-13 21:00:06 +01:00
parent a734d1c3ae
commit c8ccc0b529
26 changed files with 184 additions and 105 deletions

View File

@@ -12,10 +12,21 @@ Grocy.Components.UserfieldsForm.Save = function(success, error)
return;
}
var jsonData = {};
var editedUserfieldInputs = $("#userfields-form .userfield-input.is-dirty").not("div");
$("#userfields-form .userfield-input").not("div").each(function()
if (!editedUserfieldInputs.length)
{
if (success)
{
success();
}
return;
}
editedUserfieldInputs.each(function(index, item)
{
var jsonData = {};
var input = $(this);
var fieldName = input.attr("data-userfield-name");
var fieldValue = input.val();
@@ -30,33 +41,21 @@ Grocy.Components.UserfieldsForm.Save = function(success, error)
}
else if (input.attr("type") == "file")
{
var oldFile = input.data('old-file')
if (oldFile)
if (input.hasAttr("data-old-file"))
{
Grocy.Api.Delete('files/userfiles/' + oldFile, null, null,
function(xhr)
{
Grocy.FrontendHelpers.ShowGenericError('Could not delete file', xhr);
});
jsonData[fieldName] = "";
var oldFile = input.attr("data-old-file");
if (oldFile)
{
jsonData[fieldName] = "";
}
}
if (input[0].files.length > 0)
{
// Files service requires an extension
var fileName = RandomString() + '.' + CleanFileName(input[0].files[0].name.split('.').reverse()[0]);
var newFile = RandomString() + '.' + CleanFileName(input[0].files[0].name.split('.').reverse()[0]);
jsonData[fieldName] = btoa(newFile) + '_' + btoa(CleanFileName(input[0].files[0].name));
jsonData[fieldName] = btoa(fileName) + '_' + btoa(CleanFileName(input[0].files[0].name));
Grocy.Api.UploadFile(input[0].files[0], 'userfiles', fileName,
function(result)
{
},
function(xhr)
{
// When navigating away immediately from the current page, this is maybe a false positive - so ignore this for now
// Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response)
}
);
}
}
else if ($(this).hasAttr("multiple"))
@@ -67,24 +66,100 @@ Grocy.Components.UserfieldsForm.Save = function(success, error)
{
jsonData[fieldName] = fieldValue;
}
});
Grocy.Api.Put('userfields/' + $("#userfields-form").data("entity") + '/' + Grocy.EditObjectId, jsonData,
function(result)
{
if (success)
Grocy.Api.Put('userfields/' + $("#userfields-form").data("entity") + '/' + Grocy.EditObjectId, jsonData,
function(result)
{
success();
}
},
function(xhr)
{
if (error)
if (typeof newFile !== 'undefined' && typeof oldFile !== 'undefined') // Delete and Upload
{
Grocy.Api.DeleteFile(oldFile, 'userfiles',
function(result)
{
Grocy.Api.UploadFile(input[0].files[0], 'userfiles', newFile,
function(result2)
{
if (success && index === editedUserfieldInputs.length - 1) // Last item
{
success();
}
},
function(xhr)
{
Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response);
if (error && index === editedUserfieldInputs.length - 1) // Last item
{
error();
}
}
);
},
function(xhr)
{
Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response);
if (error && index === editedUserfieldInputs.length - 1) // Last item
{
error();
}
}
);
}
else if (typeof newFile !== 'undefined') // Upload only
{
Grocy.Api.UploadFile(input[0].files[0], 'userfiles', newFile,
function(result2)
{
if (success && index === editedUserfieldInputs.length - 1) // Last item
{
success();
}
},
function(xhr)
{
Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response);
if (error && index === editedUserfieldInputs.length - 1) // Last item
{
error();
}
}
);
}
else if (typeof oldFile !== 'undefined') // Delete only
{
Grocy.Api.DeleteFile(oldFile, 'userfiles',
function(result)
{
if (success && index === editedUserfieldInputs.length - 1) // Last item
{
success();
}
},
function(xhr)
{
Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response);
if (error && index === editedUserfieldInputs.length - 1) // Last item
{
error();
}
}
);
}
else // Nothing else to do
{
if (success && index === editedUserfieldInputs.length - 1) // Last item
{
success();
}
}
},
function(xhr)
{
error();
if (error && index === editedUserfieldInputs.length - 1) // Last item
{
error();
}
}
}
);
);
});
}
Grocy.Components.UserfieldsForm.Load = function()
@@ -155,14 +230,13 @@ Grocy.Components.UserfieldsForm.Load = function()
if (value)
{
var fileName = atob(value.split('_')[1]);
var fileSrc = value.split('_')[0];
var fileSrc = atob(value.split('_')[0]);
var formGroup = input.parent().parent().parent();
formGroup.find("label.custom-file-label").text(fileName);
formGroup.find(".userfield-file-show").attr('href', U('/files/userfiles/' + value));
formGroup.find('.userfield-file-show').removeClass('d-none');
formGroup.find('img.userfield-current-file')
.attr('src', U('/files/userfiles/' + value + '?force_serve_as=picture&best_fit_width=250&best_fit_height=250'));
formGroup.find('img.userfield-current-file').attr('src', U('/files/userfiles/' + value + '?force_serve_as=picture&best_fit_width=250&best_fit_height=250'));
formGroup.find('.userfield-file-delete').click(
function()
@@ -170,6 +244,7 @@ Grocy.Components.UserfieldsForm.Load = function()
formGroup.find("label.custom-file-label").text(__t("No file selected"));
formGroup.find(".userfield-file-show").addClass('d-none');
input.attr('data-old-file', fileSrc);
input.addClass("is-dirty");
}
);