diff --git a/components/home/HomePhotoGallerySection.tsx b/components/home/HomePhotoGallerySection.tsx index 95d0dc6..c0a7b66 100644 --- a/components/home/HomePhotoGallerySection.tsx +++ b/components/home/HomePhotoGallerySection.tsx @@ -1,7 +1,8 @@ 'use client'; +import axios from 'axios'; import Link from 'next/link'; -import React, { useState } from 'react'; +import React, { useEffect, useState } from 'react'; import Lightbox from "yet-another-react-lightbox"; import "yet-another-react-lightbox/styles.css"; @@ -13,31 +14,62 @@ interface GalleryImage { } export default function HomePhotoGallerySection() { - const galleryImages: GalleryImage[] = [ - { src: "/assets/img/home/photos-gallery/christmas.webp", delay: 800, year: 2025 }, - { src: "/assets/img/home/photos-gallery/m-g.webp", delay: 1000, year: 2025 }, - { src: "/assets/img/home/photos-gallery/thai-pongal.webp", delay: 1000, year: 2004 }, - { src: "/assets/img/home/photos-gallery/multicultural.webp", delay: 1000, year: 2023 }, - { src: "/assets/img/home/photos-gallery/sports.webp", delay: 1000, year: 2024 }, - { src: "/assets/img/home/photos-gallery/tamil-new-yr.webp", delay: 1000, year: 2022 }, - - { src: "/assets/img/home/photos-gallery/kw.webp", delay: 1200, year: 1989 }, - { src: "/assets/img/home/photos-gallery/tamil-heritage.webp", delay: 800, year: 2025 }, - ]; - + const [galleryImages, setGalleryImages] = useState([]); + const [years, setYears] = useState([]); + const [activeYear, setActiveYear] = useState(null); const [open, setOpen] = useState(false); const [currentIndex, setCurrentIndex] = useState(0); - const [activeTab, setActiveTab] = useState<'All' | number>('All'); - const years = Array.from(new Set(galleryImages.map(img => img.year))).sort((a, b) => b - a); - const tabs: (number | 'All')[] = ['All', ...years]; + useEffect(() => { + getEvents(); + }, []); - const filteredImages = activeTab === 'All' - ? galleryImages - : galleryImages.filter(img => img.year === activeTab); + const getEvents = async () => { + try { + const eventRes = await axios.get( + 'https://api.tamilculturewaterloo.org/api/events/' + ); + + const eventsData = eventRes?.data?.data || []; + + // Extract images with year (flattened array) + const formatted = eventsData.map((img: any) => ({ + id: img.id, + src: img.eventimageurl, + title: img?.eventtitle || '', + description: img?.eventdescription || '', + year: img?.year + })); + + setGalleryImages(formatted); + + // Unique years, sorted latest first + const uniqueYears:any = [...new Set(eventsData.map((event: any) => event.year))] + + setYears(uniqueYears); + + // Set latest year as default tab + if (uniqueYears.length > 0) { + setActiveYear(uniqueYears[0]); + } + } catch (error) { + console.log("Error fetching event images:", error); + } + }; + + console.log("activeYear", activeYear) + console.log("galleryImages", galleryImages) + + // Filter images by active year + const filteredImages = galleryImages.filter((img) => img.year === activeYear) + + + console.log("filteredImages", filteredImages) const handleImageClick = (index: number) => { - const actualIndex = galleryImages.findIndex(img => img.src === filteredImages[index].src); + const actualIndex = galleryImages.findIndex( + (img) => img.src === filteredImages[index].src + ); setCurrentIndex(actualIndex); setOpen(true); }; @@ -51,30 +83,32 @@ export default function HomePhotoGallerySection() {

Photos Gallery

- {/* Tabs */} + {/* Year Tabs */}
- {tabs.map((tab, i) => ( + {years.map((year) => ( ))}
-
-
+ +
+
- {filteredImages.slice(0, 8).map((img, index) => ( + {filteredImages.slice(0, 8).map((img:any, index) => (
+

{img?.eventtitle}

))} + {filteredImages.length === 0 && (
-

No images found for selected year.

+

+ No images found for {activeYear}. +

)}
-
+ +
- -
- - {/* */} - +
+
-
+ +
-
- know More +
+ + Know More +
-
-