Skip to content

Commit

Permalink
Revert "feat: replace lodash debounce for useDebounce hook in UserSea…
Browse files Browse the repository at this point in the history
…rch"

This reverts commit 3a136a3.
  • Loading branch information
ThorsAngerVaNeT committed Aug 13, 2023
1 parent 9097bf3 commit c172867
Showing 1 changed file with 8 additions and 19 deletions.
27 changes: 8 additions & 19 deletions client/src/components/UserSearch.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { useState, useEffect } from 'react';
import { Select, Typography } from 'antd';
import { GithubAvatar } from 'components/GithubAvatar';
import get from 'lodash/get';
import { get, debounce } from 'lodash';
import { SelectProps } from 'antd/lib/select';
import type { SearchStudent } from 'services/course';
import { useDebounce } from 'react-use';

type Person = { id: number; githubId: string; name: string } | SearchStudent;

Expand All @@ -18,8 +17,6 @@ export type UserProps = SelectProps<string> & {

export function UserSearch(props: UserProps) {
const [data, setData] = useState<Person[]>([]);
const [value, setValue] = useState<string>('');
const [debouncedValue, setDebouncedValue] = useState<string>('');
const {
searchFn = defaultSearch,
defaultValues,
Expand All @@ -33,23 +30,15 @@ export function UserSearch(props: UserProps) {
setData(defaultValues ?? []);
}, [props.defaultValues]);

useEffect(() => {
if (debouncedValue) {
searchFn(debouncedValue, onlyStudentsWithoutMentorShown).then(data => setData(data));
const handleSearch = async (value: string) => {
value = value.trim();
if (value) {
const data = await searchFn(value, onlyStudentsWithoutMentorShown);
setData(data);
} else {
setData(props.defaultValues ?? []);
}
}, [debouncedValue]);

useDebounce(
() => {
setDebouncedValue(value);
},
300,
[value],
);

const handleSearch = (value: string) => setValue(value.trim());
};

return (
<Select
Expand All @@ -60,7 +49,7 @@ export function UserSearch(props: UserProps) {
defaultActiveFirstOption={false}
showArrow={defaultValues ? Boolean(defaultValues.length) : false}
filterOption={false}
onSearch={handleSearch}
onSearch={debounce(handleSearch, 300)}
placeholder={defaultValues?.length ?? 0 > 0 ? 'Select...' : 'Search...'}
notFoundContent={null}
>
Expand Down

0 comments on commit c172867

Please sign in to comment.