Skip to content

Commit 0abe7df

Browse files
committed
debounce input
1 parent 5789211 commit 0abe7df

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

app/components/SearchBar.tsx

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,36 @@
11
import { Form } from "@remix-run/react";
2-
import React, { RefObject, useRef, useState } from 'react';
2+
import React, { ChangeEvent, RefObject, useRef, useState } from 'react';
33
import SearchIcon from "./icons/SearchIcon";
44

55

66
const SearchBar = () => {
77

8-
const [searchTerm, setSearchTerm] = useState('');
8+
const [inputValue, setInputValue] = React.useState("")
9+
const [debouncedInputValue, setDebouncedInputValue] = React.useState("")
10+
const inputRef = useRef<HTMLInputElement>(null);
11+
12+
const handleInputChange = (e: ChangeEvent<HTMLInputElement>) => {
13+
e.preventDefault();
14+
const clonedEvent = new Event(e.nativeEvent.type, e.nativeEvent);
15+
setInputValue(e.currentTarget.value)
16+
setTimeout(() => {
17+
if (inputRef.current) {
18+
inputRef.current.dispatchEvent(clonedEvent);
19+
}
20+
}, 1000);
21+
}
22+
23+
924
return (
1025
<div className="inline-flex gap-2">
1126
<input
1227
name="search"
1328
type="text"
29+
1430
placeholder="Suche..."
1531
className="text-[1rem] px-2 py-1 rounded-full hover:text-vsp-900 border border-vsp-400 active:border-vsp-900 focus:border-vsp-900 disabled:border-vsp-200"
16-
value={searchTerm}
17-
onChange={(e) => setSearchTerm(e.target.value)}
32+
value={inputValue}
33+
onChange={(e) => {handleInputChange(e)}}
1834
>
1935
</input>
2036
{/* <button

app/components/SpeakingTimeContent.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,14 @@ export default function SpeakingTimeContent({
5454
const formRef = useRef<HTMLFormElement>(null);
5555
const { t } = useTranslation("searchingCoach");
5656
const [isActive, setIsActive] = useState(false);
57+
var timeout: NodeJS.Timeout;
5758

5859
const handleChange = () => {
5960
if (formRef) {
60-
submit(formRef.current, { replace: true, preventScrollReset: true });
61+
// not the best solution as the tags now will timeout for 300ms as well - but with the onchange on the form I could not find another way
62+
// to debounce the search input to not sent a request for each input .. happy for suggestions
63+
clearTimeout(timeout)
64+
timeout = setTimeout(() => submit(formRef.current, { replace: true, preventScrollReset: true }), 300);
6165
}
6266
};
6367
const state = useNavigation();

app/utils/getSearchPageContents.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ export const getSearchPageContents = async (
2323
const checkedGender = searchParams.getAll("gender");
2424

2525
const searchTerm = searchParams.getAll("search");
26-
console.log(searchTerm);
2726

2827
const [page, coaches, languages, gender, tags, navigation, emailTemplate] =
2928
await Promise.all([

0 commit comments

Comments
 (0)