Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add filters to homepage. #198

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion frontend/src/components/pages/MagazineReview/MagazineReview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -25,6 +28,12 @@ const MagazineReview = (): React.ReactElement => {
const [featuredReviews, setFeaturedReviews] = useState<Review[]>([]);
const [loading, setLoading] = useState<boolean>(false);

// filter state
const [genresFilter, setGenresFilter] = useState<string[]>([]);
const [allGenres, setAllGenres] = useState<Option[]>([]);
const [allAges, setAllAges] = useState<Option[]>([]);
const [ageRangeFilter, setAgeRangeFilter] = useState<number[]>([]); // ageRange[0] is min age, ageRange[1] is max age

const displayBlurb = useBreakpointValue(
{
base: false,
Expand Down Expand Up @@ -65,6 +74,34 @@ const MagazineReview = (): React.ReactElement => {
);
setLoading(false);
});

// filtering setup

// fetch filter params from URL
const params = new URLSearchParams(window.location.search);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is going to work because we're not using the search url stuff for the homepage, instead get rid of the code from lines 81 to 98

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<Option[]>) => {
setAllGenres(genreResponse);
},
);
}, []);

return (
Expand Down Expand Up @@ -97,6 +134,13 @@ const MagazineReview = (): React.ReactElement => {
searchQuery={searchText}
homePage
/>
<FilterBox
genreOptions={allGenres}
ageOptions={allAges}
setGenreFilter={() => null}
setAgeFilter={() => null}
searchStyle
/>
</Box>

{loading ? (
Expand Down