64 lines
2.3 KiB
JavaScript
64 lines
2.3 KiB
JavaScript
/**
|
|
* Dashboard: content count cards and the latest news list.
|
|
*/
|
|
(function () {
|
|
"use strict";
|
|
|
|
const CARDS = [
|
|
{ id: "stat-news", endpoint: "/api/v1/admin/news", listKey: "news" },
|
|
{ id: "stat-categories", endpoint: "/api/v1/admin/categories", listKey: "categories" },
|
|
{ id: "stat-banners", endpoint: "/api/v1/admin/banner", listKey: "banners" },
|
|
{ id: "stat-static", endpoint: "/api/v1/admin/static", listKey: "statics" },
|
|
{ id: "stat-currency", endpoint: "/api/v1/admin/currency", listKey: "currencies" },
|
|
];
|
|
|
|
document.addEventListener("DOMContentLoaded", () => {
|
|
if (document.body.dataset.page !== "dashboard") return;
|
|
if (!Auth.requireAuth()) return;
|
|
|
|
Auth.loadProfile();
|
|
Auth.bindLogoutButtons();
|
|
|
|
CARDS.forEach(async (card) => {
|
|
const valueEl = document.querySelector("#" + card.id + " [data-stat-value]");
|
|
if (!valueEl) return;
|
|
try {
|
|
const data = await Api.get(card.endpoint, { limit: 1, page: 1 });
|
|
valueEl.textContent = (data && data.count) || 0;
|
|
} catch (_) {
|
|
valueEl.textContent = "—";
|
|
}
|
|
});
|
|
|
|
loadLatestNews();
|
|
});
|
|
|
|
async function loadLatestNews() {
|
|
const container = document.getElementById("latest-news");
|
|
if (!container) return;
|
|
try {
|
|
const data = await Api.get("/api/v1/admin/news", { limit: 5, page: 1 });
|
|
const items = (data && data.news) || [];
|
|
if (!items.length) {
|
|
container.innerHTML =
|
|
'<li class="py-4 text-base-content/60">No news yet.</li>';
|
|
return;
|
|
}
|
|
container.innerHTML = items
|
|
.map(
|
|
(item) => `
|
|
<li class="flex items-center gap-3 py-3">
|
|
${item.imagePath ? `<img src="${UI.escapeHtml(item.imagePath)}" alt="" class="h-10 w-14 rounded object-cover" loading="lazy">` : ""}
|
|
<div class="min-w-0 flex-1">
|
|
<a href="/admin/news/${item.Id}/edit" class="block truncate font-medium hover:text-primary">${UI.escapeHtml(langText(item.title))}</a>
|
|
<span class="text-sm text-base-content/60">${UI.escapeHtml(UI.formatDate(item.dateRelease))}</span>
|
|
</div>
|
|
</li>`
|
|
)
|
|
.join("");
|
|
} catch (error) {
|
|
container.innerHTML = `<li class="py-4 text-error">${UI.escapeHtml(error.message)}</li>`;
|
|
}
|
|
}
|
|
})();
|