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 transporter input #729

Merged
merged 5 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
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
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;
};
Loading