From aeb8822ad68101d2cd7542d7e59543ae0b197210 Mon Sep 17 00:00:00 2001 From: Diego Dario Date: Mon, 27 May 2024 18:03:56 -0300 Subject: [PATCH] feat: add multi option for priority queryParam --- src/pages/Home/Home.tsx | 28 +++++++++++++---- src/pages/Home/components/Filter/Filter.tsx | 34 ++++++++------------- src/pages/Home/components/Filter/types.ts | 6 ++-- 3 files changed, 36 insertions(+), 32 deletions(-) diff --git a/src/pages/Home/Home.tsx b/src/pages/Home/Home.tsx index 4f51f924..d1105a9f 100644 --- a/src/pages/Home/Home.tsx +++ b/src/pages/Home/Home.tsx @@ -7,11 +7,11 @@ import { BurgerMenu, Footer, Header } from '@/components'; import { useShelters, useThrottle } from '@/hooks'; import { Button } from '@/components/ui/button'; import { Filter, ShelterListView } from './components'; -import { IFilterFormProps } from './components/Filter/types'; +import { IFilterFormProps, IFilterSubmittionForm } from './components/Filter/types'; const initialFilterData: IFilterFormProps = { search: '', - priority: null, + priority: [], supplyCategoryIds: [], supplyIds: [], shelterStatus: [], @@ -54,21 +54,37 @@ const Home = () => { [shelters.page, shelters.perPage, shelters.count] ); + const factorySearchArgs = useCallback((values: IFilterFormProps) => { + const searchQueryArgs: IFilterSubmittionForm = { + search: values.search, + priority: values.priority.join(), + supplyCategoryIds: values.supplyCategoryIds, + supplyIds: values.supplyIds, + shelterStatus: values.shelterStatus, + cities: values.cities + } + + return searchQueryArgs; + }, []) + const onSubmitFilterForm = useCallback( (values: IFilterFormProps) => { setOpenModal(false); setFilterData(values); - const searchQuery = qs.stringify(values, { + + const searchQuery = qs.stringify(factorySearchArgs(values), { skipNulls: true, }); + setSearchParams(searchQuery); + refresh({ params: { search: searchQuery, }, }); }, - [refresh, setSearchParams] + [refresh, setSearchParams, factorySearchArgs] ); const handleFetchMore = useCallback(() => { @@ -76,7 +92,7 @@ const Home = () => { ...shelters.filters, page: shelters.page + 1, perPage: shelters.perPage, - search: qs.stringify(filterData), + search: qs.stringify(factorySearchArgs(filterData)), }; refresh( @@ -85,7 +101,7 @@ const Home = () => { }, true ); - }, [refresh, filterData, shelters.filters, shelters.page, shelters.perPage]); + }, [refresh, filterData, shelters.filters, shelters.page, shelters.perPage, factorySearchArgs]); return (
diff --git a/src/pages/Home/components/Filter/Filter.tsx b/src/pages/Home/components/Filter/Filter.tsx index 59caf9eb..f71d98b7 100644 --- a/src/pages/Home/components/Filter/Filter.tsx +++ b/src/pages/Home/components/Filter/Filter.tsx @@ -19,7 +19,6 @@ import { import { IFilterFormikProps, IFilterProps, - ISelectField, ShelterAvailabilityStatus, } from './types'; import { priorityOptions } from '@/lib/utils'; @@ -66,12 +65,10 @@ const Filter = (props: IFilterProps) => { { initialValues: { cities: data.cities ?? [], - priority: data.priority - ? { - label: priorityOpts[data.priority], - value: data.priority, - } - : null, + priority: data.priority.map((p: string) => ({ + label: priorityOpts[Number(p) as SupplyPriority], + value: p, + })), search: data.search, shelterStatus: data.shelterStatus.map((s) => ({ label: ShelterAvailabilityStatusMapped[s], @@ -103,7 +100,7 @@ const Filter = (props: IFilterProps) => { cities, } = values; onSubmit({ - priority: priority?.value ? +priority.value : null, + priority: priority.map((p) => p.value), search, shelterStatus: shelterStatus.map((s) => s.value), supplyCategoryIds: supplyCategories.map((s) => s.value), @@ -111,8 +108,7 @@ const Filter = (props: IFilterProps) => { cities, }); }, - } - ); + }); const supplyOptions = useMemo(() => { return supplies @@ -187,20 +183,14 @@ const Filter = (props: IFilterProps) => {