@@ -9,25 +9,41 @@ const SettingsForms = {
9
9
isSwaparrGloballyEnabled : function ( ) {
10
10
// Try to get Swaparr settings from cache or current settings
11
11
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
+
12
19
// Check if we have cached settings
13
20
const cachedSettings = localStorage . getItem ( 'huntarr-settings-cache' ) ;
14
21
if ( cachedSettings ) {
15
22
const settings = JSON . parse ( cachedSettings ) ;
16
23
if ( settings . swaparr && settings . swaparr . enabled !== undefined ) {
24
+ console . log ( `[SettingsForms] Reading Swaparr state from cache: ${ settings . swaparr . enabled } ` ) ;
17
25
return settings . swaparr . enabled === true ;
18
26
}
19
27
}
20
28
21
29
// Fallback: check if we have current settings loaded
22
30
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 } ` ) ;
23
32
return window . huntarrUI . originalSettings . swaparr . enabled === true ;
24
33
}
25
34
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 ;
28
44
} catch ( e ) {
29
45
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
31
47
}
32
48
} ,
33
49
@@ -3358,6 +3374,10 @@ const SettingsForms = {
3358
3374
Object . assign ( originalSettings , updatedSettings ) ;
3359
3375
}
3360
3376
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
+
3361
3381
} ) . catch ( error => {
3362
3382
console . error ( '[SettingsForms] Swaparr manual save failed:' , error ) ;
3363
3383
@@ -5278,41 +5298,89 @@ const SettingsForms = {
5278
5298
5279
5299
// Update visibility of Swaparr fields in all app forms based on global Swaparr setting
5280
5300
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 ) ;
5304
5330
}
5305
- } catch ( e ) {
5306
- console . warn ( `[SettingsForms] Failed to regenerate ${ appType } form:` , e ) ;
5307
5331
}
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' } ` ) ;
5308
5338
}
5309
5339
} ) ;
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
+ } ) ;
5316
5384
} ,
5317
5385
5318
5386
// Generate Notifications settings form
0 commit comments