Skip to content

Commit f884593

Browse files
committed
Enhance Swaparr state management in SettingsForms by adding DOM checks and asynchronous fetching. Update visibility of Swaparr fields across all app forms upon state changes, improving user experience and ensuring accurate settings representation.
1 parent 7b7c4d5 commit f884593

File tree

2 files changed

+109
-34
lines changed

2 files changed

+109
-34
lines changed

frontend/static/js/new-main.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1835,6 +1835,13 @@ let huntarrUI = {
18351835
.then(data => {
18361836
if (data.success !== false) { // API returns all settings on success, not just success:true
18371837
console.log('[huntarrUI] Swaparr auto-save successful');
1838+
1839+
// Update Swaparr field visibility in all loaded app forms
1840+
if (window.SettingsForms && typeof window.SettingsForms.updateSwaparrFieldsDisabledState === 'function') {
1841+
console.log('[huntarrUI] Broadcasting Swaparr state change to all app forms...');
1842+
window.SettingsForms.updateSwaparrFieldsDisabledState();
1843+
}
1844+
18381845
resolve();
18391846
} else {
18401847
console.error('[huntarrUI] Swaparr auto-save failed:', data);

frontend/static/js/settings_forms.js

Lines changed: 102 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,41 @@ const SettingsForms = {
99
isSwaparrGloballyEnabled: function() {
1010
// Try to get Swaparr settings from cache or current settings
1111
try {
12+
// First check the DOM - if we're on the Swaparr page, check the toggle directly
13+
const swaparrToggle = document.querySelector('#swaparr_enabled');
14+
if (swaparrToggle) {
15+
console.log(`[SettingsForms] Reading Swaparr state from DOM toggle: ${swaparrToggle.checked}`);
16+
return swaparrToggle.checked;
17+
}
18+
1219
// Check if we have cached settings
1320
const cachedSettings = localStorage.getItem('huntarr-settings-cache');
1421
if (cachedSettings) {
1522
const settings = JSON.parse(cachedSettings);
1623
if (settings.swaparr && settings.swaparr.enabled !== undefined) {
24+
console.log(`[SettingsForms] Reading Swaparr state from cache: ${settings.swaparr.enabled}`);
1725
return settings.swaparr.enabled === true;
1826
}
1927
}
2028

2129
// Fallback: check if we have current settings loaded
2230
if (window.huntarrUI && window.huntarrUI.originalSettings && window.huntarrUI.originalSettings.swaparr) {
31+
console.log(`[SettingsForms] Reading Swaparr state from window settings: ${window.huntarrUI.originalSettings.swaparr.enabled}`);
2332
return window.huntarrUI.originalSettings.swaparr.enabled === true;
2433
}
2534

26-
// Default to true if we can't determine the state (enable the field by default)
27-
return true;
35+
// If we can't determine the state, trigger an async fetch but return false for now
36+
console.log('[SettingsForms] Cannot determine Swaparr state, fetching from server...');
37+
this.fetchAndCacheSwaparrState().then(() => {
38+
// After fetching, trigger a visibility update
39+
setTimeout(() => {
40+
this.updateSwaparrFieldsDisabledState();
41+
}, 100);
42+
});
43+
return false;
2844
} catch (e) {
2945
console.warn('[SettingsForms] Error checking Swaparr global status:', e);
30-
return true; // Default to enabling the field if there's an error
46+
return false; // Default to disabling the field if there's an error
3147
}
3248
},
3349

@@ -3358,6 +3374,10 @@ const SettingsForms = {
33583374
Object.assign(originalSettings, updatedSettings);
33593375
}
33603376

3377+
// Update Swaparr field visibility in all loaded app forms
3378+
console.log('[SettingsForms] Broadcasting Swaparr state change to all app forms after manual save...');
3379+
this.updateSwaparrFieldsDisabledState();
3380+
33613381
}).catch(error => {
33623382
console.error('[SettingsForms] Swaparr manual save failed:', error);
33633383

@@ -5278,41 +5298,89 @@ const SettingsForms = {
52785298

52795299
// Update visibility of Swaparr fields in all app forms based on global Swaparr setting
52805300
updateSwaparrFieldsDisabledState: function() {
5281-
const isEnabled = this.isSwaparrGloballyEnabled();
5282-
5283-
// Since Swaparr sections are now conditionally rendered, we need to regenerate forms
5284-
// to show/hide the Swaparr sections. Check if any app forms are currently visible.
5285-
const appTypes = ['sonarr', 'radarr', 'lidarr', 'readarr', 'whisparr', 'eros'];
5286-
let formsRegenerated = false;
5287-
5288-
appTypes.forEach(appType => {
5289-
const container = document.querySelector(`[data-app-type="${appType}"]`);
5290-
if (container && container.innerHTML.trim() !== '') {
5291-
// This app form is currently loaded, regenerate it to update Swaparr visibility
5292-
try {
5293-
// Get current settings from cache or original settings
5294-
let settings = {};
5295-
if (window.huntarrUI?.originalSettings?.[appType]) {
5296-
settings = window.huntarrUI.originalSettings[appType];
5297-
}
5298-
5299-
// Regenerate the form
5300-
const formMethodName = `generate${appType.charAt(0).toUpperCase() + appType.slice(1)}Form`;
5301-
if (this[formMethodName]) {
5302-
this[formMethodName](container, settings);
5303-
formsRegenerated = true;
5301+
// First ensure we have the current Swaparr state
5302+
this.fetchAndCacheSwaparrState().then(() => {
5303+
const isEnabled = this.isSwaparrGloballyEnabled();
5304+
console.log(`[SettingsForms] Updating Swaparr fields visibility with state: ${isEnabled}`);
5305+
5306+
// Since Swaparr sections are now conditionally rendered, we need to regenerate forms
5307+
// to show/hide the Swaparr sections. Check if any app forms are currently visible.
5308+
const appTypes = ['sonarr', 'radarr', 'lidarr', 'readarr', 'whisparr', 'eros'];
5309+
let formsRegenerated = false;
5310+
5311+
appTypes.forEach(appType => {
5312+
const container = document.querySelector(`[data-app-type="${appType}"]`);
5313+
if (container && container.innerHTML.trim() !== '') {
5314+
// This app form is currently loaded, regenerate it to update Swaparr visibility
5315+
try {
5316+
// Get current settings from cache or original settings
5317+
let settings = {};
5318+
if (window.huntarrUI?.originalSettings?.[appType]) {
5319+
settings = window.huntarrUI.originalSettings[appType];
5320+
}
5321+
5322+
// Regenerate the form
5323+
const formMethodName = `generate${appType.charAt(0).toUpperCase() + appType.slice(1)}Form`;
5324+
if (this[formMethodName]) {
5325+
this[formMethodName](container, settings);
5326+
formsRegenerated = true;
5327+
}
5328+
} catch (e) {
5329+
console.warn(`[SettingsForms] Failed to regenerate ${appType} form:`, e);
53045330
}
5305-
} catch (e) {
5306-
console.warn(`[SettingsForms] Failed to regenerate ${appType} form:`, e);
53075331
}
5332+
});
5333+
5334+
if (formsRegenerated) {
5335+
console.log(`[SettingsForms] Regenerated app forms to update Swaparr visibility: ${isEnabled ? 'enabled' : 'disabled'}`);
5336+
} else {
5337+
console.log(`[SettingsForms] Updated Swaparr field visibility state: ${isEnabled ? 'enabled' : 'disabled'}`);
53085338
}
53095339
});
5310-
5311-
if (formsRegenerated) {
5312-
console.log(`[SettingsForms] Regenerated app forms to update Swaparr visibility: ${isEnabled ? 'enabled' : 'disabled'}`);
5313-
} else {
5314-
console.log(`[SettingsForms] Updated Swaparr field visibility state: ${isEnabled ? 'enabled' : 'disabled'}`);
5315-
}
5340+
},
5341+
5342+
// Update Swaparr instance visibility for all loaded app forms
5343+
updateAllSwaparrInstanceVisibility: function() {
5344+
console.log('[SettingsForms] Updating Swaparr instance visibility...');
5345+
this.updateSwaparrFieldsDisabledState();
5346+
},
5347+
5348+
// Fetch and cache current Swaparr state from server
5349+
fetchAndCacheSwaparrState: function() {
5350+
return fetch('./api/settings/swaparr')
5351+
.then(response => response.json())
5352+
.then(data => {
5353+
console.log('[SettingsForms] Fetched current Swaparr state:', data);
5354+
5355+
// Update cache
5356+
try {
5357+
let cachedSettings = {};
5358+
const existing = localStorage.getItem('huntarr-settings-cache');
5359+
if (existing) {
5360+
cachedSettings = JSON.parse(existing);
5361+
}
5362+
if (!cachedSettings.swaparr) cachedSettings.swaparr = {};
5363+
cachedSettings.swaparr.enabled = data.enabled === true;
5364+
localStorage.setItem('huntarr-settings-cache', JSON.stringify(cachedSettings));
5365+
console.log('[SettingsForms] Updated Swaparr cache:', cachedSettings.swaparr);
5366+
} catch (e) {
5367+
console.warn('[SettingsForms] Failed to update Swaparr cache:', e);
5368+
}
5369+
5370+
// Update window settings if available
5371+
if (window.huntarrUI && window.huntarrUI.originalSettings) {
5372+
if (!window.huntarrUI.originalSettings.swaparr) {
5373+
window.huntarrUI.originalSettings.swaparr = {};
5374+
}
5375+
window.huntarrUI.originalSettings.swaparr.enabled = data.enabled === true;
5376+
}
5377+
5378+
return data.enabled === true;
5379+
})
5380+
.catch(error => {
5381+
console.warn('[SettingsForms] Failed to fetch Swaparr state:', error);
5382+
return false;
5383+
});
53165384
},
53175385

53185386
// Generate Notifications settings form

0 commit comments

Comments
 (0)