Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debounce for search request #89

Merged
merged 2 commits into from
Feb 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 22 additions & 21 deletions my-app/src/components/searchDrawer/SearchDrawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,33 @@ const SearchDrawer = () => {
const [isDraweOpen, setIsDrawerOpen] = useState<boolean>(false);
const [searchKeys, setSearchKeys] = useState('');
const [searchedData, setSearchedData] = useState<Product[]>([]);
const [loader, setLoader] = useState<boolean>(false)
//const [loader, setLoader] = useState<boolean>(false);


useEffect(() => {
const getproducts = async () => {
try {
setLoader(true)
const response: any = await getQueryProducts(searchKeys)

if (typeof response === null) {
setSearchedData(response)
} else {
setSearchedData([])
const handler = setTimeout(async () => {
if (searchKeys.length > 0) {
try {
//setLoader(true);
const response = await getQueryProducts(searchKeys);
setSearchedData(response);
if (typeof response === null) {
setSearchedData(response)
} else {
setSearchedData([])
}
} catch (error) {
console.error('Error fetching data:', error);
} finally {
//setLoader(false);
}

} catch (error) {
console.error('Error fetching data:', error);
} finally {
setLoader(false)
}
}

getproducts();
console.log(searchedData)
}, 500);

return () => {
clearTimeout(handler);
};
}, [searchKeys]);

const handleSearchInput: React.ChangeEventHandler<HTMLInputElement>= (event) => {
setSearchKeys(event.target.value)
}
Expand Down
Loading