Skip to content

Commit

Permalink
fix: duplicate search in home
Browse files Browse the repository at this point in the history
  • Loading branch information
fagundesjg committed May 6, 2024
1 parent 2096543 commit 7f261c9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 24 deletions.
2 changes: 0 additions & 2 deletions src/hooks/useShelters/useShelters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ const useShelters = () => {
[]
);

console.log(data.results.length);

useEffect(() => {
refresh();
}, [refresh]);
Expand Down
17 changes: 9 additions & 8 deletions src/hooks/useThrottle/useThrottle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@ import React, { useState, useEffect } from 'react';
import { ThrottleOptions } from './types';

function useThrottle<T>(
initialValue: T,
options: ThrottleOptions<T>,
deps: any[]
): [T, React.Dispatch<React.SetStateAction<T>>] {
): [T | null, React.Dispatch<React.SetStateAction<T | null>>] {
const { throttle = 400, callback } = options;
const [value, setValue] = useState<T>(initialValue);
const [value, setValue] = useState<T | null>(null);
const [, setIntervalId] = useState<NodeJS.Timeout>();

useEffect(() => {
const id = setTimeout(() => callback(value), throttle);
setIntervalId((prev) => {
if (prev) clearTimeout(prev);
return id;
});
if (value !== null) {
const id = setTimeout(() => callback(value), throttle);
setIntervalId((prev) => {
if (prev) clearTimeout(prev);
return id;
});
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [value, throttle, ...deps]);

Expand Down
24 changes: 10 additions & 14 deletions src/pages/Home/Home.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Fragment, useCallback, useEffect, useMemo, useState } from 'react';
import { Fragment, useCallback, useMemo, useState } from 'react';
import { RotateCw, CircleAlert, Search, Loader } from 'lucide-react';

import { Alert, Header, NoFoundSearch, ShelterListItem } from '@/components';
Expand All @@ -13,15 +13,15 @@ const Home = () => {
const { data: shelters, loading, refresh } = useShelters();
const [searchValue, setSearchValue] = useState<string>('');
const [, setSearch] = useThrottle<string>(
'',
{
throttle: 400,
callback: (v) => {
const params = {
search: `address:contains:${v},name:contains:${v}`,
or: 'true',
};
refresh({
params: {
search: `address:contains:${v},name:contains:${v}`,
or: 'true',
},
params: v ? params : {},
});
},
},
Expand Down Expand Up @@ -52,13 +52,6 @@ const Home = () => {
);
}, [refresh, searchValue, shelters.page, shelters.perPage]);

useEffect(() => {
setSearch(searchValue);
}, [searchValue, setSearch]);

// const { page, perPage, count } = shelters;
// const hasNextPage = page * perPage < count;

return (
<div className="flex flex-col h-screen items-center">
<Header
Expand Down Expand Up @@ -89,7 +82,10 @@ const Home = () => {
<Input
placeholder="Buscar por abrigo ou endereço"
className="h-12 text-md font-medium text-zinc-600 pl-10 pr-4"
onChange={(ev) => setSearchValue(ev.target.value)}
onChange={(ev) => {
setSearchValue(ev.target.value);
setSearch(ev.target.value);
}}
value={searchValue}
/>
<div className="absolute inset-y-0 left-0 flex items-center pl-3">
Expand Down

0 comments on commit 7f261c9

Please sign in to comment.