// web/js/user.js // Dashboard User JavaScript - FIXED ENDPOINTS let allItems = []; let allLostItems = []; let allClaims = []; // Initialize dashboard window.addEventListener("DOMContentLoaded", async () => { const user = checkAuth(); // FIXED: Tambahkan pengecekan role yang lebih spesifik if (!user) { window.location.href = "/login"; return; } if (user.role !== "user") { // Redirect ke dashboard yang sesuai dengan role if (user.role === "admin") { window.location.href = "/admin"; } else if (user.role === "manager") { window.location.href = "/manager"; } return; } // Lanjutkan load data untuk user await loadStats(); await loadBrowseItems(); setupSearchAndFilters(); }); // Load statistics - CORRECT (sudah sesuai) async function loadStats() { try { const stats = await apiCall("/api/user/stats"); document.getElementById("statLost").textContent = stats.lost_items || 0; document.getElementById("statFound").textContent = stats.found_items || 0; document.getElementById("statClaims").textContent = stats.claims || 0; } catch (error) { console.error("Error loading stats:", error); } } // Load browse items - CORRECT (sudah sesuai) async function loadBrowseItems() { setLoading("itemsGrid", true); try { const response = await apiCall("/api/items"); allItems = response.data || []; renderItems(allItems); } catch (error) { console.error("Error loading items:", error); showEmptyState("itemsGrid", "📦", "Gagal memuat data barang"); } } // Render items function renderItems(items) { const grid = document.getElementById("itemsGrid"); if (!items || items.length === 0) { showEmptyState("itemsGrid", "📦", "Belum ada barang ditemukan"); return; } grid.innerHTML = items .map( (item) => `
${ item.description }
${claim.description}
${ claim.status === "rejected" && claim.notes ? `Alasan ditolak: ${claim.notes}
` : "" }