town council filter
This commit is contained in:
@@ -2,6 +2,7 @@ import React, { useState, useEffect } from "react";
|
|||||||
import { useNavigate } from "react-router-dom";
|
import { useNavigate } from "react-router-dom";
|
||||||
import instance from "../security/http";
|
import instance from "../security/http";
|
||||||
import config from "../config";
|
import config from "../config";
|
||||||
|
import axios from "axios";
|
||||||
import {
|
import {
|
||||||
Card,
|
Card,
|
||||||
CardHeader,
|
CardHeader,
|
||||||
@@ -16,7 +17,7 @@ interface Event {
|
|||||||
title: string;
|
title: string;
|
||||||
category: string;
|
category: string;
|
||||||
location: string;
|
location: string;
|
||||||
time: string; // Assuming time is a string, adjust if necessary
|
time: string;
|
||||||
description: string;
|
description: string;
|
||||||
imageUrl: string;
|
imageUrl: string;
|
||||||
}
|
}
|
||||||
@@ -25,9 +26,9 @@ const EventsPage: React.FC = () => {
|
|||||||
const [events, setEvents] = useState<Event[]>([]);
|
const [events, setEvents] = useState<Event[]>([]);
|
||||||
const [filteredEvents, setFilteredEvents] = useState<Event[]>([]);
|
const [filteredEvents, setFilteredEvents] = useState<Event[]>([]);
|
||||||
const [categories, setCategories] = useState<string[]>([]);
|
const [categories, setCategories] = useState<string[]>([]);
|
||||||
const [locations, setLocations] = useState<string[]>([]);
|
const [townCouncils, setTownCouncils] = useState<string[]>([]);
|
||||||
const [selectedCategory, setSelectedCategory] = useState<string>("");
|
const [selectedCategory, setSelectedCategory] = useState<string>("");
|
||||||
const [selectedLocation, setSelectedLocation] = useState<string>("");
|
const [selectedTownCouncil, setSelectedTownCouncil] = useState<string>("");
|
||||||
const [selectedTime, setSelectedTime] = useState<string>("");
|
const [selectedTime, setSelectedTime] = useState<string>("");
|
||||||
|
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
@@ -44,31 +45,37 @@ const EventsPage: React.FC = () => {
|
|||||||
const uniqueCategories = Array.from(
|
const uniqueCategories = Array.from(
|
||||||
new Set(res.data.map((event) => event.category).filter(Boolean))
|
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);
|
setCategories(uniqueCategories);
|
||||||
setLocations(uniqueLocations);
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Failed to fetch events:", error);
|
console.error("Failed to fetch events:", error);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const fetchTownCouncils = async () => {
|
||||||
|
try {
|
||||||
|
const res = await axios.get(`${config.serverAddress}/users/town-councils-metadata`);
|
||||||
|
setTownCouncils(JSON.parse(res.data).townCouncils);
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Failed to fetch town councils:", error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
fetchEvents();
|
fetchEvents();
|
||||||
|
fetchTownCouncils();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// Filter events based on selected criteria
|
// Filter events based on selected criteria
|
||||||
const filtered = events.filter((event) => {
|
const filtered = events.filter((event) => {
|
||||||
const matchCategory = selectedCategory ? event.category === selectedCategory : true;
|
const matchCategory = selectedCategory ? event.category === selectedCategory : true;
|
||||||
const matchLocation = selectedLocation ? event.location === selectedLocation : true;
|
const matchTownCouncil = selectedTownCouncil ? event.location === selectedTownCouncil : true;
|
||||||
const matchTime = selectedTime ? event.time === selectedTime : true;
|
const matchTime = selectedTime ? event.time === selectedTime : true;
|
||||||
|
|
||||||
return matchCategory && matchLocation && matchTime;
|
return matchCategory && matchTownCouncil && matchTime;
|
||||||
});
|
});
|
||||||
|
|
||||||
setFilteredEvents(filtered);
|
setFilteredEvents(filtered);
|
||||||
}, [selectedCategory, selectedLocation, selectedTime, events]);
|
}, [selectedCategory, selectedTownCouncil, selectedTime, events]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="w-full h-full">
|
<div className="w-full h-full">
|
||||||
@@ -90,17 +97,19 @@ const EventsPage: React.FC = () => {
|
|||||||
))}
|
))}
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<select
|
{townCouncils.length > 0 && (
|
||||||
value={selectedLocation}
|
<select
|
||||||
onChange={(e) => setSelectedLocation(e.target.value)}
|
value={selectedTownCouncil}
|
||||||
>
|
onChange={(e) => setSelectedTownCouncil(e.target.value)}
|
||||||
<option value="">All Locations</option>
|
>
|
||||||
{locations.map((location, index) => (
|
<option value="">All location</option>
|
||||||
<option key={index} value={location}>
|
{townCouncils.map((townCouncil) => (
|
||||||
{location}
|
<option key={townCouncil} value={townCouncil}>
|
||||||
|
{townCouncil}
|
||||||
</option>
|
</option>
|
||||||
))}
|
))}
|
||||||
</select>
|
</select>
|
||||||
|
)}
|
||||||
|
|
||||||
<select
|
<select
|
||||||
value={selectedTime}
|
value={selectedTime}
|
||||||
|
|||||||
Reference in New Issue
Block a user