Skip to content

Commit

Permalink
Add debounce to search terms to reduce extra calls (#1126)
Browse files Browse the repository at this point in the history
  • Loading branch information
gdalcengio authored Jun 10, 2024
1 parent 8816279 commit f24d690
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/components/selectRangeUsePlanPage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
getDataFromLocalStorage,
saveDataInLocalStorage
} from '../../utils';
import useDebounce from '../../utils/hooks/useDebounce';
import Error from './Error';
import { makeStyles } from '@material-ui/core/styles';
import ZoneSelect, { ZoneSelectAll } from './ZoneSelect';
Expand Down Expand Up @@ -56,19 +57,25 @@ const useStyles = makeStyles(() => ({

const SelectRangeUsePlanPage = ({ match, history }) => {
const { page = 1 } = match.params;
const debouncedPage = useDebounce(page, 500);
const [toastId, setToastId] = useState();
const [limit = 10, setLimit] = useQueryParam('limit', StringParam);
const debouncedLimit = useDebounce(limit, 500);
const [searchSelectedZones, setSearchSelectedZones] = useState([]);
const debouncedZones = useDebounce(searchSelectedZones, 500);
const [orderBy = 'agreement.forest_file_id', setOrderBy] = useQueryParam(
'orderBy',
StringParam,
);
const debouncedOrderBy = useDebounce(orderBy, 500);
const [order = 'asc', setOrder] = useQueryParam('order', StringParam);
const debouncedOrder = useDebounce(order, 500);
const filterInfo = getDataFromLocalStorage("filter-info");
const [filters = { agreementCheck: 'true' }, setFilters] = useQueryParam(
'filters',
NewObjectParam,
);
const debouncedFilters = useDebounce(filters, 500);
const [filtersInitialized, setFiltersInitialized] = useState(false);
// startup
useEffect(() => {
Expand Down
18 changes: 18 additions & 0 deletions src/utils/hooks/useDebounce.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// useDebounce.js, from https://github.com/vercel/swr/issues/110
import { useState, useEffect } from 'react';

export default function useDebounce(value, delay) {
const [debouncedValue, setDebouncedValue] = useState(value);

useEffect(() => {
const handler = setTimeout(() => {
setDebouncedValue(value);
}, delay);

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

return debouncedValue;
}

0 comments on commit f24d690

Please sign in to comment.