-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmedium.js
72 lines (65 loc) · 2.21 KB
/
medium.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import React, { useState } from "react"
import { Link, navigate } from 'gatsby'
import Layout from "../components/layout"
import NetlifyForm from '../react-ssg-netlify-forms'
const IndexPage = () => {
// Pre-Submit for validations and disabling button
const [processing, setProcessing] = useState(false)
const preSubmit = async () => {
if (formValues.name.length > 0 && formValues.email.length > 0) {
setProcessing(true)
// Wait 2 seconds to simulate async delay (maybe user confirmation? or
// external checks?)
await (new Promise(resolve => setTimeout(resolve, 2000)))
return true
}
else {
return false
}
}
// Post-Submit for navigating to 'Thank You' page .. or maybe displaying 'sent!'
// text; totally up to you!
const postSubmit = () => {
console.log('Sent!')
setProcessing(false)
navigate('/hooray')
}
// Simple controlled form setup
const handleChange = e => setFormValues({ ...formValues, [e.target.name]: e.target.value })
const [formValues, setFormValues] = useState({
name: '',
email: '',
message: ''
})
return (
<Layout>
<NetlifyForm
formName="Medium Complexity Form"
formValues={formValues}
preSubmit={preSubmit}
postSubmit={postSubmit}
automaticHoneypot={true}
>
<div style={{ padding: '10px' }}>
Your Name: <input type="text" name="name" value={formValues.name} onChange={handleChange} />
</div>
<div style={{ padding: '10px' }}>
Your Email: <input type="email" name="email" value={formValues.email} onChange={handleChange} />
</div>
<div style={{ padding: '10px' }}>
Message: <textarea name="message" value={formValues.message} onChange={handleChange} />
</div>
<div style={{ padding: '10px' }}>
<button disabled={processing} type="submit">Send</button>
</div>
</NetlifyForm>
<div>
<Link to="/" >For a simpler form, click here</Link>
</div>
<div>
<a href="https://github.com/jon-sully/react-ssg-netlify-forms-demo/blob/master/src/pages/medium.js">To see the code, click here!</a>
</div>
</Layout>
)
}
export default IndexPage