1
1
"use client" ;
2
+
2
3
import React , { useRef , useState } from "react" ;
3
4
import axios from "axios" ;
4
5
import toast , { Toaster } from "react-hot-toast" ;
@@ -17,19 +18,12 @@ import {
17
18
SelectLabel ,
18
19
SelectItem ,
19
20
} from "@/components/ui/select" ;
20
- import {
21
- Command ,
22
- CommandInput ,
23
- CommandList ,
24
- CommandItem ,
25
- CommandEmpty ,
26
- CommandGroup ,
27
- } from "@/components/ui/command" ;
28
21
import Navbar from "@/components/Navbar" ;
29
22
import Footer from "@/components/Footer" ;
30
23
import { PostPDFToCloudinary } from "@/interface" ;
31
24
import { courses , slots , years } from "@/components/select_options" ;
32
25
import SearchBar from "@/components/searchbarSubjectList" ;
26
+
33
27
const Page = ( ) => {
34
28
const router = useRouter ( ) ;
35
29
const fileInputRef = useRef < HTMLInputElement > ( null ) ;
@@ -39,9 +33,8 @@ const Page = () => {
39
33
const [ exam , setExam ] = useState ( "" ) ;
40
34
const [ year , setYear ] = useState ( "" ) ;
41
35
const [ files , setFiles ] = useState < File [ ] > ( [ ] ) ;
42
- const [ inputValue , setInputValue ] = useState ( "" ) ;
43
- const [ isSubjectCommandOpen , setIsSubjectCommandOpen ] = useState ( false ) ;
44
36
const [ isUploading , setIsUploading ] = useState ( false ) ;
37
+ const [ resetSearch , setResetSearch ] = useState ( false ) ;
45
38
46
39
const handlePrint = async ( ) => {
47
40
const maxFileSize = 5 * 1024 * 1024 ;
@@ -53,38 +46,26 @@ const Page = () => {
53
46
] ;
54
47
const files = fileInputRef . current ?. files as FileList | null ;
55
48
56
- if ( ! slot ) {
57
- toast . error ( "Slot is required" ) ;
58
- return ;
59
- }
60
- if ( ! subject ) {
61
- toast . error ( "Subject is required" ) ;
62
- return ;
63
- }
64
- if ( ! exam ) {
65
- toast . error ( "Exam is required" ) ;
49
+ if ( ! slot || ! subject || ! exam || ! year || ! files || files . length === 0 ) {
50
+ toast . error ( "All fields are required." ) ;
66
51
return ;
67
52
}
68
- if ( ! year ) {
69
- toast . error ( "Year is required" ) ;
53
+
54
+ if ( files . length > 5 ) {
55
+ toast . error ( "More than 5 files selected" ) ;
70
56
return ;
71
57
}
72
- if ( ! files || files . length === 0 ) {
73
- toast . error ( "No files selected" ) ;
74
- return ;
75
- } else if ( files . length > 5 ) {
76
- toast . error ( "More than 5 files selected" ) ;
58
+
59
+ if ( ! Array . from ( files ) . every ( ( file ) => file . type === files [ 0 ] ?. type ) ) {
60
+ toast . error ( `All files MUST be of the same type` ) ;
77
61
return ;
78
62
}
79
- //file have same extension
80
- if ( ! Array . from ( files ) . every ( ( file ) => file . type == files [ 0 ] ?. type ) )
81
- toast . error ( `All files MUST be of same type` ) ;
63
+
82
64
for ( const file of files ) {
83
65
if ( file . size > maxFileSize ) {
84
66
toast . error ( `File ${ file . name } is more than 5MB` ) ;
85
67
return ;
86
68
}
87
-
88
69
if ( ! allowedFileTypes . includes ( file . type ) ) {
89
70
toast . error (
90
71
`File type of ${ file . name } is not allowed. Only PDFs and images are accepted.` ,
@@ -97,53 +78,54 @@ const Page = () => {
97
78
if ( files [ 0 ] ?. type === "application/pdf" ) {
98
79
isPdf = true ;
99
80
if ( files . length > 1 ) {
100
- toast . error ( `PDFs should be uploaded seperately ` ) ;
81
+ toast . error ( `PDFs should be uploaded separately ` ) ;
101
82
return ;
102
83
}
103
84
}
85
+
104
86
const Arrfiles = Array . from ( files ) ;
105
87
const formData = new FormData ( ) ;
106
88
Arrfiles . forEach ( ( file ) => {
107
89
formData . append ( "files" , file ) ;
108
90
} ) ;
109
-
110
- // const body = {
111
- // subject: subject,
112
- // slot: slot,
113
- // year: year,
114
- // exam: exam,
115
- // isPdf: isPdf,
116
- // };
117
91
formData . append ( "subject" , subject ) ;
118
92
formData . append ( "slot" , slot ) ;
119
93
formData . append ( "year" , year ) ;
120
94
formData . append ( "exam" , exam ) ;
121
95
formData . append ( "isPdf" , String ( isPdf ) ) ;
96
+
97
+ setIsUploading ( true ) ;
98
+
122
99
void toast . promise (
123
100
( async ( ) => {
124
101
try {
125
- const response = await axios . post < PostPDFToCloudinary > (
126
- "/api/upload" ,
127
- formData ,
128
- ) ;
129
- } catch ( error : unknown ) {
130
- throw handleAPIError ( error ) ;
102
+ await axios . post ( "/api/upload" , formData ) ;
103
+
104
+ setSlot ( "" ) ;
105
+ setSubject ( "" ) ;
106
+ setExam ( "" ) ;
107
+ setYear ( "" ) ;
108
+ setFiles ( [ ] ) ;
109
+ if ( fileInputRef . current ) {
110
+ fileInputRef . current . value = "" ;
111
+ }
112
+
113
+ setResetSearch ( true ) ;
114
+ setTimeout ( ( ) => setResetSearch ( false ) , 100 ) ;
115
+ } catch ( error ) {
116
+ handleAPIError ( error ) ;
117
+ } finally {
118
+ setIsUploading ( false ) ;
131
119
}
132
120
} ) ( ) ,
133
121
{
134
122
loading : "Uploading papers..." ,
135
- success : "papers uploaded" ,
136
- error : ( err : ApiError ) => err . message ,
123
+ success : "Papers uploaded" ,
124
+ error : ( err :ApiError ) => err . message ,
137
125
} ,
138
126
) ;
139
127
} ;
140
128
141
- const handleSubjectSelect = ( value : string ) => {
142
- setSubject ( value ) ;
143
- setInputValue ( value ) ;
144
- setIsSubjectCommandOpen ( false ) ;
145
- } ;
146
-
147
129
return (
148
130
< div className = "flex h-screen flex-col justify-between" >
149
131
< div >
@@ -195,31 +177,7 @@ const Page = () => {
195
177
{ /* Subject Selection */ }
196
178
< div >
197
179
< label > Subject:</ label >
198
- { /* setSubject */ }
199
- < SearchBar setSubject = { setSubject } > </ SearchBar >
200
- { /* <Command className="rounded-lg border shadow-md md:min-w-[450px]">
201
- <CommandInput
202
- value={inputValue}
203
- onChangeCapture={(e) =>
204
- setInputValue((e.target as HTMLInputElement).value)
205
- }
206
- placeholder="Type a subject or search..."
207
- /> */ }
208
- { /* <CommandList className="h-[100px]">
209
- <CommandEmpty>No results found.</CommandEmpty>
210
-
211
- <CommandGroup heading="Subjects">
212
- {courses.map((course) => (
213
- <CommandItem
214
- key={course}
215
- onSelect={() => handleSubjectSelect(course)}
216
- >
217
- <span>{course}</span>
218
- </CommandItem>
219
- ))}
220
- </CommandGroup>
221
- </CommandList>
222
- </Command> */ }
180
+ < SearchBar setSubject = { setSubject } resetSearch = { resetSearch } />
223
181
</ div >
224
182
225
183
{ /* Year Selection */ }
@@ -232,14 +190,11 @@ const Page = () => {
232
190
< SelectContent >
233
191
< SelectGroup >
234
192
< SelectLabel > Years</ SelectLabel >
235
- { years . map ( ( year ) =>
236
- {
237
- return ( < SelectItem key = { year } value = { String ( year ) } >
193
+ { years . map ( ( year ) => (
194
+ < SelectItem key = { year } value = { String ( year ) } >
238
195
{ year }
239
- </ SelectItem > )
240
-
241
- }
242
- ) }
196
+ </ SelectItem >
197
+ ) ) }
243
198
</ SelectGroup >
244
199
</ SelectContent >
245
200
</ Select >
@@ -283,11 +238,11 @@ const Page = () => {
283
238
{ isUploading ? "Uploading..." : "Upload Papers" }
284
239
</ Button >
285
240
</ div >
286
- < div className = "" >
241
+ < div >
287
242
< Footer />
288
243
</ div >
289
244
</ div >
290
245
) ;
291
246
} ;
292
247
293
- export default Page ;
248
+ export default Page ;
0 commit comments