File tree Expand file tree Collapse file tree 2 files changed +9
-36
lines changed Expand file tree Collapse file tree 2 files changed +9
-36
lines changed Original file line number Diff line number Diff line change @@ -4,20 +4,21 @@ import Paper from "@/db/papers";
4
4
5
5
export const dynamic = "force-dynamic" ;
6
6
7
- export async function GET ( ) {
7
+ export async function GET ( req : Request ) {
8
8
try {
9
9
await connectToDatabase ( ) ;
10
10
11
- const count : number = await Paper . countDocuments ( ) ;
11
+ const { searchParams } = new URL ( req . url ) ;
12
+ const subject = searchParams . get ( "subject" ) ;
12
13
13
- return NextResponse . json (
14
- { count } ,
15
- { status : 200 }
16
- ) ;
14
+ const filter = subject ? { subject } : { } ;
15
+ const count = await Paper . countDocuments ( filter ) ;
16
+
17
+ return NextResponse . json ( { count } , { status : 200 } ) ;
17
18
} catch ( error ) {
18
19
return NextResponse . json (
19
20
{ message : "Failed to fetch papers" , error } ,
20
- { status : 500 }
21
+ { status : 500 } ,
21
22
) ;
22
23
}
23
24
}
Original file line number Diff line number Diff line change @@ -20,25 +20,6 @@ function SearchBarChild({
20
20
const suggestionsRef = useRef < HTMLUListElement | null > ( null ) ;
21
21
const fuzzy = new Fuse ( initialSubjects ) ;
22
22
23
- const fetchPaperQuantityByName = async ( subjectName : string ) => {
24
- try {
25
- const response = await axios . get ( "/api/papers" , {
26
- params : { subject : subjectName } ,
27
- } ) ;
28
-
29
- if (
30
- response . data . message === "No papers found for the specified subject"
31
- ) {
32
- return 0 ;
33
- }
34
-
35
- return response . data . papers . length ;
36
- } catch ( error ) {
37
- console . error ( "Error fetching paper quantity:" , error ) ;
38
- return "request-error" ;
39
- }
40
- } ;
41
-
42
23
const handleSearchChange = async ( e : React . ChangeEvent < HTMLInputElement > ) => {
43
24
const text = e . target . value ;
44
25
setSearchText ( text ) ;
@@ -52,16 +33,7 @@ function SearchBarChild({
52
33
. map ( ( item ) => item . item )
53
34
. slice ( 0 , 10 ) ;
54
35
55
- const suggestionsWithCount = await Promise . all (
56
- filteredSuggestions . map ( async ( suggestion ) => {
57
- const count = await fetchPaperQuantityByName ( suggestion ) ;
58
- return count !== "request-error"
59
- ? `${ suggestion } (${ count } )`
60
- : suggestion ;
61
- } ) ,
62
- ) ;
63
-
64
- setSuggestions ( suggestionsWithCount ) ;
36
+ setSuggestions ( filteredSuggestions ) ;
65
37
} else {
66
38
setSuggestions ( [ ] ) ;
67
39
}
You can’t perform that action at this time.
0 commit comments