Skip to content

Commit 841b6d0

Browse files
changes in search and admin upload
1 parent 6a09a98 commit 841b6d0

File tree

6 files changed

+37
-23
lines changed

6 files changed

+37
-23
lines changed

src/app/adminupload/page.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const Upload: React.FC = () => {
3131
const [subject, setSubject] = useState<string>("Digital Logic and Microprocessors[BITE202L]");
3232
const [slot, setSlot] = useState<string>("A1");
3333
const [year, setYear] = useState<string>("2011");
34-
const [exam, setExam] = useState<string>("cat1");
34+
const [exam, setExam] = useState<string>("CAT-1");
3535
const [tag, setTag] = useState<string>();
3636
const [urls, setUrls] = useState<string[]>();
3737
const [publicIds, setPublicIds] = useState<string[]>();
@@ -380,7 +380,7 @@ const Upload: React.FC = () => {
380380
setSubject("");
381381
setSlot("");
382382
setYear("");
383-
setExam("cat1");
383+
setExam("CAT-1");
384384
setAsset([]);
385385
setPublicIds([]);
386386
setUrls([]);

src/app/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import SearchBar from "@/app/components/searchbar"
1+
import SearchBar from "@/components/searchbar";
22

33
const HomePage = () => {
44
return (

src/app/upload/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use client";
22
import React, { useRef, useState } from "react";
3-
import Camera from "../components/camera";
3+
import Camera from "@/components/camera";
44
import JSZip from "jszip";
55
import axios from "axios";
66
import { slots, courses } from "./select_options";
File renamed without changes.

src/app/components/searchbar.tsx renamed to src/components/searchbar.tsx

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,73 +16,87 @@ const SearchBar = () => {
1616
const [searchText, setSearchText] = useState("");
1717
const [suggestions, setSuggestions] = useState<string[]>([]);
1818
const [error, setError] = useState<string | null>(null);
19+
const [loading, setLoading] = useState(false);
1920

20-
21-
2221
const debouncedSearch = useCallback(
2322
debounce(async (text: string) => {
2423
if (text.length > 1) {
24+
setLoading(true);
2525
try {
2626
const searchResponse = await axios.get("/api/search", {
2727
params: { text },
2828
});
29-
29+
3030
const { res: encryptedSearchResponse } = searchResponse.data;
3131
const decryptedSearchResponse = cryptr.decrypt(encryptedSearchResponse);
3232
const { subjects } = JSON.parse(decryptedSearchResponse);
3333
const suggestionList = subjects.map((subjectObj: { subject: string }) => subjectObj.subject);
3434
setSuggestions(suggestionList);
35+
setError(null);
3536
} catch (error) {
3637
const typedError = error as AxiosError<{ message?: string }>;
3738
const errorMessage = typedError.response?.data?.message ?? "Error fetching suggestions";
3839
setError(errorMessage);
40+
} finally {
41+
setLoading(false);
3942
}
4043
} else {
4144
setSuggestions([]);
4245
}
43-
}, 1000),
44-
[cryptr, axios]
46+
}, 500),
47+
[cryptr]
4548
);
4649

4750
const handleSearchChange = (e: React.ChangeEvent<HTMLInputElement>) => {
4851
const text = e.target.value;
4952
setSearchText(text);
53+
if (text.length <= 1) {
54+
setSuggestions([]);
55+
}
5056
debouncedSearch(text);
5157
};
5258

5359
const handleSelectSuggestion = async (suggestion: string) => {
5460
setSearchText(suggestion);
5561
setSuggestions([]);
56-
// console.log(encodeURI(suggestion))
5762
router.push(`/catalogue?subject=${encodeURIComponent(suggestion)}`);
5863
};
5964

6065
return (
6166
<div className="flex min-h-screen items-center justify-center bg-gray-50 flex-col">
6267
<form className="w-full max-w-md">
6368
<div className="relative">
64-
65-
<input type="text" value={searchText} onChange={handleSearchChange}
66-
placeholder="Search..." className="w-full rounded-md border border-gray-300 px-4 py-2 pr-10 shadow-sm focus:border-indigo-500 focus:outline-none focus:ring-2 focus:ring-indigo-500"/>
67-
68-
<button type="submit" className="absolute inset-y-0 right-0 flex items-center pr-3">
69+
<input
70+
type="text"
71+
value={searchText}
72+
onChange={handleSearchChange}
73+
placeholder="Search..."
74+
className={`w-full rounded-md border border-gray-300 px-4 py-2 pr-10 shadow-sm focus:border-indigo-500 focus:outline-none focus:ring-2 focus:ring-indigo-500 ${loading ? 'opacity-50' : ''}`}
75+
/>
76+
<button type="submit" className="absolute inset-y-0 right-0 flex items-center pr-3" disabled={loading}>
6977
<Search className="h-5 w-5 text-gray-400" />
7078
</button>
71-
7279
</div>
73-
{suggestions.length > 0 && (
74-
<ul className="absolute z-10 w-full bg-white border border-gray-300 rounded-md mt-2">
80+
{loading && (
81+
<div className="absolute z-10 w-full max-w-md bg-white border border-gray-300 rounded-md mt-2 p-2 text-center text-gray-500">
82+
Loading suggestions...
83+
</div>
84+
)}
85+
{suggestions.length > 0 && !loading && (
86+
<ul className="absolute w-full text-center max-w-md z-10 bg-white border border-gray-300 rounded-md mt-2">
7587
{suggestions.map((suggestion, index) => (
76-
77-
<li key={index} onClick={() => handleSelectSuggestion(suggestion)} className="cursor-pointer p-2 hover:bg-gray-100">
88+
<li
89+
key={index}
90+
onClick={() => handleSelectSuggestion(suggestion)}
91+
className="cursor-pointer p-2 hover:bg-gray-100 truncate"
92+
style={{ width: '100%', overflow: 'hidden', whiteSpace: 'nowrap', textOverflow: 'ellipsis' }}
93+
>
7894
{suggestion}
7995
</li>
80-
8196
))}
8297
</ul>
8398
)}
8499
</form>
85-
86100
{error && <p className="mt-4 text-red">{error}</p>}
87101
</div>
88102
);

src/db/papers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const paperSchema = new Schema<IPaper>({
77
subject: { type: String, required: true },
88
slot: { type: String, required: true },
99
year: { type: String, required: true },
10-
exam: { type: String, enum: ["cat1", "cat2", "fat"], required: true },
10+
exam: { type: String, enum: ["CAT-1", "CAT-2", "FAT"], required: true },
1111
});
1212

1313
const Paper: Model<IPaper> =

0 commit comments

Comments
 (0)