110 lines
2.7 KiB
TypeScript
110 lines
2.7 KiB
TypeScript
"use client";
|
|
|
|
import Image from "next/image";
|
|
import {
|
|
Carousel,
|
|
CarouselContent,
|
|
CarouselItem,
|
|
CarouselNext,
|
|
CarouselPrevious,
|
|
} from "@/components/ui/carousel";
|
|
import { Button } from "@/components/ui/button";
|
|
import { motion } from "framer-motion";
|
|
import Link from "next/link";
|
|
import {
|
|
Dialog,
|
|
DialogContent,
|
|
DialogHeader,
|
|
DialogTitle,
|
|
DialogTrigger,
|
|
} from "../ui/dialog";
|
|
import { Input } from "../ui/input";
|
|
import { Textarea } from "../ui/textarea";
|
|
import { useEffect, useState } from "react";
|
|
import Autoplay from "embla-carousel-autoplay"; // ✅ Import plugin autoplay
|
|
import { useRef } from "react";
|
|
import { getBannerData } from "@/service/banner";
|
|
|
|
const heroImages = [
|
|
"/Hero.png",
|
|
// "/Carousell-01.png",
|
|
"/Carousell-02.png",
|
|
"/Carousell-03.png",
|
|
"/Carousell-04.png",
|
|
"/Carousell-05.png",
|
|
];
|
|
type Banner = {
|
|
id: number;
|
|
title: string;
|
|
description: string;
|
|
status_id: number;
|
|
position: string;
|
|
thumbnail_url: string;
|
|
created_at: string;
|
|
};
|
|
|
|
export default function Header() {
|
|
const [open, setOpen] = useState(false);
|
|
const [banners, setBanners] = useState<Banner[]>([]);
|
|
|
|
useEffect(() => {
|
|
const fetchBanners = async () => {
|
|
try {
|
|
const req = {
|
|
limit: "10",
|
|
page: 1,
|
|
search: "",
|
|
};
|
|
|
|
const res = await getBannerData(req);
|
|
|
|
const bannerData: Banner[] = res?.data?.data || [];
|
|
|
|
const activeBanners = bannerData
|
|
.filter((banner) => banner.status_id === 2) // ✅ approved only
|
|
.sort((a, b) => {
|
|
const posA = Number(a.position);
|
|
const posB = Number(b.position);
|
|
|
|
if (posA !== posB) return posA - posB;
|
|
|
|
return (
|
|
new Date(b.created_at).getTime() -
|
|
new Date(a.created_at).getTime()
|
|
);
|
|
});
|
|
|
|
setBanners(activeBanners);
|
|
} catch (err) {
|
|
console.error("Failed to fetch banners:", err);
|
|
}
|
|
};
|
|
|
|
fetchBanners();
|
|
}, []);
|
|
|
|
const plugin = useRef(Autoplay({ delay: 4000, stopOnInteraction: false }));
|
|
|
|
return (
|
|
<section className="relative w-full overflow-hidden bg-white mt-5">
|
|
<Carousel className="w-full relative" plugins={[plugin.current]}>
|
|
<CarouselContent>
|
|
{banners.map((banner, index) => (
|
|
<CarouselItem key={banner.id}>
|
|
<div className="relative w-full aspect-[16/9]">
|
|
<Image
|
|
src={banner.thumbnail_url}
|
|
alt={banner.title}
|
|
fill
|
|
priority={index === 0}
|
|
className="object-contain"
|
|
/>
|
|
</div>
|
|
</CarouselItem>
|
|
))}
|
|
</CarouselContent>
|
|
</Carousel>
|
|
</section>
|
|
);
|
|
}
|