Skip to content

Commit 93ce8d0

Browse files
Merge pull request #6 from AqViolet/master
Debounce added
2 parents 86f4d5f + 8d56abb commit 93ce8d0

File tree

3 files changed

+70
-22
lines changed

3 files changed

+70
-22
lines changed

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@
1717
"bcrypt": "^5.1.1",
1818
"cloudinary": "^2.2.0",
1919
"cryptr": "^6.3.0",
20+
"debounce": "^2.1.1",
2021
"file-saver": "^2.0.5",
2122
"geist": "^1.3.0",
2223
"gridfs-stream": "^1.1.1",
2324
"jose": "^5.4.1",
2425
"jszip": "^3.10.1",
26+
"lodash": "^4.17.21",
27+
"lodash.debounce": "^4.0.8",
2528
"lucide-react": "^0.395.0",
2629
"mongodb": "^6.7.0",
2730
"mongoose": "^8.4.1",
@@ -37,6 +40,7 @@
3740
"react-dom": "^18.3.0",
3841
"react-dropzone": "^14.2.3",
3942
"react-hot-toast": "^2.4.1",
43+
"use-debounce": "^10.0.3",
4044
"zod": "^3.23.3"
4145
},
4246
"devDependencies": {

pnpm-lock.yaml

Lines changed: 33 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/app/components/searchbar.tsx

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
"use client";
22

3-
import { useState } from "react";
3+
import { useState, useCallback } from "react";
44
import axios from "axios";
55
import { Search } from "lucide-react";
66
import Cryptr from "cryptr";
7+
import debounce from 'debounce';
78
import { useRouter } from "next/navigation";
89

910
const cryptr = new Cryptr(
@@ -16,29 +17,39 @@ const SearchBar = () => {
1617
const [suggestions, setSuggestions] = useState<string[]>([]);
1718
const [error, setError] = useState<string | null>(null);
1819

19-
const handleSearchChange = async (e: React.ChangeEvent<HTMLInputElement>) => {
20-
const text = e.target.value;
21-
setSearchText(text);
2220

23-
if (text.length > 1) {
24-
try {
25-
const searchResponse = await axios.get("/api/search", {
26-
params: { text },
27-
});
28-
29-
const { res: encryptedSearchResponse } = searchResponse.data;
30-
const decryptedSearchResponse = cryptr.decrypt(encryptedSearchResponse);
31-
// console.log("Decrypted Search Response:", decryptedSearchResponse);
32-
33-
const { subjects } = JSON.parse(decryptedSearchResponse);
34-
const suggestionList = subjects.map((subjectObj: { subject: string }) => subjectObj.subject);
35-
setSuggestions(suggestionList);
36-
} catch (error) {
37-
setError("Error fetching suggestions");
21+
22+
const debouncedSearch = useCallback(
23+
debounce(async (text: string) => {
24+
if (text.length > 1) {
25+
26+
try {
27+
const searchResponse = await axios.get("http://localhost:3000/api/search", {
28+
params: { text },
29+
});
30+
31+
const { res: encryptedSearchResponse } = searchResponse.data;
32+
const decryptedSearchResponse = cryptr.decrypt(encryptedSearchResponse);
33+
// console.log("Decrypted Search Response:", decryptedSearchResponse);
34+
35+
const { subjects } = JSON.parse(decryptedSearchResponse);
36+
const suggestionList = subjects.map((subjectObj: { subject: string }) => subjectObj.subject);
37+
setSuggestions(suggestionList);
38+
} catch (error) {
39+
setError("Error fetching suggestions");
40+
41+
}
42+
} else {
43+
setSuggestions([]);
3844
}
39-
} else {
40-
setSuggestions([]);
41-
}
45+
}, 1000),
46+
[]
47+
);
48+
49+
const handleSearchChange = (e: React.ChangeEvent<HTMLInputElement>) => {
50+
const text = e.target.value;
51+
setSearchText(text);
52+
debouncedSearch(text);
4253
};
4354

4455
const handleSelectSuggestion = async (suggestion: string) => {

0 commit comments

Comments
 (0)