Skip to content

Commit 4ad06a2

Browse files
committed
Enhance Notifications settings form with improved change detection and initialization logic. Normalize original settings to prevent mismatches during form setup and suppress initial change detection until the form is fully loaded. Update save button state management for better user experience.
1 parent 589f05f commit 4ad06a2

File tree

1 file changed

+42
-7
lines changed

1 file changed

+42
-7
lines changed

frontend/static/js/settings_forms.js

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2555,11 +2555,25 @@ const SettingsForms = {
25552555
}
25562556

25572557
let hasChanges = false;
2558+
let suppressInitialDetection = true; // Suppress change detection during initial setup
25582559

25592560
// Clear any existing unsaved changes state and warnings when setting up
25602561
window.notificationsUnsavedChanges = false;
25612562
this.removeUnsavedChangesWarning();
25622563

2564+
// Ensure originalSettings has the same defaults used in form generation
2565+
// This prevents mismatches between form display values and change detection
2566+
const normalizedSettings = {
2567+
enable_notifications: originalSettings.enable_notifications !== undefined ? originalSettings.enable_notifications : false,
2568+
notification_level: originalSettings.notification_level || 'warning',
2569+
apprise_urls: originalSettings.apprise_urls || [],
2570+
notify_on_missing: originalSettings.notify_on_missing !== undefined ? originalSettings.notify_on_missing : true,
2571+
notify_on_upgrade: originalSettings.notify_on_upgrade !== undefined ? originalSettings.notify_on_upgrade : true,
2572+
notification_include_instance: originalSettings.notification_include_instance !== undefined ? originalSettings.notification_include_instance : true,
2573+
notification_include_app: originalSettings.notification_include_app !== undefined ? originalSettings.notification_include_app : true,
2574+
...originalSettings // Keep any other settings that might exist
2575+
};
2576+
25632577
// Initialize button in disabled/grey state immediately
25642578
saveButton.disabled = true;
25652579
saveButton.style.background = '#6b7280';
@@ -2601,6 +2615,12 @@ const SettingsForms = {
26012615

26022616
// Function to detect changes in form elements
26032617
const detectChanges = () => {
2618+
// Skip change detection if still in initial setup phase
2619+
if (suppressInitialDetection) {
2620+
console.log('[SettingsForms] Notifications change detection suppressed during initial setup');
2621+
return;
2622+
}
2623+
26042624
// Check regular form inputs
26052625
const inputs = container.querySelectorAll('input, select, textarea');
26062626
let formChanged = false;
@@ -2615,15 +2635,15 @@ const SettingsForms = {
26152635
let originalValue, currentValue;
26162636

26172637
if (input.type === 'checkbox') {
2618-
originalValue = originalSettings[key] !== undefined ? originalSettings[key] : false;
2638+
originalValue = normalizedSettings[key] !== undefined ? normalizedSettings[key] : false;
26192639
currentValue = input.checked;
26202640
} else if (input.type === 'number') {
26212641
// Get default from input attributes or use 0
26222642
const defaultValue = parseInt(input.getAttribute('value')) || parseInt(input.min) || 0;
2623-
originalValue = originalSettings[key] !== undefined ? parseInt(originalSettings[key]) : defaultValue;
2643+
originalValue = normalizedSettings[key] !== undefined ? parseInt(normalizedSettings[key]) : defaultValue;
26242644
currentValue = parseInt(input.value) || 0;
26252645
} else {
2626-
originalValue = originalSettings[key] || '';
2646+
originalValue = normalizedSettings[key] || '';
26272647
currentValue = input.value.trim();
26282648
}
26292649

@@ -2638,7 +2658,7 @@ const SettingsForms = {
26382658
// Special handling for apprise_urls which is a textarea with newlines
26392659
const appriseUrlsElement = container.querySelector('#apprise_urls');
26402660
if (appriseUrlsElement) {
2641-
const originalUrls = originalSettings.apprise_urls || [];
2661+
const originalUrls = normalizedSettings.apprise_urls || [];
26422662
const currentUrls = appriseUrlsElement.value.split('\n')
26432663
.map(url => url.trim())
26442664
.filter(url => url.length > 0);
@@ -2684,7 +2704,7 @@ const SettingsForms = {
26842704
// Update original settings for future change detection
26852705
const updatedSettings = window.huntarrUI.getFormSettings('general');
26862706
if (updatedSettings) {
2687-
Object.assign(originalSettings, updatedSettings);
2707+
Object.assign(normalizedSettings, updatedSettings);
26882708
}
26892709

26902710
}).catch(error => {
@@ -2700,9 +2720,24 @@ const SettingsForms = {
27002720
// Initial change detection - ensure form is fully loaded before checking
27012721
// Use a longer timeout to ensure all form elements are properly initialized
27022722
setTimeout(() => {
2703-
console.log('[SettingsForms] Running initial change detection for Notifications');
2723+
console.log('[SettingsForms] Initializing notifications form baseline');
2724+
2725+
// Force button to grey state and clear any changes
2726+
saveButton.disabled = true;
2727+
saveButton.style.background = '#6b7280';
2728+
saveButton.style.color = '#9ca3af';
2729+
saveButton.style.borderColor = '#4b5563';
2730+
saveButton.style.cursor = 'not-allowed';
2731+
window.notificationsUnsavedChanges = false;
2732+
hasChanges = false;
2733+
2734+
// Enable change detection now that form is properly initialized
2735+
suppressInitialDetection = false;
2736+
console.log('[SettingsForms] Notifications change detection enabled - button should remain grey unless changes made');
2737+
2738+
// Run one final change detection to confirm no changes detected
27042739
detectChanges();
2705-
}, 200);
2740+
}, 500);
27062741
},
27072742

27082743
// Set up manual save functionality for Swaparr

0 commit comments

Comments
 (0)