Skip to content

Commit

Permalink
Debounce filter transporter search (#729)
Browse files Browse the repository at this point in the history
* add 'skip' state

* remove empty epaId querystring when selecting transporter
  • Loading branch information
sheckathorne authored Jun 14, 2024
1 parent 4442655 commit e57bb47
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
import { useSearchParams } from 'react-router-dom';
import Select from 'react-select';
import { useGetProfileQuery, useSearchRcrainfoSitesQuery, useSearchRcraSitesQuery } from 'store';
import { useDebounce } from 'hooks';

interface Props {
handleClose: () => void;
Expand All @@ -37,6 +38,8 @@ export function HandlerSearchForm({
const { handleSubmit, control } = useForm<searchHandlerForm>();
const manifestForm = useFormContext<Manifest>();
const [inputValue, setInputValue] = useState<string>('');
const debouncedInputValue = useDebounce(inputValue, 500);

const [selectedHandler, setSelectedHandler] = useState<RcraSite | null>(null);
const { org } = useGetProfileQuery(undefined, {
selectFromResult: ({ data }) => {
Expand All @@ -47,9 +50,9 @@ export function HandlerSearchForm({
const { data } = useSearchRcraSitesQuery(
{
siteType: handlerType,
siteId: inputValue,
siteId: debouncedInputValue,
},
{ skip }
{ skip: skip || debouncedInputValue === '' }
);
const {
data: rcrainfoData,
Expand All @@ -58,7 +61,7 @@ export function HandlerSearchForm({
} = useSearchRcrainfoSitesQuery(
{
siteType: handlerType,
siteId: inputValue,
siteId: debouncedInputValue,
},
{ skip: skip || !org?.rcrainfoIntegrated }
);
Expand Down Expand Up @@ -96,9 +99,9 @@ export function HandlerSearchForm({
};

useEffect(() => {
const inputTooShort = inputValue.length < 5;
const inputTooShort = inputValue.length < 2;
setSkip(inputTooShort);
}, [inputValue]);
}, [debouncedInputValue]);

useEffect(() => {
const knownSites = data && data.length > 0 ? data : [];
Expand Down
1 change: 1 addition & 0 deletions client/src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export { usePagination } from './usePagination/usePagination';
export { useProgressTracker } from './useProgressTracker/useProgressTracker';
export { useTitle } from './useTitle/useTitle';
export { useUserSiteIds } from './useUserSiteIds/useUserSiteIds';
export { useDebounce } from './useDebounce/useDebounce';
63 changes: 63 additions & 0 deletions client/src/hooks/useDebounce/useDebounce.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import '@testing-library/jest-dom';
import { renderHook, act } from '@testing-library/react';
import { useDebounce } from './useDebounce';
import { beforeAll, afterAll, afterEach, describe, expect, it, vi } from 'vitest';

describe('useDebounce hook', () => {
beforeAll(() => {
vi.useFakeTimers();
});

afterAll(() => {
vi.clearAllTimers();
});

it('should return the initial value immediately', () => {
const { result } = renderHook(() => useDebounce('initial', 500));
expect(result.current).toBe('initial');
});

it('should update the debounced value after the specified delay', () => {
const { result, rerender } = renderHook(({ value, delay }) => useDebounce(value, delay), {
initialProps: { value: 'initial', delay: 500 },
});

rerender({ value: 'updated', delay: 500 });

expect(result.current).toBe('initial');

act(() => {
vi.advanceTimersByTime(500);
});

expect(result.current).toBe('updated');
});

it('should reset the timer when value changes within the delay period', () => {
const { result, rerender } = renderHook(({ value, delay }) => useDebounce(value, delay), {
initialProps: { value: 'initial', delay: 500 },
});

rerender({ value: 'updated1', delay: 500 });
act(() => {
vi.advanceTimersByTime(300);
});
rerender({ value: 'updated2', delay: 500 });

act(() => {
vi.advanceTimersByTime(200);
});

expect(result.current).toBe('initial');

act(() => {
vi.advanceTimersByTime(300);
});

expect(result.current).toBe('updated2');
});

afterEach(() => {
vi.clearAllTimers();
});
});
17 changes: 17 additions & 0 deletions client/src/hooks/useDebounce/useDebounce.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { useState, useEffect } from 'react';

export const useDebounce = (value: string, milliSeconds: number) => {
const [debouncedValue, setDebouncedValue] = useState(value);

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

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

return debouncedValue;
};

0 comments on commit e57bb47

Please sign in to comment.