2019-04-20 17:04:40 +02:00
|
|
|
|
$('#save-shopping-list-button').on('click', function(e)
|
2017-04-21 15:36:04 +02:00
|
|
|
|
{
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
2019-04-20 17:04:40 +02:00
|
|
|
|
var jsonData = $('#shopping-list-form').serializeJSON();
|
|
|
|
|
Grocy.FrontendHelpers.BeginUiBusy("shopping-list-form");
|
2018-11-24 19:40:50 +01:00
|
|
|
|
|
2017-04-21 15:36:04 +02:00
|
|
|
|
if (Grocy.EditMode === 'create')
|
|
|
|
|
{
|
2019-04-20 17:04:40 +02:00
|
|
|
|
Grocy.Api.Post('objects/shopping_lists', jsonData,
|
2017-04-21 15:36:04 +02:00
|
|
|
|
function(result)
|
|
|
|
|
{
|
2019-09-20 20:14:47 +02:00
|
|
|
|
window.location.href = U('/shoppinglist?list=' + result.created_object_id);
|
2017-04-21 15:36:04 +02:00
|
|
|
|
},
|
|
|
|
|
function(xhr)
|
|
|
|
|
{
|
2019-04-20 17:04:40 +02:00
|
|
|
|
Grocy.FrontendHelpers.EndUiBusy("shopping-list-form");
|
|
|
|
|
Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response)
|
2017-04-21 15:36:04 +02:00
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2019-04-20 17:04:40 +02:00
|
|
|
|
Grocy.Api.Put('objects/shopping_lists/' + Grocy.EditObjectId, jsonData,
|
2017-04-21 15:36:04 +02:00
|
|
|
|
function(result)
|
|
|
|
|
{
|
2018-04-18 19:03:39 +02:00
|
|
|
|
window.location.href = U('/shoppinglist');
|
2017-04-21 15:36:04 +02:00
|
|
|
|
},
|
|
|
|
|
function(xhr)
|
|
|
|
|
{
|
2019-04-20 17:04:40 +02:00
|
|
|
|
Grocy.FrontendHelpers.EndUiBusy("shopping-list-form");
|
|
|
|
|
Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response)
|
2017-04-21 15:36:04 +02:00
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2019-04-20 17:04:40 +02:00
|
|
|
|
$('#shopping-list-form input').keyup(function(event)
|
2017-04-21 19:02:00 +02:00
|
|
|
|
{
|
2019-04-20 17:04:40 +02:00
|
|
|
|
Grocy.FrontendHelpers.ValidateForm('shopping-list-form');
|
2018-07-11 19:43:05 +02:00
|
|
|
|
});
|
|
|
|
|
|
2019-04-20 17:04:40 +02:00
|
|
|
|
$('#shopping-list-form input').keydown(function (event)
|
2018-04-16 19:11:32 +02:00
|
|
|
|
{
|
|
|
|
|
if (event.keyCode === 13) //Enter
|
2017-04-21 15:36:04 +02:00
|
|
|
|
{
|
2018-09-29 13:41:56 +02:00
|
|
|
|
event.preventDefault();
|
2019-01-19 00:37:21 -07:00
|
|
|
|
|
2019-04-20 17:04:40 +02:00
|
|
|
|
if (document.getElementById('shopping-list-form').checkValidity() === false) //There is at least one validation error
|
2017-04-21 15:36:04 +02:00
|
|
|
|
{
|
2018-04-16 19:11:32 +02:00
|
|
|
|
return false;
|
2017-04-21 15:36:04 +02:00
|
|
|
|
}
|
2018-07-11 19:43:05 +02:00
|
|
|
|
else
|
|
|
|
|
{
|
2019-04-20 17:04:40 +02:00
|
|
|
|
$('#save-shopping-list-button').click();
|
2018-07-11 19:43:05 +02:00
|
|
|
|
}
|
2018-04-16 19:11:32 +02:00
|
|
|
|
}
|
2017-04-21 15:36:04 +02:00
|
|
|
|
});
|
2019-04-20 17:04:40 +02:00
|
|
|
|
|
|
|
|
|
$('#name').focus();
|
|
|
|
|
Grocy.FrontendHelpers.ValidateForm('shopping-list-form');
|