1- // Helper function to safely parse JSON
2- async function parseResponse ( response ) {
3- const text = await response . text ( ) ;
4- try {
5- return JSON . parse ( text ) ;
6- } catch ( e ) {
7- console . error ( 'Response is not JSON:' , text ) ;
8- throw new Error ( 'Server returned invalid response (not JSON)' ) ;
9- }
10- }
11-
121// Load current settings on page load
132document . addEventListener ( 'DOMContentLoaded' , async ( ) => {
143 try {
@@ -17,7 +6,7 @@ document.addEventListener('DOMContentLoaded', async () => {
176 throw new Error ( `HTTP error! status: ${ response . status } ` ) ;
187 }
198
20- const config = await parseResponse ( response ) ;
9+ const config = await response . json ( ) ;
2110 console . log ( 'Loaded config:' , config ) ;
2211
2312 if ( config . da_server ) document . getElementById ( 'da_server' ) . value = config . da_server ;
@@ -29,111 +18,144 @@ document.addEventListener('DOMContentLoaded', async () => {
2918 }
3019 } catch ( error ) {
3120 console . error ( 'Error loading settings:' , error ) ;
32- if ( error . message . includes ( 'not JSON' ) ) {
33- alert ( 'Session expired. Please login again.' ) ;
34- window . location . href = '/login' ;
35- }
3621 }
3722} ) ;
3823
39- // Handle form submission
24+ // Handle form submission - SAVE WITHOUT TESTING
4025document . getElementById ( 'daConfigForm' ) . addEventListener ( 'submit' , async ( e ) => {
4126 e . preventDefault ( ) ;
4227
28+ const saveButton = e . target . querySelector ( 'button[type="submit"]' ) ;
29+ const originalText = saveButton . textContent ;
30+ saveButton . textContent = 'Saving...' ;
31+ saveButton . disabled = true ;
32+
4333 const formData = {
4434 da_server : document . getElementById ( 'da_server' ) . value . trim ( ) ,
4535 da_username : document . getElementById ( 'da_username' ) . value . trim ( ) ,
4636 da_password : document . getElementById ( 'da_password' ) . value ,
4737 da_domain : document . getElementById ( 'da_domain' ) . value . trim ( )
4838 } ;
4939
50- console . log ( 'Submitting settings:' , { ...formData , da_password : '***' } ) ;
40+ console . log ( 'Submitting settings (no connection test) :' , { ...formData , da_password : '***' } ) ;
5141
5242 try {
5343 const response = await fetch ( '/settings/api/da-config' , {
5444 method : 'POST' ,
5545 headers : {
5646 'Content-Type' : 'application/json' ,
5747 } ,
58- credentials : 'same-origin' , // Important for cookies!
48+ credentials : 'same-origin' ,
5949 body : JSON . stringify ( formData )
6050 } ) ;
6151
62- const result = await parseResponse ( response ) ;
52+ const result = await response . json ( ) ;
6353 console . log ( 'Save response:' , result ) ;
6454
6555 if ( response . ok && result . success ) {
66- alert ( 'Settings saved successfully!' ) ;
56+ // Show success message
57+ showMessage ( 'success' , result . message || 'Settings saved successfully!' ) ;
58+
59+ // Clear password field
6760 document . getElementById ( 'da_password' ) . value = '' ;
6861 document . getElementById ( 'da_password' ) . placeholder = 'Password is set (leave empty to keep current)' ;
6962
63+ // Optional: Redirect after a delay
7064 setTimeout ( ( ) => {
71- window . location . href = '/dashboard' ;
65+ if ( confirm ( 'Settings saved! Go to dashboard now?' ) ) {
66+ window . location . href = '/dashboard' ;
67+ }
7268 } , 1000 ) ;
7369 } else {
74- console . error ( 'Save failed:' , result ) ;
75- alert ( result . error || 'Failed to save settings' ) ;
70+ showMessage ( 'error' , result . error || 'Failed to save settings' ) ;
71+
72+ // Highlight missing fields if any
73+ if ( result . missing_fields ) {
74+ result . missing_fields . forEach ( field => {
75+ document . getElementById ( field ) . classList . add ( 'error' ) ;
76+ } ) ;
77+ }
7678 }
7779 } catch ( error ) {
7880 console . error ( 'Error saving settings:' , error ) ;
79- alert ( 'Error saving settings: ' + error . message ) ;
81+ showMessage ( 'error' , 'Error saving settings: ' + error . message ) ;
82+ } finally {
83+ saveButton . textContent = originalText ;
84+ saveButton . disabled = false ;
8085 }
8186} ) ;
8287
83- // Test connection function
88+ // Test connection function - COMPLETELY SEPARATE
8489async function testConnection ( ) {
90+ const testButton = event . target ;
91+ const originalText = testButton . textContent ;
92+ testButton . textContent = 'Testing...' ;
93+ testButton . disabled = true ;
94+
8595 const formData = {
8696 da_server : document . getElementById ( 'da_server' ) . value . trim ( ) ,
8797 da_username : document . getElementById ( 'da_username' ) . value . trim ( ) ,
88- da_password : document . getElementById ( 'da_password' ) . value
98+ da_password : document . getElementById ( 'da_password' ) . value ,
99+ da_domain : document . getElementById ( 'da_domain' ) . value . trim ( )
89100 } ;
90101
91102 if ( ! formData . da_server || ! formData . da_username ) {
92- alert ( 'Please enter server URL and username' ) ;
93- return ;
94- }
95-
96- // Ensure URL has protocol
97- if ( ! formData . da_server . startsWith ( 'http://' ) && ! formData . da_server . startsWith ( 'https://' ) ) {
98- formData . da_server = 'https://' + formData . da_server ;
99- document . getElementById ( 'da_server' ) . value = formData . da_server ;
100- }
101-
102- if ( ! formData . da_password && ! confirm ( 'No password entered. Test with saved password?' ) ) {
103+ showMessage ( 'warning' , 'Please enter server URL and username to test' ) ;
104+ testButton . textContent = originalText ;
105+ testButton . disabled = false ;
103106 return ;
104107 }
105108
106109 console . log ( 'Testing connection to:' , formData . da_server ) ;
107110
108- // Show loading state
109- const originalText = event . target . textContent ;
110- event . target . textContent = 'Testing...' ;
111- event . target . disabled = true ;
112-
113111 try {
114112 const response = await fetch ( '/settings/api/test-connection' , {
115113 method : 'POST' ,
116114 headers : {
117115 'Content-Type' : 'application/json' ,
118116 } ,
119- credentials : 'same-origin' , // Important!
117+ credentials : 'same-origin' ,
120118 body : JSON . stringify ( formData )
121119 } ) ;
122120
123- const result = await parseResponse ( response ) ;
121+ const result = await response . json ( ) ;
124122 console . log ( 'Test response:' , result ) ;
125123
126- if ( response . ok && result . success ) {
127- alert ( '✓ ' + result . message ) ;
124+ if ( result . success ) {
125+ showMessage ( 'success' , '✓ ' + result . message ) ;
128126 } else {
129- alert ( ' ✗ Connection failed: ' + ( result . error || 'Unknown error' ) ) ;
127+ showMessage ( 'warning' , ' ✗ Connection failed: ' + ( result . error || 'Unknown error' ) + '\nYou can still save these settings.' ) ;
130128 }
131129 } catch ( error ) {
132130 console . error ( 'Error testing connection:' , error ) ;
133- alert ( '✗ Connection test failed : ' + error . message ) ;
131+ showMessage ( 'error' , '✗ Test error : ' + error . message + '\nYou can still save these settings.' ) ;
134132 } finally {
135- // Restore button state
136- event . target . textContent = originalText ;
137- event . target . disabled = false ;
133+ testButton . textContent = originalText ;
134+ testButton . disabled = false ;
138135 }
139136}
137+
138+ // Helper function to show messages
139+ function showMessage ( type , message ) {
140+ // Remove any existing messages
141+ const existingMsg = document . querySelector ( '.message' ) ;
142+ if ( existingMsg ) existingMsg . remove ( ) ;
143+
144+ const msgDiv = document . createElement ( 'div' ) ;
145+ msgDiv . className = `message message-${ type } ` ;
146+ msgDiv . textContent = message ;
147+
148+ // Insert after form title
149+ const formTitle = document . querySelector ( 'h2' ) ;
150+ formTitle . parentNode . insertBefore ( msgDiv , formTitle . nextSibling ) ;
151+
152+ // Auto-remove after 5 seconds
153+ setTimeout ( ( ) => msgDiv . remove ( ) , 5000 ) ;
154+ }
155+
156+ // Remove error class on input
157+ document . querySelectorAll ( 'input' ) . forEach ( input => {
158+ input . addEventListener ( 'input' , ( ) => {
159+ input . classList . remove ( 'error' ) ;
160+ } ) ;
161+ } ) ;
0 commit comments