Skip to content

Commit

Permalink
merge with staging
Browse files Browse the repository at this point in the history
  • Loading branch information
fagundesjg committed Jun 10, 2024
2 parents c8292f1 + a1e8945 commit 03229f8
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions src/pages/Home/Home.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useCallback, useEffect, useMemo, useState } from 'react';
import { useSearchParams } from 'react-router-dom';
import { RotateCw} from 'lucide-react';
import { RotateCw } from 'lucide-react';
import qs from 'qs';

import { BurgerMenu, Footer, Header } from '@/components';
Expand All @@ -19,8 +19,14 @@ const initialFilterData: IFilterFormProps = {
};

const loadFilterData = (): IFilterFormProps => {
const storedFilterData = JSON.parse(localStorage.getItem('filter-data') || '{}');
return { ...initialFilterData, ...storedFilterData, ...qs.parse(new URLSearchParams(window.location.search).toString()) };
const storedFilterData = JSON.parse(
localStorage.getItem('filter-data') || '{}'
);
return {
...initialFilterData,
...storedFilterData,
...qs.parse(new URLSearchParams(window.location.search).toString()),
};
};

const saveFilterData = (filterData: IFilterFormProps) => {
Expand All @@ -31,7 +37,9 @@ const Home = () => {
const { data: shelters, loading, refresh } = useShelters({ cache: true });
const [isModalOpen, setOpenModal] = useState<boolean>(false);
const [, setSearchParams] = useSearchParams();
const [filterData, setFilterData] = useState<IFilterFormProps>(loadFilterData());
const [filterData, setFilterData] = useState<IFilterFormProps>(
loadFilterData()
);

const [, setSearch] = useThrottle<string>(
{
Expand Down Expand Up @@ -79,7 +87,7 @@ const Home = () => {
saveFilterData(values);
refresh({ params: { search: searchQuery } });
},
[refresh, setSearchParams]
[refresh, setSearchParams, factorySearchArgs]
);

const handleFetchMore = useCallback(() => {
Expand All @@ -90,10 +98,24 @@ const Home = () => {
search: qs.stringify(factorySearchArgs(filterData)),
};
refresh({ params }, true);
}, [refresh, filterData, shelters.filters, shelters.page, shelters.perPage, factorySearchArgs]);
}, [
refresh,
filterData,
shelters.filters,
shelters.page,
shelters.perPage,
factorySearchArgs,
]);

useEffect(() => {
if (filterData.search || filterData.cities.length > 0 || filterData.priorities.length > 0 || filterData.shelterStatus.length > 0 || filterData.supplyCategoryIds.length > 0 || filterData.supplyIds.length > 0){
if (
filterData.search ||
filterData.cities.length > 0 ||
filterData.priorities.length > 0 ||
filterData.shelterStatus.length > 0 ||
filterData.supplyCategoryIds.length > 0 ||
filterData.supplyIds.length > 0
) {
setSearchParams(qs.stringify(filterData));
refresh({ params: { search: qs.stringify(filterData) } });
}
Expand Down

0 comments on commit 03229f8

Please sign in to comment.