|
| 1 | +// Load current settings on page load |
| 2 | +document.addEventListener('DOMContentLoaded', async () => { |
| 3 | + try { |
| 4 | + const response = await fetch('/settings/api/da-config'); |
| 5 | + const config = await response.json(); |
| 6 | + |
| 7 | + if (config.da_server) document.getElementById('da_server').value = config.da_server; |
| 8 | + if (config.da_username) document.getElementById('da_username').value = config.da_username; |
| 9 | + if (config.da_domain) document.getElementById('da_domain').value = config.da_domain; |
| 10 | + |
| 11 | + if (config.has_password) { |
| 12 | + document.getElementById('da_password').placeholder = 'Password is set (leave empty to keep current)'; |
| 13 | + } |
| 14 | + } catch (error) { |
| 15 | + console.error('Error loading settings:', error); |
| 16 | + } |
| 17 | +}); |
| 18 | + |
| 19 | +// Handle form submission |
| 20 | +document.getElementById('daConfigForm').addEventListener('submit', async (e) => { |
| 21 | + e.preventDefault(); |
| 22 | + |
| 23 | + const formData = { |
| 24 | + da_server: document.getElementById('da_server').value, |
| 25 | + da_username: document.getElementById('da_username').value, |
| 26 | + da_password: document.getElementById('da_password').value, |
| 27 | + da_domain: document.getElementById('da_domain').value |
| 28 | + }; |
| 29 | + |
| 30 | + try { |
| 31 | + const response = await fetch('/settings/api/da-config', { |
| 32 | + method: 'POST', |
| 33 | + headers: { |
| 34 | + 'Content-Type': 'application/json', |
| 35 | + }, |
| 36 | + body: JSON.stringify(formData) |
| 37 | + }); |
| 38 | + |
| 39 | + const result = await response.json(); |
| 40 | + |
| 41 | + if (response.ok) { |
| 42 | + alert('Settings saved successfully!'); |
| 43 | + // Clear password field |
| 44 | + document.getElementById('da_password').value = ''; |
| 45 | + document.getElementById('da_password').placeholder = 'Password is set (leave empty to keep current)'; |
| 46 | + |
| 47 | + // Redirect to dashboard after successful save |
| 48 | + setTimeout(() => { |
| 49 | + window.location.href = '/dashboard'; |
| 50 | + }, 1000); |
| 51 | + } else { |
| 52 | + alert(result.error || 'Failed to save settings'); |
| 53 | + } |
| 54 | + } catch (error) { |
| 55 | + console.error('Error saving settings:', error); |
| 56 | + alert('Error saving settings'); |
| 57 | + } |
| 58 | +}); |
| 59 | + |
| 60 | +// Test connection function |
| 61 | +async function testConnection() { |
| 62 | + const formData = { |
| 63 | + da_server: document.getElementById('da_server').value, |
| 64 | + da_username: document.getElementById('da_username').value, |
| 65 | + da_password: document.getElementById('da_password').value |
| 66 | + }; |
| 67 | + |
| 68 | + if (!formData.da_server || !formData.da_username) { |
| 69 | + alert('Please enter server URL and username'); |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + if (!formData.da_password && !confirm('No password entered. Test with saved password?')) { |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + try { |
| 78 | + const response = await fetch('/settings/api/test-connection', { |
| 79 | + method: 'POST', |
| 80 | + headers: { |
| 81 | + 'Content-Type': 'application/json', |
| 82 | + }, |
| 83 | + body: JSON.stringify(formData) |
| 84 | + }); |
| 85 | + |
| 86 | + const result = await response.json(); |
| 87 | + |
| 88 | + if (response.ok) { |
| 89 | + alert('✓ ' + result.message); |
| 90 | + } else { |
| 91 | + alert('✗ Connection failed: ' + (result.error || 'Unknown error')); |
| 92 | + } |
| 93 | + } catch (error) { |
| 94 | + console.error('Error testing connection:', error); |
| 95 | + alert('✗ Connection test failed'); |
| 96 | + } |
| 97 | +} |
0 commit comments