function getQueryParam(name) {
return new URLSearchParams(window.location.search).get(name);
}
function formatTanggal(dateStr) {
return new Date(dateStr).toLocaleDateString("id-ID", {
day: "numeric",
month: "long",
year: "numeric"
});
}
function loadDetail() {
const id = getQueryParam("id");
fetch(`/WebNew/Berita/GetDetail?id=${id}`)
.then(res => {
if (!res.ok) throw new Error("Data tidak ditemukan");
return res.json();
})
.then(data => {
if (!data) {
document.getElementById("judul").innerText = "Data tidak ditemukan";
return;
}
document.getElementById("judul").innerText = data.judul || "-";
document.getElementById("tanggal").innerText = formatTanggal(data.tanggal || "");
renderSlider(data.gambar);
document.getElementById("isi").innerHTML =
(data.isi || "").replace(/\n/g, "
");
})
.catch(err => {
console.error(err);
document.getElementById("judul").innerText = "Gagal load berita";
});
}
function loadSidebar() {
fetch('/WebNew/Berita/GetAll')
.then(res => res.json())
.then(data => {
let html = "";
data.slice(0, 4).forEach(item => {
let gambar = Array.isArray(item.gambar) ? item.gambar[0] : item.gambar;
html += `