From 08ee4adddd52d35b2c031fe59dcd6ae2adceb27a Mon Sep 17 00:00:00 2001 From: Jokob-sk Date: Mon, 18 Mar 2024 22:44:16 +1100 Subject: [PATCH] =?UTF-8?q?Better=20first-load=20handling=20=F0=9F=94=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- front/js/pialert_common.js | 28 ++++++++++++++++++------- front/php/templates/language/de_de.json | 0 front/php/templates/language/es_es.json | 0 3 files changed, 20 insertions(+), 8 deletions(-) mode change 100644 => 100755 front/php/templates/language/de_de.json mode change 100644 => 100755 front/php/templates/language/es_es.json diff --git a/front/js/pialert_common.js b/front/js/pialert_common.js index 5ce4a213..7804f62f 100755 --- a/front/js/pialert_common.js +++ b/front/js/pialert_common.js @@ -155,7 +155,7 @@ function cacheSettings() setCache(`pia_set_${set.Code_Name}`, set.Value) setCache(`pia_set_opt_${set.Code_Name}`, resolvedOptions) }); - }).then(() => handleSuccess('cacheSettings')).catch(() => handleFailure('cacheSettings')); // handle AJAX synchronization + }).then(() => handleSuccess('cacheSettings')).catch(() => handleFailure('cacheSettings', cacheSettings)); // handle AJAX synchronization }) } @@ -221,7 +221,7 @@ function cacheStrings() data.forEach((langString) => { setCache(`pia_lang_${langString.String_Key}_${langString.Language_Code}`, langString.String_Value) }); - }).then(() => handleSuccess('cacheStrings')).catch(() => handleFailure('cacheStrings')); // handle AJAX synchronization + }).then(() => handleSuccess('cacheStrings')).catch(() => handleFailure('cacheStrings', cacheStrings)); // handle AJAX synchronization } @@ -891,7 +891,7 @@ function initDeviceListAll_JSON() setCache('devicesListAll_JSON', devicesListAll_JSON_str) // console.log(getCache('devicesListAll_JSON')) - }).then(() => handleSuccess('initDeviceListAll_JSON')).catch(() => handleFailure('initDeviceListAll_JSON')); // handle AJAX synchronization + }).then(() => handleSuccess('initDeviceListAll_JSON')).catch(() => handleFailure('initDeviceListAll_JSON', initDeviceListAll_JSON)); // handle AJAX synchronization } @@ -1074,6 +1074,13 @@ function resolveParams(params, template) { return template; } +// ----------------------------------------------------------------------------- +// check if two arrays contain same values even if out of order +function arraysContainSameValues(arr1, arr2) { + // Sort and stringify arrays, then compare + return JSON.stringify(arr1.slice().sort()) === JSON.stringify(arr2.slice().sort()); +} + // ----------------------------------------------------------------------------- // initialize // ----------------------------------------------------------------------------- @@ -1147,8 +1154,9 @@ function executeOnce() { showSpinner() - // Counter to keep track of completed AJAX calls - completedCalls = 0; + // to keep track of completed AJAX calls + completedCalls = [] + completedCalls_final = ['cacheSettings', 'cacheStrings', 'initDeviceListAll_JSON']; // Your initialization code here cacheSettings(); @@ -1161,22 +1169,26 @@ function executeOnce() { // Function to handle successful completion of an AJAX call const handleSuccess = (callName) => { console.log(`AJAX call ${callName} successful`); - completedCalls++; + // store completed call + completedCalls.push(callName) onAllCallsComplete(); }; // ----------------------------------------------------------------------------- // Function to handle failure of an AJAX call -const handleFailure = (callName) => { +const handleFailure = (callName, callback) => { // Handle AJAX call failure here console.error(`AJAX call ${callName} failed`); + + // try until successful + callback() }; // ----------------------------------------------------------------------------- // Function to execute when all AJAX calls have completed const onAllCallsComplete = () => { // Check if all three AJAX calls have completed - if (completedCalls === 3) { + if (arraysContainSameValues(completedCalls,completedCalls_final)) { // Set the flag in sessionStorage to indicate that the code has been executed // and save time when last time the page was initialized diff --git a/front/php/templates/language/de_de.json b/front/php/templates/language/de_de.json old mode 100644 new mode 100755 diff --git a/front/php/templates/language/es_es.json b/front/php/templates/language/es_es.json old mode 100644 new mode 100755