feat: add save functionality to long-term stats config with validation

This commit is contained in:
Herbert Wolverson (aider)
2025-01-22 14:28:54 -06:00
parent cd3d4d41a2
commit 895e378f08
2 changed files with 54 additions and 8 deletions

View File

@@ -1,4 +1,44 @@
import {loadConfig} from "./config/config_helper";
import {saveConfig, loadConfig} from "./config/config_helper";
function validateConfig() {
// Validate numeric fields
const collationPeriod = parseInt(document.getElementById("collationPeriod").value);
if (isNaN(collationPeriod) || collationPeriod < 1) {
alert("Collation Period must be a number greater than 0");
return false;
}
const uispInterval = parseInt(document.getElementById("uispInterval").value);
if (isNaN(uispInterval) || uispInterval < 0) {
alert("UISP Reporting Interval must be a number of at least 0");
return false;
}
// Validate URL format if provided
const ltsUrl = document.getElementById("ltsUrl").value.trim();
if (ltsUrl) {
try {
new URL(ltsUrl);
} catch {
alert("LTS Server URL must be a valid URL");
return false;
}
}
return true;
}
function updateConfig() {
// Update only the long-term stats section
window.config.long_term_stats = {
gather_stats: document.getElementById("gatherStats").checked,
collation_period_seconds: parseInt(document.getElementById("collationPeriod").value),
license_key: document.getElementById("licenseKey").value.trim() || null,
uisp_reporting_interval_seconds: parseInt(document.getElementById("uispInterval").value) || null,
lts_url: document.getElementById("ltsUrl").value.trim() || null,
use_insight: document.getElementById("useInsight").checked
};
}
loadConfig(() => {
// window.config now contains the configuration.
@@ -11,16 +51,22 @@ loadConfig(() => {
document.getElementById("useInsight").checked = lts.use_insight ?? false;
// Numeric fields
if (lts.collation_period_seconds) {
document.getElementById("collationPeriod").value = lts.collation_period_seconds;
}
if (lts.uisp_reporting_interval_seconds) {
document.getElementById("uispInterval").value = lts.uisp_reporting_interval_seconds;
}
document.getElementById("collationPeriod").value = lts.collation_period_seconds ?? 60;
document.getElementById("uispInterval").value = lts.uisp_reporting_interval_seconds ?? 300;
// Optional string fields
document.getElementById("licenseKey").value = lts.license_key ?? "";
document.getElementById("ltsUrl").value = lts.lts_url ?? "";
// Add save button click handler
document.getElementById('saveButton').addEventListener('click', () => {
if (validateConfig()) {
updateConfig();
saveConfig(() => {
alert("Configuration saved successfully!");
});
}
});
} else {
console.error("Long-term stats configuration not found in window.config");
}

View File

@@ -60,7 +60,7 @@
<div class="form-text">Experimental next-gen statistics system (alpha)</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>
</div>
</div>