1+ import { useState } from 'react' ;
2+
3+ function ContactForm ( ) {
4+ const [ form , setForm ] = useState ( { name : '' , email : '' , message : '' } ) ;
5+ const [ submitting , setSubmitting ] = useState ( false ) ;
6+ const [ status , setStatus ] = useState ( '' ) ;
7+
8+ const handleChange = ( e ) => {
9+ setForm ( { ...form , [ e . target . name ] : e . target . value } ) ;
10+ } ;
11+
12+ const isComplete = form . name && form . email && form . message ;
13+
14+ const handleSubmit = async ( e ) => {
15+ e . preventDefault ( ) ;
16+ setSubmitting ( true ) ;
17+ setStatus ( '' ) ;
18+
19+ try {
20+ const response = await fetch ( 'https://script.google.com/macros/s/AKfycby8qBWrMlQ-uJ3uItcoKy5_E38E9mUsOKv6m1bNQ4sG6sZHiVq1ymY8ZLhfbPGGgVW5Fg/exec' , {
21+ method : 'POST' ,
22+ redirect : "follow" ,
23+ headers : { 'Content-Type' : 'text/plain;charset=utf-8' } ,
24+ body : JSON . stringify ( form ) ,
25+ } ) ;
26+ if ( response . ok ) {
27+ setStatus ( 'Thanks for contacting us!' ) ;
28+ setForm ( { name : '' , email : '' , message : '' } ) ;
29+ } else {
30+ setStatus ( 'Submission failed.' ) ;
31+ }
32+ } catch ( error ) {
33+ console . error ( error ) ;
34+ setStatus ( 'Something went wrong.' ) ;
35+ } finally {
36+ setSubmitting ( false ) ;
37+ }
38+ } ;
39+
40+ return (
41+ < form onSubmit = { handleSubmit } style = { { display : 'flex' , flexDirection : 'column' , gap : '1rem' , maxWidth : '400px' } } >
42+ < h2 > Contact</ h2 >
43+ < p > For help with custom software or open source development.</ p >
44+ < input
45+ type = "text"
46+ name = "name"
47+ placeholder = "Your Name"
48+ value = { form . name }
49+ onChange = { handleChange }
50+ required
51+ />
52+ < input
53+ type = "email"
54+ name = "email"
55+ placeholder = "Your Email"
56+ value = { form . email }
57+ onChange = { handleChange }
58+ required
59+ />
60+ < textarea
61+ name = "message"
62+ placeholder = "How can we help you?"
63+ value = { form . message }
64+ onChange = { handleChange }
65+ required
66+ />
67+ < button type = "submit" disabled = { ! isComplete || submitting } >
68+ Submit
69+ </ button >
70+ { status && < p > { status } </ p > }
71+ </ form >
72+ ) ;
73+ }
74+
75+ export default ContactForm ;
0 commit comments