diff --git a/src/pages/Mentors/index.tsx b/src/pages/Mentors/index.tsx index 7b84d1c8..c674a6d6 100644 --- a/src/pages/Mentors/index.tsx +++ b/src/pages/Mentors/index.tsx @@ -5,10 +5,17 @@ import { usePublicMentors } from '../../hooks/usePublicMentors'; import { type Mentor, type Category } from '../../types'; import MentorCard from '../../components/MentorCard/MentorCard.component'; import Loading from '../../assets/svg/Loading'; +import { ApplicationStatus } from '../../enums'; const Mentors = () => { const [selectedCategory, setSelectedCategory] = useState(null); + const [selectedCountries, setSelectedCountries] = useState([]); + const [selectedAvailableSlots, setSelectedAvailableSlots] = useState([]); const [sortedMentors, setSortedMentors] = useState([]); + const [uniqueCountries, setUniqueCountries] = useState([]); + const [uniqueAvailableSlots, setUniqueAvailableSlots] = useState([]); + const [isCountryDropdownOpen, setIsCountryDropdownOpen] = useState(false); + const [isSlotsDropdownOpen, setIsSlotsDropdownOpen] = useState(false); const pageSize = 10; const { ref, inView } = useInView(); @@ -31,13 +38,62 @@ const Mentors = () => { useEffect(() => { if (data) { const allMentors = data.pages.flatMap((page) => page.items); - setSortedMentors(allMentors); + + const countries = allMentors + .map((mentor) => mentor.application?.country) + .filter((country): country is string => !!country); + setUniqueCountries([...new Set(countries)]); + + const availableSlots = allMentors + .map((mentor) => { + const approvedMenteesCount = mentor?.mentees + ? mentor.mentees.filter( + (mentee) => mentee.state === ApplicationStatus.APPROVED + ).length + : 0; + const availableSlots = mentor?.application.noOfMentees + ? Math.max(0, mentor.application.noOfMentees - approvedMenteesCount) + : 0; + return availableSlots; + }) + .filter((slots, index, self) => self.indexOf(slots) === index); + setUniqueAvailableSlots(availableSlots); } }, [data]); + useEffect(() => { + if (data) { + let allMentors = data.pages.flatMap((page) => page.items); + + if (selectedCountries.length > 0) { + allMentors = allMentors.filter((mentor) => + selectedCountries.includes(mentor.application.country) + ); + } + + if (selectedAvailableSlots.length > 0) { + allMentors = allMentors.filter((mentor) => { + const approvedMenteesCount = mentor?.mentees + ? mentor.mentees.filter( + (mentee) => mentee.state === ApplicationStatus.APPROVED + ).length + : 0; + const availableSlots = mentor?.application.noOfMentees + ? Math.max(0, mentor.application.noOfMentees - approvedMenteesCount) + : 0; + return selectedAvailableSlots.includes(availableSlots); + }); + } + + setSortedMentors(allMentors); + } + }, [data, selectedCountries, selectedAvailableSlots]); + const handleSortAZ = () => { const sorted = [...sortedMentors].sort((a, b) => - a.application.firstName.localeCompare(b.application.firstName) + (a.application?.firstName || '').localeCompare( + b.application?.firstName || '' + ) ); setSortedMentors(sorted); }; @@ -46,6 +102,34 @@ const Mentors = () => { setSelectedCategory(category); }; + const handleCountryChange = (country: string) => { + setSelectedCountries((prevCountries) => { + if (prevCountries.includes(country)) { + return prevCountries.filter((c) => c !== country); + } else { + return [...prevCountries, country]; + } + }); + }; + + const handleAvailableSlotsChange = (slots: number) => { + setSelectedAvailableSlots((prevSlots) => { + if (prevSlots.includes(slots)) { + return prevSlots.filter((s) => s !== slots); + } else { + return [...prevSlots, slots]; + } + }); + }; + + const toggleCountryDropdown = () => { + setIsCountryDropdownOpen(!isCountryDropdownOpen); + }; + + const toggleSlotsDropdown = () => { + setIsSlotsDropdownOpen(!isSlotsDropdownOpen); + }; + if (status === 'pending' || categoriesLoading) { return (
@@ -59,69 +143,149 @@ const Mentors = () => { } return ( -
-
-
-

Mentors

-
-
+
+
+
+

Filters

-
-
- - {allCategories.map((category: Category) => ( +
+

Categories

+
+ + + {allCategories.map((category: Category) => ( + + ))} +
+
+ +
+

- ))} +

+ + {isCountryDropdownOpen && ( +
+ + {uniqueCountries.map((country) => ( + + ))} +
+ )}
-
-
- -
+
+

+ +

- {sortedMentors.length > 0 ? ( -
- {sortedMentors.map((mentor) => ( - - ))} + {isSlotsDropdownOpen && ( +
+ + {uniqueAvailableSlots.map((slots) => ( + + ))} +
+ )}
- ) : ( -

No mentors found for this category.

- )} - {isFetchingNextPage && ( -
- +
+
- )} +
-
+
+
+

Mentors

+
+ + {sortedMentors.length > 0 ? ( +
+ {sortedMentors.map((mentor) => ( + + ))} +
+ ) : ( +

No mentors found for this filter.

+ )} + + {isFetchingNextPage && ( +
+ +
+ )} + +
+
+
);