web-mikul-news/components/landing-page/header.tsx

238 lines
8.2 KiB
TypeScript
Raw Normal View History

2025-07-02 15:44:00 +00:00
"use client";
2025-07-03 02:03:07 +00:00
2025-07-02 15:44:00 +00:00
import { getListArticle } from "@/service/article";
import { useEffect, useState } from "react";
import Image from "next/image";
2025-07-03 02:03:07 +00:00
import Link from "next/link";
2025-10-12 15:09:52 +00:00
import { getAdvertise } from "@/service/advertisement";
2025-07-02 15:44:00 +00:00
type Article = {
id: number;
title: string;
description: string;
2026-01-23 07:23:37 +00:00
publishedAt: string;
2025-07-02 15:44:00 +00:00
categoryName: string;
createdAt: string;
createdByName: string;
2025-10-28 05:38:49 +00:00
slug: string;
2025-10-12 15:09:52 +00:00
customCreatorName: string;
2025-07-02 15:44:00 +00:00
thumbnailUrl: string;
categories: {
title: string;
}[];
files: {
2025-09-24 09:01:07 +00:00
fileUrl: string;
2025-07-02 15:44:00 +00:00
file_alt: string;
}[];
};
2025-10-12 15:09:52 +00:00
type Advertise = {
id: number;
title: string;
description: string;
placement: string;
contentFileUrl: string;
redirectLink: string;
};
2025-07-02 15:44:00 +00:00
export default function HeroNewsSection() {
const [page, setPage] = useState(1);
const [totalPage, setTotalPage] = useState(1);
const [articles, setArticles] = useState<Article[]>([]);
const [showData, setShowData] = useState("5");
const [search, setSearch] = useState("");
const [selectedCategories, setSelectedCategories] = useState<any>("");
const [startDateValue, setStartDateValue] = useState({
startDate: null,
endDate: null,
});
2025-10-12 15:09:52 +00:00
const [bannerAd, setBannerAd] = useState<Advertise | null>(null);
useEffect(() => {
initStateAdver();
}, []);
async function initStateAdver() {
const req = {
limit: 100,
page: 1,
sort: "desc",
sortBy: "created_at",
isPublish: true,
};
try {
const res = await getAdvertise(req);
const data: Advertise[] = res?.data?.data || [];
// filter iklan dengan placement = "banner"
const banner = data.find((ad) => ad.placement === "banner");
if (banner) {
setBannerAd(banner);
}
} catch (err) {
console.error("Error fetching advertisement:", err);
}
}
2025-07-02 15:44:00 +00:00
useEffect(() => {
initState();
}, [page, showData, startDateValue, selectedCategories]);
async function initState() {
// loading();
const req = {
limit: showData,
page,
search,
categorySlug: Array.from(selectedCategories).join(","),
sort: "desc",
2025-08-19 05:33:21 +00:00
isPublish: true,
2025-07-02 15:44:00 +00:00
sortBy: "created_at",
};
try {
const res = await getListArticle(req);
setArticles(res?.data?.data || []);
setTotalPage(res?.data?.meta?.totalPage || 1);
} finally {
2025-07-03 02:03:07 +00:00
// close();
2025-07-02 15:44:00 +00:00
}
}
return (
<section className="max-w-7xl mx-auto bg-white">
<div className="flex items-center bg-[#F2F4F3] w-full overflow-hidden mb-4 py-6 px-8">
<Image
src="/mikul.png"
alt="Background"
width={272}
height={90}
2025-07-03 02:03:07 +00:00
className="w-full md:w-[272px] h-[90px] object-cover border"
2025-07-02 15:44:00 +00:00
priority
/>
</div>
<div className="pb-5">
2025-07-03 08:09:16 +00:00
<div className="grid grid-cols-1 md:grid-cols-3 lg:grid-cols-5 gap-2 m-8 ">
2025-07-02 15:44:00 +00:00
{articles.length > 0 && (
<div className="md:col-span-2 lg:col-span-3 relative">
2025-11-03 02:17:06 +00:00
<Link href={`/details/${articles[0]?.slug}`}>
2025-07-02 15:44:00 +00:00
<Image
src={
2025-09-24 09:01:07 +00:00
articles[0]?.files?.[0]?.fileUrl ||
articles[0]?.files?.[0]?.fileUrl ||
2025-07-02 15:44:00 +00:00
"/default-image.jpg"
}
2025-07-03 02:03:07 +00:00
alt={articles[0].title}
width={800}
height={500}
className="w-full h-full max-h-[460px] object-cover"
2025-07-02 15:44:00 +00:00
/>
2025-07-03 02:03:07 +00:00
<div className="absolute inset-0 bg-gradient-to-t from-black/70 via-black/50 to-transparent p-6 flex flex-col justify-end">
<span className="text-xs bg-yellow-400 text-black px-2 py-0.5 inline-block mb-2 uppercase w-[130px]">
{articles[0].categories?.[0]?.title || "TANPA KATEGORI"}
2025-07-02 15:44:00 +00:00
</span>
2025-07-03 02:03:07 +00:00
<h2 className="text-sm md:text-xl lg:text-2xl font-bold text-white leading-snug mb-2 w-full md:w-9/12">
{articles[0].title}
</h2>
2025-07-04 02:47:42 +00:00
<p className="text-white text-xs flex items-center gap-2">
2025-10-12 15:09:52 +00:00
{articles[0]?.customCreatorName ||
articles[0]?.createdByName}{" "}
-{" "}
2025-07-04 02:47:42 +00:00
<svg
xmlns="http://www.w3.org/2000/svg"
width="16"
height="16"
viewBox="0 0 24 24"
>
<g fill="none">
<path d="m12.593 23.258l-.011.002l-.071.035l-.02.004l-.014-.004l-.071-.035q-.016-.005-.024.005l-.004.01l-.017.428l.005.02l.01.013l.104.074l.015.004l.012-.004l.104-.074l.012-.016l.004-.017l-.017-.427q-.004-.016-.017-.018m.265-.113l-.013.002l-.185.093l-.01.01l-.003.011l.018.43l.005.012l.008.007l.201.093q.019.005.029-.008l.004-.014l-.034-.614q-.005-.018-.02-.022m-.715.002a.02.02 0 0 0-.027.006l-.006.014l-.034.614q.001.018.017.024l.015-.002l.201-.093l.01-.008l.004-.011l.017-.43l-.003-.012l-.01-.01z" />
<path
fill="currentColor"
d="M12 2c5.523 0 10 4.477 10 10s-4.477 10-10 10S2 17.523 2 12S6.477 2 12 2m0 2a8 8 0 1 0 0 16a8 8 0 0 0 0-16m0 2a1 1 0 0 1 .993.883L13 7v4.586l2.707 2.707a1 1 0 0 1-1.32 1.497l-.094-.083l-3-3a1 1 0 0 1-.284-.576L11 12V7a1 1 0 0 1 1-1"
/>
</g>
</svg>{" "}
2026-01-23 07:23:37 +00:00
{new Date(articles[0].publishedAt)
.toLocaleString("id-ID", {
2025-07-03 02:03:07 +00:00
day: "numeric",
month: "long",
year: "numeric",
2026-01-23 07:23:37 +00:00
hour: "2-digit",
minute: "2-digit",
hour12: false,
timeZone: "Asia/Jakarta",
})
.replace("pukul ", "")}{" "}
WIB
2025-07-03 02:03:07 +00:00
</p>
2025-07-02 15:44:00 +00:00
</div>
2025-07-03 02:03:07 +00:00
</Link>
</div>
)}
<div className="md:col-span-1 lg:col-span-2 grid grid-cols-1 sm:grid-cols-2 gap-2">
{articles.slice(1, 5).map((article, index) => (
<div key={index} className="relative">
2025-11-03 02:17:06 +00:00
<Link href={`/details/${article?.slug}`}>
2025-07-03 02:03:07 +00:00
<Image
src={
2025-10-28 06:21:49 +00:00
article.thumbnailUrl ||
2025-09-24 09:01:07 +00:00
article?.files?.[0]?.fileUrl ||
2025-07-03 02:03:07 +00:00
"/default-image.jpg"
}
alt={article.title}
width={400}
height={240}
className="w-full h-56 object-cover"
/>
<div className="absolute inset-0 bg-gradient-to-t from-black/70 via-black/40 to-transparent p-4 flex flex-col justify-end">
<span className="text-xs bg-yellow-400 text-black px-2 py-0.5 inline-block uppercase w-[130px]">
2025-09-02 03:22:35 +00:00
{article?.categories?.[0]?.title || "TANPA KATEGORI"}
2025-07-03 02:03:07 +00:00
</span>
<h3 className="text-sm font-semibold text-white leading-snug mb-1">
{article.title}
</h3>
</div>
</Link>
2025-07-02 15:44:00 +00:00
</div>
))}
</div>
</div>
2025-10-12 15:09:52 +00:00
<div className="relative mt-10 mb-2 flex justify-center mx-8 border my-8 h-[350px] overflow-hidden bg-white">
{bannerAd ? (
<a
href={bannerAd.redirectLink}
target="_blank"
rel="noopener noreferrer"
className="block w-full"
>
<div className="relative w-full h-[350px] flex justify-center">
<Image
src={bannerAd.contentFileUrl}
alt={bannerAd.title || "Iklan Banner"}
width={1200} // ukuran dasar untuk responsive
height={350}
className="object-cover w-full h-full"
/>
</div>
</a>
) : (
<Image
src="/image-kolom.png"
alt="Berita Utama"
width={1200}
height={188}
className="object-contain w-full h-[188px]"
/>
)}
2025-07-02 15:44:00 +00:00
</div>
</div>
</section>
);
}