From 217bc609e7a11101d64f24c8dcd0a7f3a5089d05 Mon Sep 17 00:00:00 2001 From: ItsFlash10 Date: Sun, 1 Sep 2024 22:23:56 +0530 Subject: [PATCH] feat-604: Fixed the filter button on the client side --- apps/web/components/Tracks.tsx | 35 +++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/apps/web/components/Tracks.tsx b/apps/web/components/Tracks.tsx index a5aa1de6..b97cc2e5 100644 --- a/apps/web/components/Tracks.tsx +++ b/apps/web/components/Tracks.tsx @@ -39,26 +39,31 @@ interface TracksWithCategoriesProps { categories: { id: string; category: string }[]; } +enum CohortGroup { + One = 1, + Two = 2, + Three = 3, +} + export const Tracks = ({ tracks, categories }: TracksWithCategoriesProps) => { const [selectedCategory, setSelectedCategory] = useRecoilState(category); const [filteredTracks, setFilteredTracks] = useState(tracks); const [visibleTracks, setVisibleTracks] = useState([]); const [sortBy, setSortBy] = useState("new"); - const [cohort2, setCohort2] = useState(false); const [isSelectOpen, setIsSelectOpen] = useState(false); - const [cohort3, setCohort3] = useState(false); const [currentPage, setCurrentPage] = useState(1); - const tracksPerPage = 10; const [loading, setLoading] = useState(true); + const [selectedCohort, setSelectedCohort] = useState(null); + + const tracksPerPage = 10; + const isCohort2Selected = selectedCohort === CohortGroup.Two; + const isCohort3Selected = selectedCohort === CohortGroup.Three; const filterTracks = () => { setLoading(true); let newFilteredTracks = tracks; - if (cohort3) { - newFilteredTracks = newFilteredTracks.filter((t) => t.cohort === 3); - } - if (cohort2) { - newFilteredTracks = newFilteredTracks.filter((t) => t.cohort === 2); + if (selectedCohort) { + newFilteredTracks = newFilteredTracks.filter((t) => t.cohort === selectedCohort); } if (selectedCategory && selectedCategory !== "All") { newFilteredTracks = newFilteredTracks.filter((t) => @@ -86,9 +91,13 @@ export const Tracks = ({ tracks, categories }: TracksWithCategoriesProps) => { setLoading(false); }; + const handleCohortSelection = (cohort: number) => { + setSelectedCohort((prevCohort) => (prevCohort === cohort ? null : cohort)); + }; + useEffect(() => { filterTracks(); - }, [selectedCategory, cohort2, cohort3, tracks]); + }, [selectedCategory, selectedCohort, tracks]); useEffect(() => { sortTracks(sortBy); @@ -127,8 +136,8 @@ export const Tracks = ({ tracks, categories }: TracksWithCategoriesProps) => { @@ -136,8 +145,8 @@ export const Tracks = ({ tracks, categories }: TracksWithCategoriesProps) => {