diff --git a/views/pools/pool-filter/dropdown/index.tsx b/views/pools/pool-filter/dropdown/index.tsx index 6f185da6..be571137 100644 --- a/views/pools/pool-filter/dropdown/index.tsx +++ b/views/pools/pool-filter/dropdown/index.tsx @@ -7,6 +7,7 @@ import { v4 } from 'uuid'; import useClickOutsideListenerRef from '@/hooks/use-click-outside-listener-ref'; import { ArrowDownSVG, ArrowUpSVG } from '@/svg'; +import { updateURL } from '@/utils'; import { FormFilterValue } from '../../pool-card/pool-card.types'; import { FilterItemProps, PoolForm } from '../../pools.types'; @@ -29,7 +30,8 @@ const Dropdown: FC = ({ label, type, filterData, disabled }) => { const [isSelected, setIsSelected] = useState(false); - const router = useRouter(); + const { pathname } = useRouter(); + const searchParams = new URLSearchParams(); const closeDropdown = (event: any) => { if ( @@ -59,15 +61,15 @@ const Dropdown: FC = ({ label, type, filterData, disabled }) => { setIsSelected(!isSelected); replace(tmpFilters); - const newQuery = { ...router.query, [type]: option.value }; + searchParams.delete(type); - if (!option.value) { - delete newQuery[type]; - } - - router.push({ pathname: router.pathname, query: newQuery }, undefined, { - shallow: true, + tmpFilters.forEach((filter) => { + if (filter.type && filter.value) { + searchParams.append(filter.type, filter.value); + } }); + + updateURL(`${pathname}?${searchParams.toString()}`); } setOpen(not); }; diff --git a/views/pools/pool-filter/filter-selected-item/index.tsx b/views/pools/pool-filter/filter-selected-item/index.tsx index 23cbcbb8..b2daa891 100644 --- a/views/pools/pool-filter/filter-selected-item/index.tsx +++ b/views/pools/pool-filter/filter-selected-item/index.tsx @@ -5,6 +5,7 @@ import { useFieldArray, useFormContext, useWatch } from 'react-hook-form'; import { v4 } from 'uuid'; import { TimesSVG } from '@/svg'; +import { updateURL } from '@/utils'; import { FilterItemProps, PoolForm } from '../../pools.types'; @@ -18,19 +19,23 @@ const FilterSelectedItem: FC = () => { name: 'filterList', }); - const router = useRouter(); + const { pathname } = useRouter(); + + const searchParams = new URLSearchParams(); const removeFilter = (filter: FilterItemProps) => { const tmpFilters = fields?.filter((field) => filter.value != field.value); replace(tmpFilters); - const newQuery = { ...router.query }; - - delete newQuery[filter.type]; + searchParams.delete(filter.type); - router.push({ pathname: router.pathname, query: newQuery }, undefined, { - shallow: true, + tmpFilters.forEach((filter) => { + if (filter.type && filter.value) { + searchParams.append(filter.type, filter.value); + } }); + + updateURL(`${pathname}?${searchParams.toString()}`); }; const resetPoolPairFilter = () => { @@ -40,7 +45,7 @@ const FilterSelectedItem: FC = () => { const erase = () => { replace([]); - router.push(router.pathname, undefined, { shallow: true }); + updateURL(`${pathname}`); }; return (