Last active
March 6, 2020 01:37
-
-
Save jsorkin24/3d76b14fa8146cf3678b46016b43e390 to your computer and use it in GitHub Desktop.
Index.js file for Contact Form
This file contains hidden or 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
| import React, { useState } from 'react' | |
| import { Axios, db } from '../firebase/firebaseConfig' | |
| import './styled.scss' | |
| const ContactForm = () => { | |
| const [formData, setFormData] = useState({}) | |
| const updateInput = e => { | |
| setFormData({ | |
| ...formData, | |
| [e.target.name]: e.target.value, | |
| }) | |
| } | |
| const handleSubmit = event => { | |
| event.preventDefault() | |
| sendEmail() | |
| setFormData({ | |
| name: '', | |
| email: '', | |
| message: '', | |
| }) | |
| } | |
| const sendEmail = () => { | |
| Axios.post( | |
| 'https://us-central1-your-app-name.cloudfunctions.net/submit', | |
| formData | |
| ) | |
| .then(res => { | |
| db.collection('emails').add({ | |
| name: formData.name, | |
| email: formData.email, | |
| message: formData.message, | |
| time: new Date(), | |
| }) | |
| }) | |
| .catch(error => { | |
| console.log(error) | |
| }) | |
| } | |
| return ( | |
| <> | |
| <form onSubmit={handleSubmit}> | |
| <input | |
| type="text" | |
| name="name" | |
| placeholder="Name" | |
| onChange={updateInput} | |
| value={formData.name || ''} | |
| /> | |
| <input | |
| type="email" | |
| name="email" | |
| placeholder="Email" | |
| onChange={updateInput} | |
| value={formData.email || ''} | |
| /> | |
| <textarea | |
| type="text" | |
| name="message" | |
| placeholder="Message" | |
| onChange={updateInput} | |
| value={formData.message || ''} | |
| ></textarea> | |
| <button type="submit">Submit</button> | |
| </form> | |
| </> | |
| ) | |
| } | |
| export default ContactForm |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment