jaecoo-cihampelas/components/landing-page/galery.tsx

128 lines
3.3 KiB
TypeScript
Raw Normal View History

2025-07-13 07:48:15 +00:00
"use client";
2026-02-02 13:43:15 +00:00
import { useEffect, useState } from "react";
2025-07-13 07:48:15 +00:00
import Image from "next/image";
2026-02-02 13:43:15 +00:00
import {
getAllGaleryFiles,
getGaleryData,
getGaleryFileData,
} from "@/service/galery";
2025-07-13 07:48:15 +00:00
2026-02-02 13:43:15 +00:00
export default function GallerySection() {
const [data, setData] = useState<any[]>([]);
const [loading, setLoading] = useState(false);
2026-02-03 06:52:48 +00:00
const TABS = [
"All",
"Grand Opening",
"IIMS",
"GIIAS",
"GJAW",
"Exhibitions",
"Test Drive",
];
const [activeTab, setActiveTab] = useState("All");
2025-07-13 07:48:15 +00:00
2026-02-02 13:43:15 +00:00
const fetchData = async () => {
try {
setLoading(true);
2025-07-13 07:48:15 +00:00
2026-02-02 13:43:15 +00:00
// 1⃣ Ambil gallery (ada status_id)
const galleryRes = await getGaleryData({
limit: "100",
page: 1,
search: "",
});
2025-07-13 07:48:15 +00:00
2026-02-02 13:43:15 +00:00
const galleries = galleryRes?.data?.data ?? [];
// hanya approved
const approvedGalleries = galleries.filter((g: any) => g.status_id === 2);
// 2⃣ Ambil SEMUA files
const filesRes = await getAllGaleryFiles();
const files = filesRes?.data?.data ?? [];
// 3⃣ Mapping gallery + file berdasarkan gallery_id
const merged = approvedGalleries.map((gallery: any) => {
const file = files.find((f: any) => f.gallery_id === gallery.id);
return {
...gallery,
image_url: file?.image_url ?? null,
};
});
setData(merged);
} catch (err) {
console.error("Error fetch galeri:", err);
} finally {
setLoading(false);
}
};
useEffect(() => {
fetchData();
}, []);
2025-07-13 07:48:15 +00:00
2026-02-03 06:52:48 +00:00
const filteredData = data.filter((item) => {
if (activeTab === "All") return true;
2026-02-03 11:30:26 +00:00
return item.category === activeTab;
2026-02-03 06:52:48 +00:00
});
2025-07-13 07:48:15 +00:00
return (
<section className="py-16 px-4 max-w-[1400px] mx-auto">
<h2 className="text-4xl font-bold mb-8">Galeri Kami</h2>
2026-02-03 06:52:48 +00:00
<div className="flex justify-center gap-2 mb-10">
{TABS.map((tab) => (
<button
key={tab}
onClick={() => setActiveTab(tab)}
className={`px-4 py-2 rounded-full text-sm font-medium transition
${
activeTab === tab
? "bg-black text-white"
: "bg-gray-100 text-gray-600 hover:bg-gray-200"
}`}
>
{tab}
</button>
))}
</div>
2025-07-13 07:48:15 +00:00
2026-02-02 13:43:15 +00:00
{loading ? (
<p className="text-center">Loading...</p>
) : (
<div className="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 gap-4">
2026-02-03 06:52:48 +00:00
{filteredData.length > 0 ? (
filteredData.map((item: any) => (
<div
key={`gallery-${item.id}`}
className="relative w-full aspect-[3/2] bg-gray-100 rounded overflow-hidden"
>
{item.image_url ? (
<Image
src={item.image_url}
alt={item.title}
fill
className="object-cover"
unoptimized
/>
) : (
<div className="flex items-center justify-center h-full text-gray-400">
No Image
</div>
)}
</div>
))
) : (
<p className="col-span-full text-center text-gray-400">
Konten belum tersedia
</p>
)}
2026-02-02 13:43:15 +00:00
</div>
)}
2025-07-13 07:48:15 +00:00
</section>
);
}