@@ -22,26 +22,36 @@ import { toast } from "react-toastify";
22
22
import axios from "axios" ;
23
23
import AppLayout from "../../layout/AppLayout" ;
24
24
25
-
26
25
const Assignment = ( ) => {
27
26
const [ title , setTitle ] = useState ( "" ) ;
28
27
const [ description , setDescription ] = useState ( "" ) ;
29
28
const [ questions , setQuestions ] = useState ( "" ) ;
30
29
const [ deadline , setDeadline ] = useState ( "" ) ;
31
30
const [ loading , setLoading ] = useState ( false ) ;
31
+ const [ pdfFile , setPdfFile ] = useState ( null ) ;
32
32
const backendUrl = import . meta. env . VITE_BACKEND_URL ;
33
33
34
34
const handleSubmit = async ( e ) => {
35
35
e . preventDefault ( ) ;
36
- setTimeout ( setLoading ( true ) , 2000 ) ;
36
+ if ( ! pdfFile ) {
37
+ toast . error ( "Please upload a PDF file for the assignment." ) ;
38
+ return ;
39
+ }
40
+ setLoading ( true ) ;
41
+ // Prepare form data
42
+ const formData = new FormData ( ) ;
43
+ formData . append ( "title" , title ) ;
44
+ formData . append ( "description" , description ) ;
45
+ formData . append ( "questions" , questions ) ;
46
+ formData . append ( "deadline" , deadline ) ;
47
+ formData . append ( "pdfFile" , pdfFile ) ;
37
48
38
49
try {
39
- const response = await axios . post ( `${ backendUrl } /api/v7/postAssignment` , {
40
- title,
41
- description,
42
- questions,
43
- deadline,
44
- } ) ;
50
+ const response = await axios . post (
51
+ `${ backendUrl } /api/v7/postAssignment` ,
52
+ formData ,
53
+ { headers : { "Content-Type" : "multipart/form-data" } }
54
+ ) ;
45
55
if ( response . data . success ) {
46
56
toast . success ( response . data . message ) ;
47
57
}
@@ -61,86 +71,95 @@ const Assignment = () => {
61
71
< div className = "min-h-screen w-full flex ml-[23%]" >
62
72
< div className = "flex w-full gap-8" >
63
73
{ /* Sidebar */ }
64
-
65
-
66
- < div className = "form-details flex justify-center items-center h-screen w-[70%]" >
67
- < form
68
- onSubmit = { handleSubmit }
69
- className = "w-full h-[90%] lg:w-3/4 p-6 bg-white rounded-lg shadow-md"
70
- >
71
- < div className = "space-y-6" >
72
- < h1 className = "text-3xl font-semibold text-gray-800 text-center" >
73
- Post New Assignment: Empower Your Students with Engaging Challenges!
74
- </ h1 >
75
-
76
- { /* Title Input */ }
77
- < div >
78
- < label className = "block text-gray-600 font-medium" > Title</ label >
79
- < input
80
- type = "text"
81
- placeholder = "Enter assignment title"
82
- value = { title }
83
- required
84
- className = "w-full mt-2 p-3 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-green-500"
85
- onChange = { ( e ) => setTitle ( e . target . value ) }
86
- />
87
- </ div >
88
-
89
- { /* Description Input */ }
90
- < div >
91
- < label className = "block text-gray-600 font-medium" > Description</ label >
92
- < input
93
- type = "text"
94
- placeholder = "Enter assignment description"
95
- value = { description }
96
- required
97
- className = "w-full mt-2 p-3 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-green-500"
98
- onChange = { ( e ) => setDescription ( e . target . value ) }
99
- />
100
- </ div >
101
-
102
- { /* Questions Input */ }
103
- < div >
104
- < label className = "block text-gray-600 font-medium" > Questions</ label >
105
- < input
106
- type = "text"
107
- placeholder = "Enter assignment questions"
108
- value = { questions }
109
- required
110
- className = "w-full mt-2 p-3 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-green-500"
111
- onChange = { ( e ) => setQuestions ( e . target . value ) }
112
- />
113
- </ div >
114
-
115
- { /* Deadline Input */ }
116
- < div >
117
- < label className = "block text-gray-600 font-medium" > Deadline</ label >
118
- < input
119
- type = "date"
120
- value = { deadline }
121
- required
122
- className = "w-full mt-2 p-3 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-green-500"
123
- onChange = { ( e ) => setDeadline ( e . target . value ) }
124
- />
125
- </ div >
126
74
127
- { /* Submit Button */ }
128
- < div className = "flex justify-center" >
129
- < button
130
- disabled = { loading }
131
- type = "submit"
132
- className = "w-full p-3 bg-green-500 text-white font-medium rounded-md disabled:opacity-50"
133
- >
134
- { loading ? "Posting..." : "Post Assignment" }
135
- </ button >
75
+ < div className = "form-details flex justify-center items-center h-screen w-[70%]" >
76
+ < form
77
+ onSubmit = { handleSubmit }
78
+ className = "w-full h-[90%] lg:w-3/4 p-6 bg-white rounded-lg shadow-md"
79
+ >
80
+ < div className = "space-y-6" >
81
+ < h1 className = "text-3xl font-semibold text-gray-800 text-center" >
82
+ Post New Assignment: Empower Your Students with Engaging Challenges!
83
+ </ h1 >
84
+
85
+ { /* Title Input */ }
86
+ < div >
87
+ < label className = "block text-gray-600 font-medium" > Title</ label >
88
+ < input
89
+ type = "text"
90
+ placeholder = "Enter assignment title"
91
+ value = { title }
92
+ required
93
+ className = "w-full mt-2 p-3 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-green-500"
94
+ onChange = { ( e ) => setTitle ( e . target . value ) }
95
+ />
96
+ </ div >
97
+
98
+ { /* Description Input */ }
99
+ < div >
100
+ < label className = "block text-gray-600 font-medium" > Description</ label >
101
+ < input
102
+ type = "text"
103
+ placeholder = "Enter assignment description"
104
+ value = { description }
105
+ required
106
+ className = "w-full mt-2 p-3 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-green-500"
107
+ onChange = { ( e ) => setDescription ( e . target . value ) }
108
+ />
109
+ </ div >
110
+
111
+ { /* Questions Input */ }
112
+ < div >
113
+ < label className = "block text-gray-600 font-medium" > Questions</ label >
114
+ < input
115
+ type = "text"
116
+ placeholder = "Enter assignment questions"
117
+ value = { questions }
118
+ required
119
+ className = "w-full mt-2 p-3 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-green-500"
120
+ onChange = { ( e ) => setQuestions ( e . target . value ) }
121
+ />
122
+ </ div >
123
+
124
+ { /* Deadline Input */ }
125
+ < div >
126
+ < label className = "block text-gray-600 font-medium" > Deadline</ label >
127
+ < input
128
+ type = "date"
129
+ value = { deadline }
130
+ required
131
+ className = "w-full mt-2 p-3 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-green-500"
132
+ onChange = { ( e ) => setDeadline ( e . target . value ) }
133
+ />
134
+ </ div >
135
+
136
+ < div >
137
+ < label className = "block text-gray-600 font-medium" > Upload PDF</ label >
138
+ < input
139
+ type = "file"
140
+ accept = "application/pdf"
141
+ required
142
+ onChange = { ( e ) => setPdfFile ( e . target . files [ 0 ] ) }
143
+ className = "mt-2 block w-full text-sm text-gray-500 file:py-2 file:px-4 file:border file:border-gray-300 file:rounded-md file:text-sm file:font-semibold file:bg-indigo-50 file:text-indigo-700 hover:file:bg-indigo-100"
144
+ />
145
+ </ div >
146
+
147
+ { /* Submit Button */ }
148
+ < div className = "flex justify-center" >
149
+ < button
150
+ disabled = { loading }
151
+ type = "submit"
152
+ className = "w-full p-3 bg-green-500 text-white font-medium rounded-md disabled:opacity-50"
153
+ >
154
+ { loading ? "Posting..." : "Post Assignment" }
155
+ </ button >
156
+ </ div >
136
157
</ div >
137
- </ div >
138
- </ form >
139
- </ div >
140
-
158
+ </ form >
159
+ </ div >
141
160
</ div >
142
161
</ div >
143
162
) ;
144
163
} ;
145
164
146
- export default AppLayout ( ) ( Assignment ) ;
165
+ export default AppLayout ( Assignment ) ;
0 commit comments