From 94d3bb1dad487b1804e2438fc04b0dc528b6d699 Mon Sep 17 00:00:00 2001 From: Calder White Date: Sun, 11 Dec 2022 15:45:18 -0500 Subject: [PATCH] Add filters to homepage. --- .../pages/MagazineReview/MagazineReview.tsx | 46 ++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/frontend/src/components/pages/MagazineReview/MagazineReview.tsx b/frontend/src/components/pages/MagazineReview/MagazineReview.tsx index d4e49efe..b5e89466 100644 --- a/frontend/src/components/pages/MagazineReview/MagazineReview.tsx +++ b/frontend/src/components/pages/MagazineReview/MagazineReview.tsx @@ -7,12 +7,15 @@ import { useBreakpointValue, VStack, } from "@chakra-ui/react"; -import React, { useEffect, useState } from "react"; +import React, { SetStateAction, useEffect, useState } from "react"; +import GenreAPIClient from "../../../APIClients/GenreAPIClient"; import reviewAPIClient from "../../../APIClients/ReviewAPIClient"; import background from "../../../assets/home-bg.png"; +import { Option } from "../../../types/BookTypes"; import { PaginatedReviewResponse, Review } from "../../../types/ReviewTypes"; import { mapReviewResponseToReview } from "../../../utils/MappingUtils"; +import FilterBox from "../FilterBox"; import SearchBox from "../SearchBox"; import CategoryReviews from "./CategoryReviews"; import FeaturedReview from "./FeaturedReview"; @@ -25,6 +28,12 @@ const MagazineReview = (): React.ReactElement => { const [featuredReviews, setFeaturedReviews] = useState([]); const [loading, setLoading] = useState(false); + // filter state + const [genresFilter, setGenresFilter] = useState([]); + const [allGenres, setAllGenres] = useState([]); + const [allAges, setAllAges] = useState([]); + const [ageRangeFilter, setAgeRangeFilter] = useState([]); // ageRange[0] is min age, ageRange[1] is max age + const displayBlurb = useBreakpointValue( { base: false, @@ -65,6 +74,34 @@ const MagazineReview = (): React.ReactElement => { ); setLoading(false); }); + + // filtering setup + + // fetch filter params from URL + const params = new URLSearchParams(window.location.search); + const searchQuery = params.has("search_query") + ? params.get("search_query") + : ""; + const genres = params.has("genres") ? params.get("genres")?.split(",") : []; + const ageFilter = params.has("minAge") || params.has("maxAge"); + const minAge = params.has("minAge") ? Number(params.get("minAge")) : 0; + const maxAge = params.has("maxAge") ? Number(params.get("maxAge")) : 0; + + if (searchQuery) { + setSearchText(searchQuery); + } + if (genres) { + setGenresFilter(genres); + } + if (ageFilter && minAge >= 0 && maxAge >= 0) { + setAgeRangeFilter([minAge, maxAge]); + } + + GenreAPIClient.getGenreOptions().then( + (genreResponse: SetStateAction) => { + setAllGenres(genreResponse); + }, + ); }, []); return ( @@ -97,6 +134,13 @@ const MagazineReview = (): React.ReactElement => { searchQuery={searchText} homePage /> + null} + setAgeFilter={() => null} + searchStyle + /> {loading ? (