Skip to content

Commit 102a62c

Browse files
committed
FE: enhance settings save functionality with reload wait detection and background processing
1 parent 6256149 commit 102a62c

5 files changed

Lines changed: 94 additions & 9 deletions

File tree

front/php/server/util.php

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,13 +166,72 @@ function saveSettings()
166166
copy($fullConfPath, $fullConfPath . ".bak");
167167
}
168168

169+
// Detect whether the frontend needs to block-wait for backend reload
170+
$requiresReloadWait = getReloadWaitRequired($decodedSettings);
171+
169172
// Open the file for writing without changing permissions
170173
$file = fopen($fullConfPath, "w") or die("Unable to open file!");
171174
fwrite($file, $txt);
172175
fclose($file);
173176

174-
echo "OK";
177+
echo json_encode(['success' => true, 'requiresReloadWait' => $requiresReloadWait]);
178+
179+
}
180+
181+
// -------------------------------------------------------------------------------------------
182+
// Determines if the frontend must wait (block) for the backend to finish reloading after a
183+
// settings save. Blocking is required when LOADED_PLUGINS changes or UI_WAIT_FOR_SETTINGS
184+
// is explicitly enabled. Defaults to true (safe) on any parsing error.
185+
function getReloadWaitRequired($decodedSettings) {
186+
$newLoadedPlugins = null;
187+
$uiWaitForSettings = false;
188+
189+
foreach ($decodedSettings as $setting) {
190+
if ($setting[1] === 'LOADED_PLUGINS') {
191+
$newLoadedPlugins = $setting[3];
192+
}
193+
if ($setting[1] === 'UI_WAIT_FOR_SETTINGS') {
194+
$uiWaitForSettings = ($setting[3] === true || $setting[3] === 1
195+
|| strtolower((string)$setting[3]) === 'true');
196+
}
197+
}
198+
199+
if ($uiWaitForSettings) {
200+
return true;
201+
}
202+
203+
$oldLoadedPlugins = getSettingValue('LOADED_PLUGINS');
204+
205+
// If the old value couldn't be read, default to blocking (safe).
206+
if (strpos((string)$oldLoadedPlugins, 'Could not') !== false) {
207+
return true;
208+
}
209+
210+
return normalizePluginList($oldLoadedPlugins) !== normalizePluginList($newLoadedPlugins);
211+
}
175212

213+
// -------------------------------------------------------------------------------------------
214+
// Normalise a plugin list value (PHP array, JSON array, or Python-style list) to a sorted
215+
// JSON string for reliable equality comparison.
216+
function normalizePluginList($value) {
217+
if ($value === null || $value === '') {
218+
return '[]';
219+
}
220+
if (is_array($value)) {
221+
$arr = $value;
222+
} else {
223+
$arr = json_decode($value, true);
224+
if (!is_array($arr)) {
225+
// Handle Python-style single-quoted lists: ['A','B']
226+
$jsonStr = str_replace("'", '"', (string)$value);
227+
$arr = json_decode($jsonStr, true);
228+
}
229+
if (!is_array($arr)) {
230+
return trim((string)$value);
231+
}
232+
}
233+
sort($arr);
234+
return json_encode($arr);
176235
}
177236

178237
// -------------------------------------------------------------------------------------------

front/php/templates/language/en_us.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,7 @@
807807
"settings_publishers_label": "Publishers",
808808
"settings_readonly": "Can't READ or WRITE <code>app.conf</code>. Try restarting the container and read the <a href=\"https://docs.netalertx.com/FILE_PERMISSIONS\" target=\"_blank\">file permissions documentation</a>",
809809
"settings_saved": "<br/>Settings saved. <br/> Reloading… <br/><i class=\"ion ion-ios-loop-strong fa-spin fa-2x fa-fw\"></i> <br/>",
810+
"settings_saved_background": "Settings saved. Changes are being applied in the background.",
810811
"settings_system_icon": "fa-solid fa-gear",
811812
"settings_system_label": "System",
812813
"settings_update_item_warning": "Update the value below. Be careful to follow the previous format. <b>Validation is not performed.</b>",

front/settings.php

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -615,19 +615,33 @@ function: 'savesettings',
615615
settings: JSON.stringify(settingsArray) },
616616
success: function(data, textStatus) {
617617

618-
if(data == "OK")
619-
{
620-
// showMessage (getString("settings_saved"), 5000, "modal_grey");
618+
// Parse response: support both legacy "OK" string and new JSON format
619+
let saveSucceeded = false;
620+
let requiresReloadWait = true; // safe default
621+
622+
if (data === "OK") {
623+
saveSucceeded = true;
624+
} else {
625+
let parsed = null;
626+
try { parsed = (typeof data === 'object') ? data : JSON.parse(data); } catch(e) {}
627+
if (parsed && parsed.success === true) {
628+
saveSucceeded = true;
629+
requiresReloadWait = parsed.requiresReloadWait === true;
630+
}
631+
}
632+
633+
if (saveSucceeded) {
621634
// Remove navigation prompt "Are you sure you want to leave..."
622635
window.onbeforeunload = null;
623636

624-
// Reloads the current page
625-
// setTimeout("clearCache()", 5000);
626-
627637
write_notification(`[Settings] Settings saved by the user`, 'info')
628638

629-
clearCache()
630-
} else{
639+
if (requiresReloadWait) {
640+
clearCache()
641+
} else {
642+
showMessage(getString("settings_saved_background"), 5000, "modal_green");
643+
}
644+
} else {
631645
// something went wrong
632646
write_notification("[Important] Please take a screenshot of the Console tab in the browser (F12) and next error. Submit it (with the nginx and php error logs) as a new issue here: https://github.com/netalertx/NetAlertX/issues", 'interrupt')
633647
write_notification(data, 'interrupt')

server/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
LOG_LEVEL = "verbose"
3333
TIMEZONE = "Europe/Berlin"
3434
UI_LANG = "English (en_us)"
35+
UI_WAIT_FOR_SETTINGS = False
3536
UI_PRESENCE = ["online", "offline", "archived"]
3637
UI_MY_DEVICES = ["online", "offline", "archived", "new", "down"]
3738
UI_NOT_RANDOM_MAC = []

server/initialise.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,16 @@ def importConfigs(pm, db, all_plugins):
452452
"UI",
453453
)
454454

455+
conf.UI_WAIT_FOR_SETTINGS = ccd(
456+
"UI_WAIT_FOR_SETTINGS",
457+
False,
458+
c_d,
459+
"Wait for settings reload",
460+
'{"dataType":"boolean", "elements": [{"elementType" : "checkbox", "elementOptions" : [] ,"transformers": []}]}',
461+
"[]",
462+
"UI",
463+
)
464+
455465
# Init timezone in case it changed and handle invalid values
456466
try:
457467
if conf.TIMEZONE not in all_timezones:

0 commit comments

Comments
 (0)