From ec87545b5b831833a19173db98bcf236968277aa Mon Sep 17 00:00:00 2001 From: HaGuesto Date: Thu, 21 Nov 2024 15:54:54 +0100 Subject: [PATCH] move setting defaultfiltervalues in statviz higher up in the DOM (#1771) --------- Signed-off-by: HaGuesto --- .../components/filter/MultiSelectFilter.tsx | 12 +------ .../components/filter/TimeRangeSelect.tsx | 11 ++----- .../statviz/components/filter/ValueFilter.tsx | 14 ++------ .../statviz/dashboard/Dashboard.tsx | 32 ++++++++++++++++++- 4 files changed, 37 insertions(+), 32 deletions(-) diff --git a/shared-components/statviz/components/filter/MultiSelectFilter.tsx b/shared-components/statviz/components/filter/MultiSelectFilter.tsx index b4473a502..7e822dcac 100644 --- a/shared-components/statviz/components/filter/MultiSelectFilter.tsx +++ b/shared-components/statviz/components/filter/MultiSelectFilter.tsx @@ -1,10 +1,8 @@ import { zodResolver } from "@hookform/resolvers/zod/src/zod.js"; import { useForm } from "react-hook-form"; -import { useSearchParams } from "react-router-dom"; import { z } from "zod"; import { useEffect } from "react"; import SelectField from "../../../form/SelectField"; -import { urlFilterValuesEncode } from "../../hooks/useMultiSelectFilter"; export interface IFilterValue { value: string; @@ -39,10 +37,7 @@ export default function MultiSelectFilter({ filterValue, fieldLabel = "display by", onFilterChange, - defaultFilterValues = undefined, }: IValueFilterProps) { - const [searchParams, setSearchParams] = useSearchParams(); - const { setValue, control, @@ -51,6 +46,7 @@ export default function MultiSelectFilter({ resolver: zodResolver(ValueFilterSchema), defaultValues: values, }); + useEffect(() => { if (filterValue) { // @ts-expect-error ts(2345) @@ -58,12 +54,6 @@ export default function MultiSelectFilter({ } }, [filterId, filterValue, setValue]); - useEffect(() => { - if (defaultFilterValues && searchParams.get(filterId) === null) { - searchParams.append(filterId, urlFilterValuesEncode(defaultFilterValues)); - setSearchParams(searchParams); - } - }); return ( { + const currentQuery = searchParams.toString(); + const newSearchParams = searchParams; + + // TimeRangeFilter + if (!searchParams.get("from")) { + newSearchParams.append("from", date2String(subMonths(new Date(), 3))); + } + if (!searchParams.get("to")) { + newSearchParams.append("to", date2String(new Date())); + } + if (!searchParams.get("boi")) { + newSearchParams.append("boi", boxesOrItemsFilterValues[0].urlId); + } + + if (newSearchParams.toString() !== currentQuery) { + setSearchParams(newSearchParams); + } + }, [searchParams, setSearchParams]); + return (
Dashboard