@@ -17,46 +17,40 @@ export default function NewEventPage() {
1717 venue : "" ,
1818 date : new Date ( ) ,
1919 time : "" ,
20- image : "" , // will store uploaded image URL
2120 } ) ;
22- const [ uploading , setUploading ] = useState ( false ) ;
23-
24- async function handleImageUpload ( file : File ) {
25- const data = new FormData ( ) ;
26- data . append ( "file" , file ) ;
27-
28- setUploading ( true ) ;
29- const res = await fetch ( "/api/events/upload" , {
30- method : "POST" ,
31- body : data ,
32- } ) ;
33- setUploading ( false ) ;
34-
35- if ( ! res . ok ) throw new Error ( "Upload failed" ) ;
36- const { url } = await res . json ( ) ;
37- setForm ( ( prev ) => ( { ...prev , image : url } ) ) ;
38- }
21+ const [ imageFile , setImageFile ] = useState < File | null > ( null ) ;
22+ const [ saving , setSaving ] = useState ( false ) ;
3923
4024 async function handleSubmit ( e : React . FormEvent ) {
41- e . preventDefault ( ) ;
42- const formattedDate = form . date . toISOString ( ) . split ( "T" ) [ 0 ] ;
43-
44- await fetch ( "/api/events" , {
45- method : "POST" ,
46- headers : { "Content-Type" : "application/json" } ,
47- body : JSON . stringify ( {
48- title : form . title ,
49- description : form . description ,
50- venue : form . venue ,
51- date : formattedDate ,
52- time : form . time ,
53- image : form . image ,
54- } ) ,
55- } ) ;
56-
57- router . push ( "/events" ) ;
25+ e . preventDefault ( ) ;
26+ setSaving ( true ) ;
27+
28+ const formattedDate = form . date ?. toISOString ( ) . split ( "T" ) [ 0 ] ?? "" ;
29+
30+ const data = new FormData ( ) ;
31+
32+ // append normal fields safely
33+ data . append ( "title" , form . title ?? "" ) ;
34+ data . append ( "description" , form . description ?? "" ) ;
35+ data . append ( "venue" , form . venue ?? "" ) ;
36+ data . append ( "date" , formattedDate ) ;
37+ data . append ( "time" , form . time ?? "" ) ;
38+
39+ // append file only if selected
40+ if ( imageFile ) {
41+ data . append ( "image" , imageFile ) ;
5842 }
5943
44+ await fetch ( "/api/events" , {
45+ method : "POST" ,
46+ body : data ,
47+ } ) ;
48+
49+ setSaving ( false ) ;
50+ router . push ( "/events" ) ;
51+ }
52+
53+
6054 return (
6155 < form onSubmit = { handleSubmit } className = "max-w-lg mx-auto p-6 space-y-4" >
6256 < h1 className = "text-2xl font-bold" > Add New Event</ h1 >
@@ -108,25 +102,22 @@ export default function NewEventPage() {
108102 />
109103 </ div >
110104
111- { /* Image Upload */ }
105+ { /* Image (optional) */ }
112106 < div >
113- < label className = "block mb-1 font-medium" > Image</ label >
107+ < label className = "block mb-1 font-medium" > Image (optional) </ label >
114108 < input
115109 type = "file"
116110 accept = "image/*"
117- onChange = { ( e ) => e . target . files ?. [ 0 ] && handleImageUpload ( e . target . files [ 0 ] ) }
111+ onChange = { ( e ) => setImageFile ( e . target . files ?. [ 0 ] || null ) }
118112 />
119- { uploading && < p className = "text-sm text-gray-500" > Uploading...</ p > }
120- { form . image && (
121- < img src = { form . image } alt = "Uploaded" className = "mt-2 w-32 h-32 object-cover rounded" />
122- ) }
123113 </ div >
124114
125115 < button
126116 type = "submit"
117+ disabled = { saving }
127118 className = "bg-black text-white px-4 py-2 rounded hover:bg-gray-800"
128119 >
129- Save
120+ { saving ? "Saving..." : " Save" }
130121 </ button >
131122 </ form >
132123 ) ;
0 commit comments