Skip to content

Commit

Permalink
Wrap search functions in useDebounced
Browse files Browse the repository at this point in the history
To reduce the number of API calls, api calls for a search query are debounced
  • Loading branch information
samau3 committed Sep 12, 2024
1 parent 4fc681b commit a60a0c8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
7 changes: 4 additions & 3 deletions client/src/pages/patients/inputs/HealthcareChoicesSearch.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import PropTypes from 'prop-types';

import { useState, useRef, useEffect } from 'react';
import { Combobox, useCombobox, ScrollArea } from '@mantine/core';

import { notifications } from '@mantine/notifications';
import { useDebouncedCallback } from '@mantine/hooks';

import SearchDatabaseInputField from './SearchDatabaseInputField';

import LifelineAPI from '../LifelineAPI.js';
Expand Down Expand Up @@ -34,7 +35,7 @@ export default function HealthcareChoicesSearch({ form, choice, initialData }) {
}
}, [initialData]);

const fetchOptions = async (query) => {
const fetchOptions = useDebouncedCallback(async (query) => {
abortController.current?.abort();
abortController.current = new AbortController();
setLoading(true);
Expand All @@ -59,7 +60,7 @@ export default function HealthcareChoicesSearch({ form, choice, initialData }) {
abortController.current = undefined;
setLoading(false);
}
};
}, 500);

const combobox = useCombobox({
onDropdownClose: () => combobox.resetSelectedOption(),
Expand Down
6 changes: 4 additions & 2 deletions client/src/pages/patients/inputs/MedicalDataSearch.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import PropTypes from 'prop-types';

import { useState, useRef, useEffect } from 'react';
import { Combobox, useCombobox, Pill, ScrollArea } from '@mantine/core';
import { useDebouncedCallback } from '@mantine/hooks';
import { notifications } from '@mantine/notifications';

import SearchDatabaseInputField from './SearchDatabaseInputField';
import LifelineAPI from '../LifelineAPI';

Expand Down Expand Up @@ -40,7 +42,7 @@ export default function MedicalDataSearch({
}
}, [initialMedicalData]);

const fetchOptions = async (query) => {
const fetchOptions = useDebouncedCallback(async (query) => {
abortController.current?.abort();
abortController.current = new AbortController();
setLoading(true);
Expand All @@ -65,7 +67,7 @@ export default function MedicalDataSearch({
abortController.current = undefined;
setLoading(false);
}
};
}, 500);

const combobox = useCombobox({
onDropdownClose: () => combobox.resetSelectedOption(),
Expand Down

0 comments on commit a60a0c8

Please sign in to comment.