Implemented user pictures (closes #1158)

This commit is contained in:
Bernd Bestel
2020-12-20 22:08:50 +01:00
parent 3f718eab60
commit 8f1ce607f7
15 changed files with 181 additions and 61 deletions

View File

@@ -379,36 +379,3 @@ $(window).on("message", function(e)
);
}
});
// Grocy.Components.RecipePicker.GetPicker().on('change', function (e)
// {
// var value = Grocy.Components.RecipePicker.GetValue();
// if (value.toString().isEmpty())
// {
// return;
// }
// Grocy.Api.Get('objects/recipes/' + value,
// function(recipe)
// {
// $("#includes_servings").val(recipe.servings);
// },
// function(xhr)
// {
// console.error(xhr);
// }
// );
// });
// Grocy.Components.ProductPicker.GetPicker().on('change', function(e)
// {
// // Just save the current recipe on every change of the product picker as a workflow could be started which leaves the page...
// Grocy.Api.Put('objects/recipes/' + Grocy.EditObjectId, $('#recipe-form').serializeJSON(), function () { }, function () { });
// });
// As the /recipe/new route immediately creates a new recipe on load,
// always replace the current location by the created recipes edit page location
// if (window.location.pathname.toLowerCase() === "/recipe/new")
// {
// window.history.replaceState(null, null, U("/recipe/" + Grocy.EditObjectId));
// }

View File

@@ -1,17 +1,46 @@
$('#save-user-button').on('click', function(e)
function SaveUserPicture(result, jsonData)
{
var userId = Grocy.EditObjectId || result.created_object_id;
Grocy.Components.UserfieldsForm.Save(() =>
{
if (jsonData.hasOwnProperty("picture_file_name") && !Grocy.DeleteUserPictureOnSave)
{
Grocy.Api.UploadFile($("#user-picture")[0].files[0], 'userpictures', jsonData.picture_file_name,
(result) =>
{
window.location.href = U('/users');
},
(xhr) =>
{
Grocy.FrontendHelpers.EndUiBusy("user-form");
Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response)
}
);
}
else
{
window.location.href = U('/users');
}
});
}
$('#save-user-button').on('click', function(e)
{
e.preventDefault();
var jsonData = $('#user-form').serializeJSON();
Grocy.FrontendHelpers.BeginUiBusy("user-form");
if ($("#user-picture")[0].files.length > 0)
{
var someRandomStuff = Math.random().toString(36).substring(2, 100) + Math.random().toString(36).substring(2, 100);
jsonData.picture_file_name = someRandomStuff + $("#user-picture")[0].files[0].name;
}
if (Grocy.EditMode === 'create')
{
Grocy.Api.Post('users', jsonData,
function(result)
{
window.location.href = U('/users');
},
(result) => SaveUserPicture(result, jsonData),
function(xhr)
{
Grocy.FrontendHelpers.EndUiBusy("user-form");
@@ -21,11 +50,25 @@
}
else
{
if (Grocy.DeleteUserPictureOnSave)
{
jsonData.picture_file_name = null;
Grocy.Api.DeleteFile(Grocy.UserPictureFileName, 'userpictures', {},
function(result)
{
// Nothing to do
},
function(xhr)
{
Grocy.FrontendHelpers.EndUiBusy("user-form");
Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response)
}
);
}
Grocy.Api.Put('users/' + Grocy.EditObjectId, jsonData,
function(result)
{
window.location.href = U('/users');
},
(result) => SaveUserPicture(result, jsonData),
function(xhr)
{
Grocy.FrontendHelpers.EndUiBusy("user-form");
@@ -76,4 +119,23 @@ else
$('#username').focus();
}
$("#user-picture").on("change", function(e)
{
$("#user-picture-label").removeClass("d-none");
$("#user-picture-label-none").addClass("d-none");
$("#delete-current-user-picture-on-save-hint").addClass("d-none");
$("#current-user-picture").addClass("d-none");
Grocy.DeleteUserePictureOnSave = false;
});
Grocy.DeleteUserPictureOnSave = false;
$("#delete-current-user-picture-button").on("click", function(e)
{
Grocy.DeleteUserPictureOnSave = true;
$("#current-user-picture").addClass("d-none");
$("#delete-current-user-picture-on-save-hint").removeClass("d-none");
$("#user-picture-label").addClass("d-none");
$("#user-picture-label-none").removeClass("d-none");
});
Grocy.FrontendHelpers.ValidateForm('user-form');