From c92ff38c8ab3a24092edf89c561d5f3dc16c9c03 Mon Sep 17 00:00:00 2001 From: Harini312821 Date: Wed, 31 Jul 2024 21:40:06 +0800 Subject: [PATCH] basic event filtering --- client/src/pages/EventsPage.tsx | 95 ++++++++++++++++++++++++++++++--- 1 file changed, 88 insertions(+), 7 deletions(-) diff --git a/client/src/pages/EventsPage.tsx b/client/src/pages/EventsPage.tsx index 1225bf9..f2f779a 100644 --- a/client/src/pages/EventsPage.tsx +++ b/client/src/pages/EventsPage.tsx @@ -11,16 +11,44 @@ import { Button, } from "@nextui-org/react"; -const EventsPage = () => { - const [events, setEvents] = useState([]); +interface Event { + id: number; + title: string; + category: string; + location: string; + time: string; // Assuming time is a string, adjust if necessary + description: string; + imageUrl: string; +} + +const EventsPage: React.FC = () => { + const [events, setEvents] = useState([]); + const [filteredEvents, setFilteredEvents] = useState([]); + const [categories, setCategories] = useState([]); + const [locations, setLocations] = useState([]); + const [selectedCategory, setSelectedCategory] = useState(""); + const [selectedLocation, setSelectedLocation] = useState(""); + const [selectedTime, setSelectedTime] = useState(""); + const navigate = useNavigate(); useEffect(() => { const fetchEvents = async () => { try { - const res = await instance.get(config.serverAddress + "/events"); - console.log("Fetched events data:", res.data); // Log the fetched data + const res = await instance.get(`${config.serverAddress}/events`); + console.log("Fetched events data:", res.data); setEvents(res.data); + setFilteredEvents(res.data); + + // Extract unique categories and locations from events + const uniqueCategories = Array.from( + new Set(res.data.map((event) => event.category).filter(Boolean)) + ); + const uniqueLocations = Array.from( + new Set(res.data.map((event) => event.location).filter(Boolean)) + ); + setCategories(uniqueCategories); + setLocations(uniqueLocations); } catch (error) { console.error("Failed to fetch events:", error); } @@ -29,17 +57,71 @@ const EventsPage = () => { fetchEvents(); }, []); + useEffect(() => { + // Filter events based on selected criteria + const filtered = events.filter((event) => { + const matchCategory = selectedCategory ? event.category === selectedCategory : true; + const matchLocation = selectedLocation ? event.location === selectedLocation : true; + const matchTime = selectedTime ? event.time === selectedTime : true; + + return matchCategory && matchLocation && matchTime; + }); + + setFilteredEvents(filtered); + }, [selectedCategory, selectedLocation, selectedTime, events]); + return (

Events

+ {/* Filter Options */} +
+ + + + + +
+ + {/* Event List */}
- {events.length === 0 ? ( + {filteredEvents.length === 0 ? (

No events available.

) : ( - events.map((event) => ( + filteredEvents.map((event) => ( { src={event.imageUrl} width="100%" height={180} - data-disableanimation={true} // Use custom data attribute />