-
Notifications
You must be signed in to change notification settings - Fork 73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Suggestion box #324
Merged
magnificode
merged 7 commits into
Rebuild-Black-Business:main
from
SeHarlan:suggestionBox
Aug 27, 2020
Merged
Suggestion box #324
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
752b841
suggestions box component with netlify form integration
SeHarlan 540c1d9
suggtion form with netlify intigration
SeHarlan f188d32
suggetion form added to contact us section on the about us page
SeHarlan bded262
removed business contact card, added titles to all forms
SeHarlan 44f2e48
spleeing error corrected, envEXAMPLES added back, netlify form wrap a…
SeHarlan a0f046e
cleaned up aboutus and contact card to remove business form logic
SeHarlan 591f2bf
netlify form props added
SeHarlan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
import React, { useState } from 'react'; | ||
import { | ||
FormControl, | ||
Flex, | ||
useTheme, | ||
FormLabel, | ||
Input, | ||
Textarea, | ||
Text, | ||
NumberInput, | ||
NumberInputField, | ||
NumberInputStepper, | ||
NumberIncrementStepper, | ||
NumberDecrementStepper, | ||
} from '@chakra-ui/core'; | ||
import PrimaryButton from '../Buttons/PrimaryButton'; | ||
|
||
const encode = data => { | ||
return Object.keys(data) | ||
.map(key => encodeURIComponent(key) + '=' + encodeURIComponent(data[key])) | ||
.join('&'); | ||
}; | ||
|
||
//how to intigrate with netlifys for handling | ||
//https://www.netlify.com/blog/2017/07/20/how-to-integrate-netlifys-form-handling-in-a-react-app/#form-handling-with-static-site-generators | ||
|
||
export default function SuggestionBox() { | ||
const [topic, setTopic] = useState(null); | ||
const [description, setDescription] = useState(null); | ||
const [benifits, setBenifits] = useState(null); | ||
const [urgency, setUrgency] = useState(5); | ||
const [name, setName] = useState(''); | ||
const [email, setEmail] = useState(''); | ||
const [validationMessage, setValidationMessage] = useState(null); | ||
const [submitted, setSubmitted] = useState(false); | ||
|
||
const theme = useTheme(); | ||
|
||
const handleSubmit = event => { | ||
const toSubmit = { | ||
topic, | ||
description, | ||
benifits, | ||
urgency, | ||
name, | ||
email, | ||
}; | ||
|
||
//Custom Validation | ||
const valuesToValidate = Object.values(toSubmit); | ||
//Validates all required fields (initiated with null) are filled | ||
if (valuesToValidate.includes(null)) { | ||
setValidationMessage('All fields with * are required.'); | ||
return; | ||
} | ||
|
||
//posts to Netlify intigration | ||
fetch('/', { | ||
method: 'POST', | ||
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, | ||
body: encode({ 'form-name': 'contact', toSubmit }), | ||
}) | ||
.then(() => console.log('Success!')) | ||
.catch(error => console.log(error)); | ||
|
||
event.preventDefault(); | ||
setSubmitted(true); | ||
}; | ||
|
||
if (submitted) { | ||
return ( | ||
<Text fontSize="2xl" textAlign="center"> | ||
Thank you for your suggestion! | ||
</Text> | ||
); | ||
} | ||
|
||
return ( | ||
<FormControl | ||
width="100%" | ||
maxWidth="1000px" | ||
margin="0 auto 3rem" | ||
padding="0 24px" | ||
onKeyPress={event => { | ||
if (event.key === 'Enter') { | ||
handleSubmit(event); | ||
} | ||
}} | ||
//netlify form handling | ||
method="post" | ||
data-netlify-honeypot="bot-field" | ||
data-netlify="true" | ||
name="contact" | ||
> | ||
{/* netlify form handling */} | ||
<input type="hidden" name="bot-field" /> | ||
<input type="hidden" name="form-name" value="contact" /> | ||
{/* netlify form handling */} | ||
|
||
<Text fontSize="xl" textAlign="center"> | ||
Suggestion Box | ||
</Text> | ||
|
||
{/* renders when form is submitted with validation errors */} | ||
{validationMessage && <Text textAlign="center">{validationMessage}</Text>} | ||
|
||
<Flex direction="column" margin={theme.spacing.base}> | ||
<FormLabel isRequired htmlFor="topic"> | ||
Topic | ||
</FormLabel> | ||
<Input | ||
value={topic} | ||
id="topic" | ||
type="text" | ||
placeholder="e.g. Business, Functionality, New Feature" | ||
onChange={event => setTopic(event.currentTarget.value)} | ||
/> | ||
</Flex> | ||
|
||
<Flex direction="column" margin={theme.spacing.base}> | ||
<FormLabel isRequired htmlFor="textField"> | ||
Description | ||
</FormLabel> | ||
<Textarea | ||
value={description} | ||
id="description" | ||
placeholder="Description" | ||
onChange={event => setDescription(event.currentTarget.value)} | ||
/> | ||
</Flex> | ||
|
||
<Flex direction="column" margin={theme.spacing.base}> | ||
<FormLabel isRequired htmlFor="benifits"> | ||
Benifits | ||
magnificode marked this conversation as resolved.
Show resolved
Hide resolved
|
||
</FormLabel> | ||
<Textarea | ||
value={benifits} | ||
id="benifits" | ||
placeholder="Benifits" | ||
onChange={event => setBenifits(event.currentTarget.value)} | ||
/> | ||
</Flex> | ||
|
||
<Flex direction="column" margin={theme.spacing.base}> | ||
<FormLabel isRequired htmlFor="urgency"> | ||
Urgency: 1 (Most Urgent) - 5 (Not Urgent) | ||
</FormLabel> | ||
<NumberInput | ||
id="urgency" | ||
type="number" | ||
min={1} | ||
max={5} | ||
value={urgency} | ||
onChange={value => setUrgency(value > 5 ? 5 : value)} | ||
> | ||
<NumberInputField /> | ||
<NumberInputStepper> | ||
<NumberIncrementStepper /> | ||
<NumberDecrementStepper /> | ||
</NumberInputStepper> | ||
</NumberInput> | ||
</Flex> | ||
|
||
<Flex width="100%" direction="column"> | ||
<Flex direction="column" margin={theme.spacing.base}> | ||
<FormLabel htmlFor="name">Name</FormLabel> | ||
<Input | ||
value={name} | ||
id="name" | ||
type="text" | ||
placeholder="Name" | ||
onChange={event => setName(event.currentTarget.value)} | ||
/> | ||
</Flex> | ||
<Flex direction="column" margin={theme.spacing.base}> | ||
<FormLabel htmlFor="email">Email</FormLabel> | ||
<Input | ||
value={email} | ||
id="email" | ||
type="text" | ||
placeholder="Email" | ||
onChange={event => setEmail(event.currentTarget.value)} | ||
/> | ||
</Flex> | ||
|
||
<Flex width="100%" justify="center"> | ||
<PrimaryButton onClick={handleSubmit}>Submit</PrimaryButton> | ||
magnificode marked this conversation as resolved.
Show resolved
Hide resolved
|
||
</Flex> | ||
</Flex> | ||
</FormControl> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It appears that this
FormControl
element is outputting a div instead of a<form>
element. It appears that we may just need to manually wrap the form elements with<form netilfy>
to get those form submissions working properly!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @SeHarlan - I'm not sure if this is just a challenge of testing the form on the deployment preview, but the form still doesn't get dropped into Netlify.
Do you mind trying one more thing for me? After looking at the documentation https://docs.netlify.com/forms/setup/#html-forms it appears that we may need a couple of additional attributes on the form. Primarily
name
andmethod
.Could you try adding the following:
name="suggestion-box"
and
method="POST"
Let's give this a shot, and if we still aren't getting form submissions through the deploy preview, we'll merge and see what we get :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like Netlify is properly recognizing the form, the honeypot and some Netlify specific elements seem to be populating. I'm going to merge this and test the submissions on production!