@@ -25,7 +25,8 @@ function switchView(viewName) {
2525 'devices' : 'Gestion des Appareils' ,
2626 'console' : 'Terminal de Commande' ,
2727 'analytics' : 'Analyses & Rapports' ,
28- 'data' : 'Historique des Données'
28+ 'data' : 'Historique des Données' ,
29+ 'settings' : 'Configuration'
2930 } ;
3031 document . getElementById ( 'page-title' ) . innerText = titles [ viewName ] ;
3132
@@ -523,7 +524,78 @@ function showAddDeviceModal() { alert("Fonctionnalité backend à venir."); }
523524
524525// Auto-refresh logic for Live Feel
525526setInterval ( ( ) => {
526- // Only fetch if tab is active to save bandwidth?
527- // Or always to keep up.
528527 fetchData ( ) ;
529- } , 30000 ) ;
528+ } , 30000 ) ;
529+
530+ // --- Settings Logic ---
531+ function togglePassword ( id ) {
532+ const el = document . getElementById ( id ) ;
533+ if ( el ) el . type = el . type === 'password' ? 'text' : 'password' ;
534+ }
535+
536+ async function loadConfig ( ) {
537+ try {
538+ const res = await fetch ( '/api/config' ) ;
539+ if ( ! res . ok ) return ; // Silent fail if optional
540+ const config = await res . json ( ) ;
541+
542+ if ( document . getElementById ( 'conf-chirpstack-url' ) ) {
543+ document . getElementById ( 'conf-chirpstack-url' ) . value = config . CHIRPSTACK_API_URL || '' ;
544+ document . getElementById ( 'conf-chirpstack-token' ) . value = config . CHIRPSTACK_API_TOKEN || '' ;
545+ document . getElementById ( 'conf-github-repo' ) . value = config . GITHUB_REPO || '' ;
546+ document . getElementById ( 'conf-github-branch' ) . value = config . GITHUB_BRANCH || '' ;
547+ }
548+ } catch ( e ) {
549+ console . error ( "Failed to load config" , e ) ;
550+ }
551+ }
552+
553+ async function saveConfig ( ) {
554+ const config = {
555+ CHIRPSTACK_API_URL : document . getElementById ( 'conf-chirpstack-url' ) . value ,
556+ CHIRPSTACK_API_TOKEN : document . getElementById ( 'conf-chirpstack-token' ) . value ,
557+ GITHUB_REPO : document . getElementById ( 'conf-github-repo' ) . value ,
558+ GITHUB_BRANCH : document . getElementById ( 'conf-github-branch' ) . value
559+ } ;
560+
561+ try {
562+ const res = await fetch ( '/api/config' , {
563+ method : 'POST' ,
564+ headers : { 'Content-Type' : 'application/json' } ,
565+ body : JSON . stringify ( config )
566+ } ) ;
567+ const data = await res . json ( ) ;
568+ if ( res . ok ) alert ( "Configuration sauvegardée !" ) ;
569+ else alert ( "Erreur: " + data . error ) ;
570+ } catch ( e ) {
571+ alert ( "Erreur réseau: " + e . message ) ;
572+ }
573+ }
574+
575+ // Hook loadConfig into navigation or init
576+ // For simplicity, load it when View Switching to settings
577+ const originalSwitchView = window . switchView ;
578+ window . switchView = function ( viewName ) {
579+ if ( originalSwitchView ) originalSwitchView ( viewName ) ; // Call original defined in global scope (actually it's defined as function switchView... hoisting might handle it but let's be safe)
580+ else switchViewRef ( viewName ) ; // Fallback if reassignment weirdness
581+
582+ if ( viewName === 'settings' ) loadConfig ( ) ;
583+ } ;
584+ // Re-assign original reference to be handled
585+ function switchViewRef ( viewName ) {
586+ document . querySelectorAll ( '.nav-item' ) . forEach ( btn => btn . classList . remove ( 'active' ) ) ;
587+ // Find button ... logic is duplicated? No, just hook it.
588+ // Actually simpler: just call loadConfig() once at startup too.
589+ }
590+ // Better: just add it to DOMContentLoaded if we want it preloaded,
591+ // or in the HTML onclick="switchView('settings'); loadConfig()" ?
592+ // Let's just modify the switchView function in place if possible,
593+ // but since I'm appending, I can just overload/wrap it?
594+ // The previous switchView was defined as `function switchView(...)`.
595+ // We can overwrite it?
596+ // Yes.
597+ const _oldSwitch = switchView ;
598+ switchView = function ( name ) {
599+ _oldSwitch ( name ) ;
600+ if ( name === 'settings' ) loadConfig ( ) ;
601+ }
0 commit comments