@@ -12,10 +12,11 @@ import Dropzone from "react-dropzone";
12
12
import { createCanvas } from "canvas" ;
13
13
import { getDocument , GlobalWorkerOptions } from "pdfjs-dist" ;
14
14
import { PDFDocument } from "pdf-lib" ;
15
- async function pdfToImage ( file : File ) {
16
- GlobalWorkerOptions . workerSrc = 'https://unpkg.com/[email protected] /build/pdf.worker.min.js'
15
+ async function pdfToImage ( file : File ) {
16
+ GlobalWorkerOptions . workerSrc =
17
+ "https://unpkg.com/[email protected] /build/pdf.worker.min.js" ;
17
18
18
- const pdfDoc = await PDFDocument . load ( await file . arrayBuffer ( ) ) ;
19
+ const pdfDoc = await PDFDocument . load ( await file . arrayBuffer ( ) ) ;
19
20
20
21
// Get the first page
21
22
const page = pdfDoc . getPages ( ) [ 0 ] ;
@@ -45,8 +46,7 @@ const Page = () => {
45
46
const [ files , setFiles ] = useState < File [ ] > ( [ ] ) ;
46
47
const [ isUploading , setIsUploading ] = useState ( false ) ;
47
48
const [ , setResetSearch ] = useState ( false ) ;
48
-
49
- const handlePrint = async ( ) => {
49
+ function pdfCheckAndSelect < T extends File > ( acceptedFiles : T [ ] ) {
50
50
const maxFileSize = 5 * 1024 * 1024 ;
51
51
const allowedFileTypes = [
52
52
"application/pdf" ,
@@ -55,70 +55,67 @@ const Page = () => {
55
55
"image/gif" ,
56
56
] ;
57
57
58
- // if (!slot) {
59
- // toast.error("Slot is required");
60
- // return;
61
- // }
62
- // if (!subject) {
63
- // toast.error("Subject is required");
64
- // return;
65
- // }
66
- // if (!exam) {
67
- // toast.error("Exam is required");
68
- // return;
69
- // }
70
- // if (!year) {
71
- // toast.error("Year is required");
72
- // return;
73
- // }
74
- if ( ! campus ) {
75
- setCampus ( "Vellore" )
76
-
77
- }
78
- // if (!semester) {
79
- // toast.error("Semester is required");
80
- // return;
81
- // }
82
- if ( ! files || files . length === 0 ) {
83
- toast . error ( "No files selected" ) ;
58
+ const toastId = toast . loading ( "uploading your files" ) ;
59
+ if ( ! acceptedFiles || acceptedFiles . length === 0 ) {
60
+ toast . error ( "No files selected" , {
61
+ id : toastId ,
62
+ } ) ;
84
63
return ;
85
64
}
86
65
87
- if ( files . length > 5 ) {
88
- toast . error ( "More than 5 files selected" ) ;
66
+ if ( acceptedFiles . length > 5 ) {
67
+ toast . error ( "More than 5 files selected" , {
68
+ id : toastId ,
69
+ } ) ;
89
70
return ;
90
71
}
91
72
92
73
// File validations
93
- const invalidFiles = files . filter (
74
+ const invalidFiles = acceptedFiles . filter (
94
75
( file ) =>
95
76
file . size > maxFileSize || ! allowedFileTypes . includes ( file . type ) ,
96
77
) ;
97
78
98
79
if ( invalidFiles . length > 0 ) {
99
80
toast . error (
100
81
`Some files are invalid. Ensure each file is below 5MB and of an allowed type (PDF, JPEG, PNG, GIF).` ,
82
+ {
83
+ id : toastId ,
84
+ } ,
101
85
) ;
102
86
return ;
103
87
}
104
88
105
- const isPdf = files . length === 1 && files [ 0 ] ?. type === "application/pdf" ;
106
- if ( isPdf && files . length > 1 ) {
107
- toast . error ( "PDFs must be uploaded separately" ) ;
89
+ const isPdf =
90
+ acceptedFiles . length === 1 &&
91
+ acceptedFiles [ 0 ] ?. type === "application/pdf" ;
92
+ if ( isPdf && acceptedFiles . length > 1 ) {
93
+ toast . error ( "PDFs must be uploaded separately" , {
94
+ id : toastId ,
95
+ } ) ;
108
96
return ;
109
97
}
110
- console . log ( "Outside" )
98
+
99
+ setFiles ( acceptedFiles ) ;
100
+ toast . success ( `${ acceptedFiles . length } files selected!` , {
101
+ id : toastId ,
102
+ } ) ;
103
+ }
104
+ const handlePrint = async ( ) => {
105
+ if ( ! campus ) {
106
+ setCampus ( "Vellore" ) ;
107
+ }
108
+
109
+ const isPdf = files . length === 1 && files [ 0 ] ?. type === "application/pdf" ;
111
110
112
111
// Prepare FormData
113
112
const formData = new FormData ( ) ;
114
113
files . forEach ( ( file ) => {
115
114
formData . append ( "files" , file ) ;
116
115
} ) ;
117
- // formData.append("subject", subject);
118
- // formData.append("slot", slot);
119
- if ( isPdf && files [ 0 ] )
120
- {
121
- formData . append ( "image" , await pdfToImage ( files [ 0 ] ) )
116
+
117
+ if ( isPdf && files [ 0 ] ) {
118
+ formData . append ( "image" , await pdfToImage ( files [ 0 ] ) ) ;
122
119
}
123
120
// formData.append("exam", exam);
124
121
formData . append ( "campus" , campus ) ;
@@ -161,15 +158,11 @@ const Page = () => {
161
158
{ /* <legend className="text-lg font-bold">Upload papers</legend> */ }
162
159
163
160
< div className = "flex w-full flex-col 2xl:gap-y-4" >
164
-
165
161
{ /* File Dropzone */ }
166
162
< div >
167
- < Dropzone
168
- onDrop = { ( acceptedFiles ) => setFiles ( acceptedFiles ) }
169
- accept = { { "image/*" : [ ] , "application/pdf" : [ ] } }
170
- >
163
+ < Dropzone onDrop = { pdfCheckAndSelect } >
171
164
{ ( { getRootProps, getInputProps } ) => (
172
- < section className = "my-2 -mr-2 rounded-2xl border-2 border-dashed p-8 text-center" >
165
+ < section className = "my-2 -mr-2 cursor-pointer rounded-2xl border-2 border-dashed p-8 text-center" >
173
166
< div { ...getRootProps ( ) } >
174
167
< input { ...getInputProps ( ) } />
175
168
< p >
0 commit comments