1- import React , { ReactNode } from "react" ;
1+ import React from "react" ;
22import { useTranslation } from "next-i18next" ;
33import { Checkbox , IconSearch , TextInput } from "hds-react" ;
44import { type SubmitHandler , useForm , Controller } from "react-hook-form" ;
@@ -11,8 +11,7 @@ import { getDurationOptions, participantCountOptions } from "@/modules/const";
1111import { DateRangePicker } from "@/components/form" ;
1212import { FilterTagList } from "./FilterTagList" ;
1313import SingleLabelInputGroup from "@/components/common/SingleLabelInputGroup" ;
14- import { useSearchModify , useSearchValues } from "@/hooks/useSearchValues" ;
15- import { type ParsedUrlQuery } from "node:querystring" ;
14+ import { useSearchModify } from "@/hooks/useSearchValues" ;
1615import { ControlledMultiSelect } from "./ControlledMultiSelect" ;
1716import { ControlledSelect } from "common/src/components/form/ControlledSelect" ;
1817import {
@@ -27,21 +26,13 @@ import {
2726 OptionalFilters ,
2827 StyledSubmitButton ,
2928} from "./styled" ;
29+ import { useSearchParams , type ReadonlyURLSearchParams } from "next/navigation" ;
3030
3131const StyledCheckBox = styled ( Checkbox ) `
3232 margin: 0 !important;
3333 grid-column: -2 / span 1;
3434` ;
3535
36- const SingleLabelRangeWrapper = styled ( SingleLabelInputGroup ) < {
37- label : string ;
38- children : ReactNode ;
39- } > `
40- & > div:not(:first-child) {
41- margin-top: var(--spacing-s);
42- }
43- ` ;
44-
4536type FormValues = {
4637 // TODO there is some confusion on the types of these
4738 // they are actually an array of pks (number) but they are encoded as val1,val2,val3 string
@@ -60,29 +51,28 @@ type FormValues = {
6051 textSearch : string ;
6152} ;
6253
63- function mapQueryToForm ( query : ParsedUrlQuery ) : FormValues {
64- const dur = mapQueryParamToNumber ( query . duration ) ;
54+ function mapQueryToForm ( params : ReadonlyURLSearchParams ) : FormValues {
55+ const dur = mapQueryParamToNumber ( params . getAll ( " duration" ) ) ;
6556 const duration = dur != null && dur > 0 ? dur : null ;
6657 const showOnlyReservable =
67- mapSingleBooleanParamToFormValue ( query . showOnlyReservable ) ?? true ;
58+ mapSingleBooleanParamToFormValue ( params . getAll ( "showOnlyReservable" ) ) ??
59+ true ;
6860 return {
69- purposes : mapQueryParamToNumberArray ( query . purposes ) ,
70- unit : mapQueryParamToNumberArray ( query . unit ) ,
71- equipments : mapQueryParamToNumberArray ( query . equipments ) ,
61+ purposes : mapQueryParamToNumberArray ( params . getAll ( " purposes" ) ) ,
62+ unit : mapQueryParamToNumberArray ( params . getAll ( " unit" ) ) ,
63+ equipments : mapQueryParamToNumberArray ( params . getAll ( " equipments" ) ) ,
7264 reservationUnitTypes : mapQueryParamToNumberArray (
73- query . reservationUnitTypes
65+ params . getAll ( " reservationUnitTypes" )
7466 ) ,
75- // ?
76- timeBegin : mapSingleParamToFormValue ( query . timeBegin ) ?? null ,
77- timeEnd : mapSingleParamToFormValue ( query . timeEnd ) ?? null ,
78- startDate : mapSingleParamToFormValue ( query . startDate ) ?? null ,
79- endDate : mapSingleParamToFormValue ( query . endDate ) ?? null ,
80- // number params
67+ timeBegin : mapSingleParamToFormValue ( params . getAll ( "timeBegin" ) ) ,
68+ timeEnd : mapSingleParamToFormValue ( params . getAll ( "timeEnd" ) ) ,
69+ startDate : mapSingleParamToFormValue ( params . getAll ( "startDate" ) ) ,
70+ endDate : mapSingleParamToFormValue ( params . getAll ( "endDate" ) ) ,
8171 duration,
82- minPersons : mapQueryParamToNumber ( query . minPersons ) ?? null ,
83- maxPersons : mapQueryParamToNumber ( query . maxPersons ) ?? null ,
72+ minPersons : mapQueryParamToNumber ( params . getAll ( " minPersons" ) ) ,
73+ maxPersons : mapQueryParamToNumber ( params . getAll ( " maxPersons" ) ) ,
8474 showOnlyReservable,
85- textSearch : mapSingleParamToFormValue ( query . textSearch ) ?? "" ,
75+ textSearch : mapSingleParamToFormValue ( params . getAll ( " textSearch" ) ) ?? "" ,
8676 } ;
8777}
8878
@@ -125,18 +115,17 @@ export function SingleSearchForm({
125115 isLoading : boolean ;
126116} ) : JSX . Element | null {
127117 const { handleSearch } = useSearchModify ( ) ;
128-
129118 const { t } = useTranslation ( ) ;
130- const unitTypeOptions = reservationUnitTypeOptions ;
131- const durationOptions = getDurationOptions ( t ) ;
132-
133- const searchValues = useSearchValues ( ) ;
119+ const searchValues = useSearchParams ( ) ;
134120 // TODO the users of this should be using watch
135121 const formValues = mapQueryToForm ( searchValues ) ;
136- const { handleSubmit, setValue, getValues, control, register } =
137- useForm < FormValues > ( {
138- values : formValues ,
139- } ) ;
122+ const form = useForm < FormValues > ( {
123+ values : formValues ,
124+ } ) ;
125+
126+ const { handleSubmit, setValue, getValues, control, register } = form ;
127+ const unitTypeOptions = reservationUnitTypeOptions ;
128+ const durationOptions = getDurationOptions ( t ) ;
140129
141130 const translateTag = ( key : string , value : string ) : string | undefined => {
142131 // Handle possible number / string comparison
@@ -195,7 +184,7 @@ export function SingleSearchForm({
195184 options = { equipmentsOptions }
196185 label = { t ( "searchForm:equipmentsFilter" ) }
197186 />
198- < SingleLabelRangeWrapper label = { t ( "common:dateLabel" ) } >
187+ < SingleLabelInputGroup label = { t ( "common:dateLabel" ) } >
199188 < DateRangePicker
200189 startDate = { fromUIDate ( String ( getValues ( "startDate" ) ) ) }
201190 endDate = { fromUIDate ( String ( getValues ( "endDate" ) ) ) }
@@ -220,8 +209,8 @@ export function SingleSearchForm({
220209 endMaxDate : addYears ( new Date ( ) , 2 ) ,
221210 } }
222211 />
223- </ SingleLabelRangeWrapper >
224- < SingleLabelRangeWrapper label = { t ( "common:timeLabel" ) } >
212+ </ SingleLabelInputGroup >
213+ < SingleLabelInputGroup label = { t ( "common:timeLabel" ) } >
225214 < TimeRangePicker
226215 control = { control }
227216 names = { { begin : "timeBegin" , end : "timeEnd" } }
@@ -235,7 +224,7 @@ export function SingleSearchForm({
235224 } }
236225 clearable = { { begin : true , end : true } }
237226 />
238- </ SingleLabelRangeWrapper >
227+ </ SingleLabelInputGroup >
239228 < ControlledSelect
240229 name = "duration"
241230 control = { control }
0 commit comments