Defer SaveUserSettings API calls until after Page 'load' event

This commit is contained in:
Katharina Bogad 2021-06-25 17:57:27 +02:00
parent 4cde333d35
commit 05bbb2656b
2 changed files with 71 additions and 27 deletions

View File

@ -70,6 +70,7 @@ class GrocyClass
strings = this.Api.LoadLanguageSync("en");
}
Object.assign(strings.messages[""], config.GettextPo.messages[""]);
this.strings = strings;
this.Translator = new Translator(strings);
this.FrontendHelpers = new GrocyFrontendHelpers(this, this.Api);
@ -85,6 +86,21 @@ class GrocyClass
{
self.Nightmode.Initialize();
self.Nightmode.StartWatchdog();
});
window.addEventListener('load', function()
{
if (self.documentReady) return;
// preload views
self.documentReady = true;
var element = self.preloadViews.pop();
while (element !== undefined)
{
self.PreloadView(element.viewName, element.loadCss, element.cb);
element = self.preloadViews.pop();
}
// DB Changed Handling
if (self.UserId !== -1)
@ -107,21 +123,6 @@ class GrocyClass
}
});
window.addEventListener('load', function()
{
if (self.documentReady) return;
// preload views
self.documentReady = true;
var element = self.preloadViews.pop();
while (element !== undefined)
{
self.PreloadView(element.viewName, element.loadCss, element.cb);
element = self.preloadViews.pop();
}
});
// save the config
this.config = config;
@ -176,24 +177,27 @@ class GrocyClass
translate(text, ...placeholderValues)
{
if (this.Mode === "dev")
{
var text2 = text;
this.Api.Post('system/log-missing-localization', { "text": text2 });
}
this.logTranslation(text);
return this.Translator.__(text, ...placeholderValues)
}
translaten(number, singularForm, pluralForm)
{
if (this.Mode === "dev")
{
var singularForm2 = singularForm;
this.Api.Post('system/log-missing-localization', { "text": singularForm2 });
}
this.logTranslation(singularForm);
return this.Translator.n__(singularForm, pluralForm, number, number)
}
logTranslation(text)
{
if (this.Mode === "dev")
{
if (!(text in this.strings.messages[""]))
{
this.Api.Post('system/log-missing-localization', { "text": text });
}
}
}
FormatUrl(relativePath)
{
return this.BaseUrl.replace(/\/$/, '') + relativePath;
@ -399,6 +403,11 @@ class GrocyClass
})
}
RegisterUnload(cb)
{
return;
}
Unload()
{
return; // root grocy instances never get unloaded.

View File

@ -23,6 +23,33 @@ class GrocyFrontendHelpers
this.dataTables = [];
this.InitDropdowns();
this.deferredPutCalls = [];
var self = this;
window.addEventListener('load', function()
{
if (self.Grocy.documentReady && self.deferredPutCalls.length == 0) return;
// save user settings
var putcall = self.deferredPutCalls.pop();
while (putcall !== undefined)
{
self.Api.Put(putcall.uri, putcall.data,
function(result)
{
// Nothing to do...
},
function(xhr)
{
if (!xhr.statusText.isEmpty())
{
this.ShowGenericError('Error while saving, probably this item already exists', xhr.response)
}
}
);
putcall = self.deferredPutCalls.pop();
}
});
}
_ApplyTemplate(data, template)
@ -194,7 +221,15 @@ class GrocyFrontendHelpers
var jsonData = {};
jsonData.value = value;
this.Api.Put('user/settings/' + settingsKey, jsonData,
let api = 'user/settings/' + settingsKey;
if (!this.Grocy.documentReady)
{
this.deferredPutCalls.push({ uri: api, data: jsonData });
return;
}
this.Api.Put(api, jsonData,
function(result)
{
// Nothing to do...