From e36fa6fdc527cf7beded6cc2a26c464e04693a1e Mon Sep 17 00:00:00 2001 From: Jinksi Date: Thu, 5 Oct 2017 09:08:27 +1000 Subject: [PATCH 1/3] Add --- src/App.js | 8 +- src/components/NetlifyForm.js | 230 ++++++++++++++++++++++++++++++++++ src/globalStyles.js | 33 ++++- src/views/Contact.js | 37 ++++++ 4 files changed, 306 insertions(+), 2 deletions(-) create mode 100644 src/components/NetlifyForm.js create mode 100644 src/views/Contact.js diff --git a/src/App.js b/src/App.js index bfe936b..9d26173 100644 --- a/src/App.js +++ b/src/App.js @@ -5,6 +5,7 @@ import Helmet from 'react-helmet' import ScrollToTop from './components/ScrollToTop' import Home from './views/Home' import About from './views/About' +import Contact from './views/Contact' import NoMatch from './views/NoMatch' import Nav from './components/Nav' import NavLink from './components/NavLink' @@ -13,7 +14,7 @@ import GithubCorner from './components/GithubCorner' import ServiceWorkerNotifications from './components/ServiceWorkerNotifications' import globalStyles from './globalStyles' -const siteTitle = 'HyperStatic' +export const siteTitle = 'HyperStatic' const routes = [ { title: 'Home', @@ -25,6 +26,11 @@ const routes = [ path: '/about/', comp: About, exact: true + }, { + title: 'Contact', + path: '/contact/', + comp: Contact, + exact: true } ] diff --git a/src/components/NetlifyForm.js b/src/components/NetlifyForm.js new file mode 100644 index 0000000..d8d050d --- /dev/null +++ b/src/components/NetlifyForm.js @@ -0,0 +1,230 @@ +import React, { Component } from 'react' +import styled from 'styled-components' +import { stringify } from 'qs' +import { color } from '../globalStyles' +import { siteTitle } from '../App' +const fetch = window.fetch + +class Form extends Component { + state = { + name: '', + email: '', + message: '', + subject: `New Submission from ${siteTitle}!`, + _gotcha: '', + disabled: false, + alert: '', + action: '/contact-us/', + 'form-name': 'Contact' + } + + form = null + + componentDidMount () { + Array.from(document.querySelectorAll('.Form .Input')).forEach(input => { + input.addEventListener('invalid', () => { + console.log(input) + input.dataset.touched = true + }) + input.addEventListener('blur', () => { + if (input.value !== '') input.dataset.touched = true + }) + }) + } + + handleSubmit = (e) => { + e.preventDefault() + const data = { + name: this.state.name, + email: this.state.email, + message: this.state.message, + subject: this.state.subject, + _gotcha: this.state._gotcha, + 'form-name': this.state['form-name'] + } + this.setState({ disabled: true }) + fetch(this.state.action + '?' + stringify(data), { + method: 'POST' + }) + .then(res => { + if (res.ok) { + return res + } else { + throw new Error('Network error') + } + }) + .then(res => { + this.setState({ + disabled: false, + alert: 'Thanks for your enquiry, we will get back to you soon.', + name: '', + email: '', + message: '', + subject: `New Submission from ${window.location.host}!`, + _gotcha: '' + }) + }) + .catch(err => { + console.log(err) + this.setState({ + disabled: false, + alert: '❗️ There is a problem, your message has not been sent, please try contacting us via email' + }) + }) + } + + handleChange = (e) => { + this.setState({ + [e.target.name]: e.target.value + }) + } + + render () { + return ( + { this.form = form }} + className='Form' + action={this.state.action} + onSubmit={this.handleSubmit} + data-netlify='' + data-netlify-honeypot='_gotcha' + > + { this.state.alert && {this.state.alert} } + + +