feat: add save functionality and validation to sonar config

This commit is contained in:
Herbert Wolverson (aider) 2025-01-22 14:44:19 -06:00
parent fe3ebbbf61
commit b2228141cc
2 changed files with 55 additions and 2 deletions

View File

@ -1,4 +1,4 @@
import {loadConfig} from "./config/config_helper"; import {saveConfig, loadConfig} from "./config/config_helper";
function arrayToString(arr) { function arrayToString(arr) {
return arr ? arr.join(', ') : ''; return arr ? arr.join(', ') : '';
@ -8,6 +8,49 @@ function stringToArray(str) {
return str ? str.split(',').map(s => s.trim()).filter(s => s.length > 0) : []; return str ? str.split(',').map(s => s.trim()).filter(s => s.length > 0) : [];
} }
function validateConfig() {
// Validate required fields when enabled
if (document.getElementById("enableSonar").checked) {
const apiUrl = document.getElementById("sonarApiUrl").value.trim();
if (!apiUrl) {
alert("API URL is required when Sonar integration is enabled");
return false;
}
try {
new URL(apiUrl);
} catch {
alert("API URL must be a valid URL");
return false;
}
const apiKey = document.getElementById("sonarApiKey").value.trim();
if (!apiKey) {
alert("API Key is required when Sonar integration is enabled");
return false;
}
const snmpCommunity = document.getElementById("snmpCommunity").value.trim();
if (!snmpCommunity) {
alert("SNMP Community is required when Sonar integration is enabled");
return false;
}
}
return true;
}
function updateConfig() {
// Update only the sonar_integration section
window.config.sonar_integration = {
enable_sonar: document.getElementById("enableSonar").checked,
sonar_api_url: document.getElementById("sonarApiUrl").value.trim(),
sonar_api_key: document.getElementById("sonarApiKey").value.trim(),
snmp_community: document.getElementById("snmpCommunity").value.trim(),
airmax_model_ids: stringToArray(document.getElementById("airmaxModelIds").value),
ltu_model_ids: stringToArray(document.getElementById("ltuModelIds").value),
active_status_ids: stringToArray(document.getElementById("activeStatusIds").value)
};
}
loadConfig(() => { loadConfig(() => {
// window.config now contains the configuration. // window.config now contains the configuration.
// Populate form fields with config values // Populate form fields with config values
@ -33,6 +76,16 @@ loadConfig(() => {
arrayToString(sonar.ltu_model_ids); arrayToString(sonar.ltu_model_ids);
document.getElementById("activeStatusIds").value = document.getElementById("activeStatusIds").value =
arrayToString(sonar.active_status_ids); arrayToString(sonar.active_status_ids);
// Add save button click handler
document.getElementById('saveButton').addEventListener('click', () => {
if (validateConfig()) {
updateConfig();
saveConfig(() => {
alert("Configuration saved successfully!");
});
}
});
} else { } else {
console.error("Sonar integration configuration not found in window.config"); console.error("Sonar integration configuration not found in window.config");
} }

View File

@ -66,7 +66,7 @@
<div class="form-text">Comma-separated list of active status IDs</div> <div class="form-text">Comma-separated list of active status IDs</div>
</div> </div>
<button type="submit" class="btn btn-outline-primary">Save Changes</button> <button type="button" id="saveButton" class="btn btn-outline-primary">Save Changes</button>
</form> </form>
</div> </div>
</div> </div>