From 09dcca64da0917e62f405b34a9137f2828eac26a Mon Sep 17 00:00:00 2001 From: pamfilos Date: Tue, 12 Nov 2024 10:33:11 +0100 Subject: [PATCH] search: fix year filtering Signed-off-by: pamfilos --- scoap3/articles/api/views.py | 5 +---- ui/src/components/search/YearFacet.tsx | 21 ++++++++++++++------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/scoap3/articles/api/views.py b/scoap3/articles/api/views.py index a416d366..0e8fd517 100644 --- a/scoap3/articles/api/views.py +++ b/scoap3/articles/api/views.py @@ -129,10 +129,7 @@ def __init__(self, *args, **kwargs): filter_fields = { "publication_year": { "field": "publication_date", - "lookups": [ - LOOKUP_FILTER_RANGE, - LOOKUP_QUERY_IN, - ], + "lookups": [LOOKUP_FILTER_RANGE, LOOKUP_QUERY_IN, "lte", "gte"], }, "journal": { "field": "publication_info.journal_title", diff --git a/ui/src/components/search/YearFacet.tsx b/ui/src/components/search/YearFacet.tsx index f94b2b31..ca519642 100644 --- a/ui/src/components/search/YearFacet.tsx +++ b/ui/src/components/search/YearFacet.tsx @@ -5,6 +5,7 @@ import { useRouter, usePathname, useSearchParams } from "next/navigation"; import "react-vis/dist/style.css"; import { PublicationYear, YearFacetData } from "@/types"; +import { URLSearchParams } from "url"; const mapInitialDataToYears = ( @@ -33,10 +34,14 @@ const YearFacet = ({ data }: any) => { setInitialEndpoints(getSliderEndpoints(initialData)); }, [data]); - const resolveYearQuery = (range: number[]) => { + const resolveYearQuery = (name: string, params: URLSearchParams, range: number[]) => { if (range[0] === range[1]) { + params.set(`${name}__gte`, range[0]?.toString()+"-01-01") + params.set(`${name}__lte`, range[0]?.toString()+"-12-31") return range[0]?.toString(); } + params.set(`${name}__gte`, range[0]?.toString()+"-01-01") + params.set(`${name}__lte`, range[1]?.toString()+"-12-31") return range.join("__"); }; @@ -44,9 +49,10 @@ const YearFacet = ({ data }: any) => { (name: string, value: any) => { const params = new URLSearchParams(searchParams.toString()) - params.delete(name); + params.delete(`${name}__lte`); + params.delete(`${name}__gte`); params.delete("page"); - params.set(name, resolveYearQuery([value[0].x, value[1].x])); + resolveYearQuery(name, params, [value[0].x, value[1].x]) return params.toString() }, @@ -64,12 +70,12 @@ const YearFacet = ({ data }: any) => { const onSliderAfterChange = (data: number[]) => { setSliderEndpoints(data) - const params = createQueryString('publication_year__range', [{x:data[0]}, {x:data[1]}]); + const params = createQueryString('publication_year', [{x:data[0]}, {x:data[1]}]); router.push(pathname + (params ? `?${params.toString()}` : "")) }; const onBarClick = (value: YearFacetData) => { - const params = createQueryString('publication_year__range', [value, value]); + const params = createQueryString('publication_year', [value, value]); router.push(pathname + (params ? `?${params.toString()}` : "")) }; @@ -81,7 +87,8 @@ const YearFacet = ({ data }: any) => { const resetFilters = () => { const params = new URLSearchParams(searchParams.toString()); - params.delete('publication_year__range'); + params.delete('publication_year__lte'); + params.delete('publication_year__gte'); params.delete('page'); router.push(pathname + (params.toString() ? `?${params.toString()}` : "")) }; @@ -98,7 +105,7 @@ const YearFacet = ({ data }: any) => { return (
- {searchParams.get('publication_year__range') && ( + {(searchParams.get('publication_year__lte') || searchParams.get('publication_year__gte')) && (