109 lines
3.1 KiB
JavaScript
109 lines
3.1 KiB
JavaScript
// Map config keys to input IDs
|
|
const CONFIG_FIELDS = {
|
|
"TORN_API_KEY": "torn-api-key",
|
|
"FFSCOUTER_KEY": "ffscouter-key",
|
|
"DISCORD_TOKEN": "discord-token",
|
|
"ALLOWED_CHANNEL_ID": "allowed-channel-id",
|
|
"POLL_INTERVAL": "poll-interval",
|
|
"HIT_CHECK_INTERVAL": "hit-check-interval",
|
|
"REASSIGN_DELAY": "reassign-delay",
|
|
"ASSIGNMENT_TIMEOUT": "assignment-timeout",
|
|
"ASSIGNMENT_REMINDER": "assignment-reminder"
|
|
};
|
|
|
|
let sensitiveFields = [];
|
|
|
|
async function loadConfig() {
|
|
try {
|
|
const res = await fetch("/api/config", { cache: "no-store" });
|
|
if (!res.ok) {
|
|
console.error("Failed to load config:", res.status);
|
|
return;
|
|
}
|
|
|
|
const data = await res.json();
|
|
sensitiveFields = data.sensitive_fields || [];
|
|
|
|
// Populate form fields
|
|
for (const [key, inputId] of Object.entries(CONFIG_FIELDS)) {
|
|
const input = document.getElementById(inputId);
|
|
if (input && data.config[key] !== undefined) {
|
|
input.value = data.config[key];
|
|
|
|
// Add placeholder for masked sensitive fields
|
|
if (sensitiveFields.includes(key) && String(data.config[key]).startsWith("****")) {
|
|
input.placeholder = "Current: " + data.config[key];
|
|
input.value = ""; // Clear the masked value
|
|
}
|
|
}
|
|
}
|
|
} catch (err) {
|
|
console.error("Error loading config:", err);
|
|
}
|
|
}
|
|
|
|
async function saveConfigValue(key) {
|
|
const inputId = CONFIG_FIELDS[key];
|
|
const input = document.getElementById(inputId);
|
|
|
|
if (!input) {
|
|
console.error("Input not found:", inputId);
|
|
return;
|
|
}
|
|
|
|
let value = input.value.trim();
|
|
|
|
// Don't save if sensitive field is empty (means user didn't change it)
|
|
if (sensitiveFields.includes(key) && value === "") {
|
|
alert("No changes to save");
|
|
return;
|
|
}
|
|
|
|
// Convert to number if needed
|
|
if (input.type === "number") {
|
|
value = parseInt(value);
|
|
if (isNaN(value)) {
|
|
alert("Invalid number");
|
|
return;
|
|
}
|
|
}
|
|
|
|
try {
|
|
const res = await fetch("/api/config", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ key, value })
|
|
});
|
|
|
|
if (!res.ok) {
|
|
console.error("Save failed:", res.status);
|
|
alert("Failed to save config");
|
|
return;
|
|
}
|
|
|
|
const data = await res.json();
|
|
console.log("Config saved:", data);
|
|
|
|
// Reload to show masked value
|
|
await loadConfig();
|
|
alert("Saved successfully");
|
|
} catch (err) {
|
|
console.error("Error saving config:", err);
|
|
alert("Error saving config");
|
|
}
|
|
}
|
|
|
|
function wireUp() {
|
|
// Attach save handlers to all save buttons
|
|
const saveButtons = document.querySelectorAll(".config-save-btn");
|
|
saveButtons.forEach(btn => {
|
|
const key = btn.dataset.key;
|
|
btn.addEventListener("click", () => saveConfigValue(key));
|
|
});
|
|
}
|
|
|
|
document.addEventListener("DOMContentLoaded", async () => {
|
|
wireUp();
|
|
await loadConfig();
|
|
});
|