423 lines
11 KiB
HTML
423 lines
11 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
<title>Admin Settings</title>
|
|
<style>
|
|
body {
|
|
font-family: Arial, sans-serif;
|
|
margin: 20px;
|
|
}
|
|
h1 {
|
|
color: #333;
|
|
}
|
|
h2 {
|
|
color: #555;
|
|
border-bottom: 1px solid #ddd;
|
|
padding-bottom: 5px;
|
|
}
|
|
.setting-group {
|
|
margin-bottom: 20px;
|
|
}
|
|
.setting {
|
|
margin: 5px 0;
|
|
}
|
|
.setting strong {
|
|
display: inline-block;
|
|
width: 200px;
|
|
}
|
|
.logout {
|
|
margin-top: 20px;
|
|
}
|
|
.logout a {
|
|
color: #007bff;
|
|
text-decoration: none;
|
|
}
|
|
.logout a:hover {
|
|
text-decoration: underline;
|
|
}
|
|
.settings-management {
|
|
margin-top: 40px;
|
|
padding: 20px;
|
|
border: 1px solid #ddd;
|
|
border-radius: 5px;
|
|
}
|
|
.settings-list {
|
|
margin-bottom: 20px;
|
|
}
|
|
.setting-item {
|
|
display: flex;
|
|
justify-content: space-between;
|
|
align-items: center;
|
|
padding: 10px;
|
|
border-bottom: 1px solid #eee;
|
|
}
|
|
.setting-item:last-child {
|
|
border-bottom: none;
|
|
}
|
|
.setting-info {
|
|
flex-grow: 1;
|
|
}
|
|
.setting-actions {
|
|
display: flex;
|
|
gap: 10px;
|
|
}
|
|
.btn {
|
|
padding: 5px 10px;
|
|
border: none;
|
|
border-radius: 3px;
|
|
cursor: pointer;
|
|
text-decoration: none;
|
|
display: inline-block;
|
|
}
|
|
.btn-primary {
|
|
background: #007bff;
|
|
color: white;
|
|
}
|
|
.btn-danger {
|
|
background: #dc3545;
|
|
color: white;
|
|
}
|
|
.btn-secondary {
|
|
background: #6c757d;
|
|
color: white;
|
|
}
|
|
.btn:hover {
|
|
opacity: 0.8;
|
|
}
|
|
.form-group {
|
|
margin-bottom: 15px;
|
|
}
|
|
.form-group label {
|
|
display: block;
|
|
margin-bottom: 5px;
|
|
font-weight: bold;
|
|
}
|
|
.form-group input,
|
|
.form-group select {
|
|
width: 100%;
|
|
padding: 8px;
|
|
border: 1px solid #ccc;
|
|
border-radius: 3px;
|
|
}
|
|
.form-row {
|
|
display: flex;
|
|
gap: 10px;
|
|
align-items: end;
|
|
}
|
|
.form-row .form-group {
|
|
flex: 1;
|
|
margin-bottom: 0;
|
|
}
|
|
.message {
|
|
padding: 10px;
|
|
margin: 10px 0;
|
|
border-radius: 3px;
|
|
}
|
|
.message.success {
|
|
background: #d4edda;
|
|
color: #155724;
|
|
border: 1px solid #c3e6cb;
|
|
}
|
|
.message.error {
|
|
background: #f8d7da;
|
|
color: #721c24;
|
|
border: 1px solid #f5c6cb;
|
|
}
|
|
.edit-form {
|
|
display: none;
|
|
margin-top: 10px;
|
|
padding: 10px;
|
|
background: #f8f9fa;
|
|
border-radius: 3px;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="logout">
|
|
<a
|
|
href="/admin/"
|
|
style="color: #007bff; text-decoration: none; margin-right: 20px"
|
|
>Dashboard</a
|
|
>
|
|
<a
|
|
href="/admin/submissions"
|
|
style="color: #007bff; text-decoration: none; margin-right: 20px"
|
|
>View Submissions</a
|
|
>
|
|
<a
|
|
href="{{ url_for('auth.logout') }}"
|
|
style="color: #007bff; text-decoration: none"
|
|
>Logout</a
|
|
>
|
|
</div>
|
|
|
|
<h1>Application Settings</h1>
|
|
|
|
{% for category, category_settings in settings.items() %}
|
|
<div class="setting-group">
|
|
<h2>{{ category }}</h2>
|
|
{% for key, value in category_settings.items() %}
|
|
<div class="setting"><strong>{{ key }}:</strong> {{ value }}</div>
|
|
{% endfor %}
|
|
</div>
|
|
{% endfor %}
|
|
|
|
<div class="settings-management">
|
|
<h2>Dynamic Settings Management</h2>
|
|
|
|
<div id="message"></div>
|
|
|
|
<div class="settings-list" id="settingsList">
|
|
<p>Loading settings...</p>
|
|
</div>
|
|
|
|
<h3>Add New Setting</h3>
|
|
<form id="addSettingForm">
|
|
<div class="form-row">
|
|
<div class="form-group">
|
|
<label for="newKey">Setting Key:</label>
|
|
<input
|
|
type="text"
|
|
id="newKey"
|
|
name="key"
|
|
required
|
|
placeholder="e.g., maintenance_mode"
|
|
/>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="newValue">Setting Value:</label>
|
|
<input
|
|
type="text"
|
|
id="newValue"
|
|
name="value"
|
|
required
|
|
placeholder="e.g., false"
|
|
/>
|
|
</div>
|
|
<div class="form-group">
|
|
<button type="submit" class="btn btn-primary">Add Setting</button>
|
|
</div>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
|
|
<script>
|
|
let appSettings = {};
|
|
|
|
// Load settings on page load
|
|
document.addEventListener("DOMContentLoaded", function () {
|
|
loadSettings();
|
|
});
|
|
|
|
// Handle add setting form
|
|
document
|
|
.getElementById("addSettingForm")
|
|
.addEventListener("submit", function (e) {
|
|
e.preventDefault();
|
|
const formData = new FormData(this);
|
|
const key = formData.get("key").trim();
|
|
const value = formData.get("value").trim();
|
|
|
|
if (!key || !value) {
|
|
showMessage("Both key and value are required.", "error");
|
|
return;
|
|
}
|
|
|
|
addSetting(key, value);
|
|
});
|
|
|
|
function loadSettings() {
|
|
fetch("/admin/api/settings")
|
|
.then((response) => response.json())
|
|
.then((data) => {
|
|
if (data.status === "ok") {
|
|
appSettings = data.settings;
|
|
displaySettings();
|
|
} else {
|
|
showMessage(
|
|
"Error loading settings: " + (data.message || "Unknown error"),
|
|
"error"
|
|
);
|
|
}
|
|
})
|
|
.catch((error) => {
|
|
console.error("Error:", error);
|
|
showMessage("Error loading settings", "error");
|
|
});
|
|
}
|
|
|
|
function displaySettings() {
|
|
const container = document.getElementById("settingsList");
|
|
|
|
if (Object.keys(appSettings).length === 0) {
|
|
container.innerHTML = "<p>No dynamic settings configured.</p>";
|
|
return;
|
|
}
|
|
|
|
const settingsHtml = Object.entries(appSettings)
|
|
.map(
|
|
([key, value]) => `
|
|
<div class="setting-item">
|
|
<div class="setting-info">
|
|
<strong>${escapeHtml(key)}:</strong> ${escapeHtml(value)}
|
|
</div>
|
|
<div class="setting-actions">
|
|
<button class="btn btn-secondary" onclick="editSetting('${escapeHtml(
|
|
key
|
|
)}')">Edit</button>
|
|
<button class="btn btn-danger" onclick="deleteSetting('${escapeHtml(
|
|
key
|
|
)}')">Delete</button>
|
|
</div>
|
|
</div>
|
|
<div class="edit-form" id="edit-${escapeHtml(key)}">
|
|
<div class="form-row">
|
|
<div class="form-group">
|
|
<label>Key:</label>
|
|
<input type="text" value="${escapeHtml(key)}" readonly>
|
|
</div>
|
|
<div class="form-group">
|
|
<label>New Value:</label>
|
|
<input type="text" id="edit-value-${escapeHtml(
|
|
key
|
|
)}" value="${escapeHtml(value)}" required>
|
|
</div>
|
|
<div class="form-group">
|
|
<button class="btn btn-primary" onclick="updateSetting('${escapeHtml(
|
|
key
|
|
)}')">Update</button>
|
|
<button class="btn btn-secondary" onclick="cancelEdit('${escapeHtml(
|
|
key
|
|
)}')">Cancel</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
`
|
|
)
|
|
.join("");
|
|
|
|
container.innerHTML = settingsHtml;
|
|
}
|
|
|
|
function addSetting(key, value) {
|
|
fetch(`/admin/api/settings/${encodeURIComponent(key)}`, {
|
|
method: "PUT",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify({ value: value }),
|
|
})
|
|
.then((response) => response.json())
|
|
.then((data) => {
|
|
if (data.status === "ok") {
|
|
appSettings[key] = value;
|
|
displaySettings();
|
|
document.getElementById("addSettingForm").reset();
|
|
showMessage("Setting added successfully!", "success");
|
|
} else {
|
|
showMessage(
|
|
"Error adding setting: " + (data.message || "Unknown error"),
|
|
"error"
|
|
);
|
|
}
|
|
})
|
|
.catch((error) => {
|
|
console.error("Error:", error);
|
|
showMessage("Error adding setting", "error");
|
|
});
|
|
}
|
|
|
|
function editSetting(key) {
|
|
document.getElementById(`edit-${key}`).style.display = "block";
|
|
}
|
|
|
|
function cancelEdit(key) {
|
|
document.getElementById(`edit-${key}`).style.display = "none";
|
|
}
|
|
|
|
function updateSetting(key) {
|
|
const newValue = document
|
|
.getElementById(`edit-value-${key}`)
|
|
.value.trim();
|
|
|
|
if (!newValue) {
|
|
showMessage("Value cannot be empty.", "error");
|
|
return;
|
|
}
|
|
|
|
fetch(`/admin/api/settings/${encodeURIComponent(key)}`, {
|
|
method: "PUT",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify({ value: newValue }),
|
|
})
|
|
.then((response) => response.json())
|
|
.then((data) => {
|
|
if (data.status === "ok") {
|
|
appSettings[key] = newValue;
|
|
displaySettings();
|
|
cancelEdit(key);
|
|
showMessage("Setting updated successfully!", "success");
|
|
} else {
|
|
showMessage(
|
|
"Error updating setting: " + (data.message || "Unknown error"),
|
|
"error"
|
|
);
|
|
}
|
|
})
|
|
.catch((error) => {
|
|
console.error("Error:", error);
|
|
showMessage("Error updating setting", "error");
|
|
});
|
|
}
|
|
|
|
function deleteSetting(key) {
|
|
if (!confirm(`Are you sure you want to delete the setting "${key}"?`)) {
|
|
return;
|
|
}
|
|
|
|
fetch(`/admin/api/settings/${encodeURIComponent(key)}`, {
|
|
method: "DELETE",
|
|
})
|
|
.then((response) => response.json())
|
|
.then((data) => {
|
|
if (data.status === "ok") {
|
|
delete appSettings[key];
|
|
displaySettings();
|
|
showMessage("Setting deleted successfully!", "success");
|
|
} else {
|
|
showMessage(
|
|
"Error deleting setting: " + (data.message || "Unknown error"),
|
|
"error"
|
|
);
|
|
}
|
|
})
|
|
.catch((error) => {
|
|
console.error("Error:", error);
|
|
showMessage("Error deleting setting", "error");
|
|
});
|
|
}
|
|
|
|
function showMessage(text, type) {
|
|
const messageDiv = document.getElementById("message");
|
|
messageDiv.className = `message ${type}`;
|
|
messageDiv.textContent = text;
|
|
messageDiv.style.display = "block";
|
|
|
|
setTimeout(() => {
|
|
messageDiv.style.display = "none";
|
|
}, 5000);
|
|
}
|
|
|
|
function escapeHtml(text) {
|
|
const div = document.createElement("div");
|
|
div.textContent = text;
|
|
return div.innerHTML;
|
|
}
|
|
</script>
|
|
</body>
|
|
</html>
|