Skip to content

Commit ee9e967

Browse files
Update settings.js
1 parent abae415 commit ee9e967

File tree

1 file changed

+40
-7
lines changed

1 file changed

+40
-7
lines changed

static/settings.js

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
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+
112
// Load current settings on page load
213
document.addEventListener('DOMContentLoaded', async () => {
314
try {
415
const response = await fetch('/settings/api/da-config');
5-
const config = await response.json();
16+
if (!response.ok) {
17+
throw new Error(`HTTP error! status: ${response.status}`);
18+
}
619

20+
const config = await parseResponse(response);
721
console.log('Loaded config:', config);
822

923
if (config.da_server) document.getElementById('da_server').value = config.da_server;
@@ -15,6 +29,10 @@ document.addEventListener('DOMContentLoaded', async () => {
1529
}
1630
} catch (error) {
1731
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+
}
1836
}
1937
});
2038

@@ -37,19 +55,18 @@ document.getElementById('daConfigForm').addEventListener('submit', async (e) =>
3755
headers: {
3856
'Content-Type': 'application/json',
3957
},
58+
credentials: 'same-origin', // Important for cookies!
4059
body: JSON.stringify(formData)
4160
});
4261

43-
const result = await response.json();
62+
const result = await parseResponse(response);
4463
console.log('Save response:', result);
4564

46-
if (response.ok) {
65+
if (response.ok && result.success) {
4766
alert('Settings saved successfully!');
48-
// Clear password field
4967
document.getElementById('da_password').value = '';
5068
document.getElementById('da_password').placeholder = 'Password is set (leave empty to keep current)';
5169

52-
// Redirect to dashboard after successful save
5370
setTimeout(() => {
5471
window.location.href = '/dashboard';
5572
}, 1000);
@@ -76,31 +93,47 @@ async function testConnection() {
7693
return;
7794
}
7895

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+
79102
if (!formData.da_password && !confirm('No password entered. Test with saved password?')) {
80103
return;
81104
}
82105

83106
console.log('Testing connection to:', formData.da_server);
84107

108+
// Show loading state
109+
const originalText = event.target.textContent;
110+
event.target.textContent = 'Testing...';
111+
event.target.disabled = true;
112+
85113
try {
86114
const response = await fetch('/settings/api/test-connection', {
87115
method: 'POST',
88116
headers: {
89117
'Content-Type': 'application/json',
90118
},
119+
credentials: 'same-origin', // Important!
91120
body: JSON.stringify(formData)
92121
});
93122

94-
const result = await response.json();
123+
const result = await parseResponse(response);
95124
console.log('Test response:', result);
96125

97-
if (response.ok) {
126+
if (response.ok && result.success) {
98127
alert('✓ ' + result.message);
99128
} else {
100129
alert('✗ Connection failed: ' + (result.error || 'Unknown error'));
101130
}
102131
} catch (error) {
103132
console.error('Error testing connection:', error);
104133
alert('✗ Connection test failed: ' + error.message);
134+
} finally {
135+
// Restore button state
136+
event.target.textContent = originalText;
137+
event.target.disabled = false;
105138
}
106139
}

0 commit comments

Comments
 (0)