photo_server_json_con_aves22/public/js/admin/ui.js.ok2
2026-04-18 20:14:42 +02:00

89 lines
2.5 KiB
Text
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// ===============================
// UI FUNCTIONS
// ===============================
// Mostra output JSON nel <pre id="out">
function out(data) {
document.getElementById("out").textContent =
JSON.stringify(data, null, 2);
}
// ===============================
// CARICA LISTA UTENTI (solo Admin)
// ===============================
async function loadUserDropdown() {
try {
const users = await apiGet(`/users`);
const sel = document.getElementById("userSelect");
if (!sel) return;
sel.innerHTML = "";
users.forEach(u => {
const opt = document.createElement("option");
opt.value = u.name;
opt.textContent = u.name;
sel.appendChild(opt);
});
} catch (err) {
console.error("Errore nel caricare gli utenti:", err);
out({ error: "Errore nel caricare gli utenti", details: err.message });
}
}
// ===============================
// TOGGLE SOFT DELETE (via prompt)
// ===============================
async function toggleSoftDeletePrompt() {
const id = prompt("Inserisci l'ID della foto da togglare (soft delete):");
if (!id) {
alert("Nessun ID inserito");
return;
}
try {
const data = await apiPost(`/photos/toggle_soft/${id}`);
out(data);
} catch (err) {
console.error("Errore toggle soft delete:", err);
out({ error: err.message });
}
}
// ===============================
// INIZIALIZZAZIONE UI
// ===============================
document.addEventListener("DOMContentLoaded", async () => {
// 1⃣ Aspetta che config.js carichi Admin.BASE_URL
await loadConfig();
// 2⃣ Inizializza pulsanti
document.getElementById("btnScan").onclick = scan;
document.getElementById("btnResetDB").onclick = resetDB;
document.getElementById("btnReadDBUser").onclick = readDBUser;
document.getElementById("btnDeletePhoto").onclick = deletePhoto;
document.getElementById("btnFindIdIndex").onclick = findIdIndex;
document.getElementById("btnResetDBuser").onclick = resetDBuser;
document.getElementById("btnSearchPhotoById").onclick = searchPhotoById;
document.getElementById("btnShowChanges").onclick = showChanges;
document.getElementById("btnBack").onclick = () => window.location.href = "index.html";
// ⭐ NUOVO: Toggle Soft Delete via prompt
document.getElementById("btnToggleSoft").onclick = toggleSoftDeletePrompt;
// 3⃣ Carica dropdown utenti SOLO se Admin
const payload = parseJwt(Admin.token);
if (payload.name === "Admin") {
loadUserDropdown();
}
});