1+ 'use client' ;
2+ import { useState , ChangeEvent , FormEvent } from 'react' ;
3+
4+ export default function ComplaintForm ( ) {
5+ const [ title , setTitle ] = useState ( '' ) ;
6+ const [ description , setDescription ] = useState ( '' ) ;
7+ const [ place , setPlace ] = useState ( '' ) ;
8+ const [ image , setImage ] = useState < File | null > ( null ) ;
9+ const [ previewUrl , setPreviewUrl ] = useState < string | null > ( null ) ;
10+
11+ const handleImageUpload = ( e : ChangeEvent < HTMLInputElement > ) => {
12+ const file = e . target . files ?. [ 0 ] ;
13+ if ( file ) {
14+ setImage ( file ) ;
15+ setPreviewUrl ( URL . createObjectURL ( file ) ) ;
16+ }
17+ } ;
18+
19+ const toBase64 = ( file : File ) : Promise < string > => {
20+ return new Promise ( ( resolve , reject ) => {
21+ const reader = new FileReader ( ) ;
22+ reader . readAsDataURL ( file ) ;
23+ reader . onload = ( ) => resolve ( reader . result as string ) ;
24+ reader . onerror = ( error ) => reject ( error ) ;
25+ } ) ;
26+ } ;
27+
28+ const handleSubmit = async ( e : FormEvent ) => {
29+ e . preventDefault ( ) ;
30+
31+ if ( ! title . trim ( ) || ! description . trim ( ) || ! place . trim ( ) || ! image ) {
32+ alert ( 'Please fill in all fields and upload an image.' ) ;
33+ return ;
34+ }
35+
36+ let imageUrl = null ;
37+ try {
38+ imageUrl = await toBase64 ( image ) ;
39+ } catch ( err ) {
40+ console . error ( 'Error converting image:' , err ) ;
41+ alert ( 'Failed to convert image.' ) ;
42+ return ;
43+ }
44+
45+ // ⛔ Don't actually send to API now
46+ const formData = { title, description, place, imageUrl } ;
47+ console . log ( '📝 Collected Complaint Data:' , formData ) ;
48+
49+ alert ( 'Form submitted (not sent to backend). Check console for data.' ) ;
50+
51+ // Reset fields (optional)
52+ setTitle ( '' ) ;
53+ setDescription ( '' ) ;
54+ setPlace ( '' ) ;
55+ setImage ( null ) ;
56+ setPreviewUrl ( null ) ;
57+ } ;
58+
59+ return (
60+ < form className = "mt-4 space-y-4" onSubmit = { handleSubmit } >
61+ < div className = "border rounded-lg p-4 text-center bg-gray-100" >
62+ { previewUrl && (
63+ < img
64+ src = { previewUrl }
65+ alt = "Preview"
66+ className = "mx-auto w-full max-w-xs h-40 object-cover rounded mb-2"
67+ />
68+ ) }
69+ < label className = "cursor-pointer underline text-gray-700" >
70+ Upload Image
71+ < input
72+ type = "file"
73+ accept = "image/*"
74+ onChange = { handleImageUpload }
75+ className = "hidden"
76+ />
77+ </ label >
78+ </ div >
79+
80+ < label htmlFor = "title" > Title</ label >
81+ < input
82+ id = "title"
83+ type = "text"
84+ placeholder = "Title Name"
85+ value = { title }
86+ onChange = { ( e ) => setTitle ( e . target . value ) }
87+ className = "w-full border p-2 rounded"
88+ required
89+ />
90+
91+ < label htmlFor = "description" > Description</ label >
92+ < textarea
93+ id = "description"
94+ placeholder = "Description"
95+ value = { description }
96+ onChange = { ( e ) => setDescription ( e . target . value ) }
97+ className = "w-full border p-2 rounded"
98+ required
99+ />
100+
101+ < label htmlFor = "place" > Place</ label >
102+ < input
103+ id = "place"
104+ type = "text"
105+ placeholder = "Place"
106+ value = { place }
107+ onChange = { ( e ) => setPlace ( e . target . value ) }
108+ className = "w-full border p-2 rounded"
109+ required
110+ />
111+
112+ < button type = "submit" className = "w-full bg-black text-white py-2 rounded" >
113+ Confirm
114+ </ button >
115+ </ form >
116+ ) ;
117+ }
0 commit comments