This is a custom thank you page for form submissions
7 |
8 | );
9 |
--------------------------------------------------------------------------------
/src/pages/index.js:
--------------------------------------------------------------------------------
1 | import React from "react";
2 | import Link from "gatsby-link";
3 | import Helmet from "react-helmet";
4 |
5 | export default class Index extends React.Component {
6 | render() {
7 | return (
8 |
9 |
Hi people
10 |
11 | This is an example site integrating Netlify’s form handling with Gatsby
12 |
13 |
14 |
Basic contact form
15 |
Form with file upload
16 |
Form with reCAPTCHA 2
17 |
18 |
19 | );
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Logs
2 | logs
3 | *.log
4 |
5 | # Runtime data
6 | pids
7 | *.pid
8 | *.seed
9 |
10 | # Directory for instrumented libs generated by jscoverage/JSCover
11 | lib-cov
12 |
13 | # Coverage directory used by tools like istanbul
14 | coverage
15 |
16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
17 | .grunt
18 |
19 | # node-waf configuration
20 | .lock-wscript
21 |
22 | # Compiled binary addons (http://nodejs.org/api/addons.html)
23 | build/Release
24 |
25 | # Dependency directory
26 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
27 | node_modules
28 | .gatsby-context.js
29 | .sass-cache/
30 | public/
31 | .cache/
32 | .DS_Store
33 |
34 | # Environment variables
35 | .env.development
36 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "gatsby-starter-default",
3 | "description": "Gatsby default starter",
4 | "version": "1.0.0",
5 | "author": "Kyle Mathews ",
6 | "dependencies": {
7 | "gatsby": "^1.9.261",
8 | "gatsby-link": "^1.0.1",
9 | "gatsby-plugin-react-helmet": "^1.0.1",
10 | "react-async-script": "^0.9.1",
11 | "react-google-recaptcha": "^0.11.1"
12 | },
13 | "devDependencies": {
14 | "dotenv": "^5.0.1",
15 | "gh-pages": "^0.12.0"
16 | },
17 | "keywords": [
18 | "gatsby"
19 | ],
20 | "license": "MIT",
21 | "main": "n/a",
22 | "scripts": {
23 | "build": "gatsby build",
24 | "deploy": "gatsby build --prefix-paths && gh-pages -d public",
25 | "develop": "gatsby develop",
26 | "format": "prettier --trailing-comma es5 --no-semi --single-quote --write \"pages/*.js\" \"utils/*.js\" \"wrappers/*.js\" \"html.js\"",
27 | "test": "echo \"Error: no test specified\" && exit 1"
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | # back to language cpp to try to bypass osx node failure
2 | language: cpp
3 | sudo: false
4 | env:
5 | - export NODE_VERSION="0.12"
6 | - export NODE_VERSION="4"
7 | - export NODE_VERSION="5"
8 | os:
9 | - linux
10 | - osx
11 | # pre-install to bring in the correct version of node via nvm
12 | before_install:
13 | - git submodule update --init --recursive
14 | - git clone https://github.com/creationix/nvm.git ./.nvm
15 | - source ./.nvm/nvm.sh
16 | - nvm install $NODE_VERSION
17 | - nvm use $NODE_VERSION
18 | - npm config set python `which python`
19 | - if [ $TRAVIS_OS_NAME == "linux" ]; then
20 | export CC="gcc-4.8";
21 | export CXX="g++-4.8";
22 | export LINK="gcc-4.8";
23 | export LINKXX="g++-4.8";
24 | fi
25 | - gcc --version
26 | - g++ --version
27 | # node 4 depends on gcc 4.8
28 | addons:
29 | apt:
30 | sources:
31 | - ubuntu-toolchain-r-test
32 | packages:
33 | - g++-4.8
34 | - gcc-4.8
35 | # script needed to test, because defaults don't work on osx
36 | script:
37 | - npm install
38 | - npm run lint
39 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2015 gatsbyjs
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
23 |
--------------------------------------------------------------------------------
/src/html.js:
--------------------------------------------------------------------------------
1 | import React from "react"
2 | import PropTypes from "prop-types"
3 |
4 | const BUILD_TIME = new Date().getTime()
5 |
6 | export default class HTML extends React.Component {
7 | static propTypes = {
8 | body: PropTypes.string,
9 | }
10 |
11 | render() {
12 | let css
13 | if (process.env.NODE_ENV === "production") {
14 | css = (
15 |
20 | )
21 | }
22 |
23 | return (
24 |
25 |
26 |
27 |
28 |
32 | {this.props.headComponents}
33 | {css}
34 |
35 |
36 |
40 | {this.props.postBodyComponents}
41 |
42 |
43 | )
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/src/layouts/index.js:
--------------------------------------------------------------------------------
1 | import React from "react"
2 | import PropTypes from "prop-types"
3 | import Link from "gatsby-link"
4 | import Helmet from "react-helmet"
5 |
6 | import "../css/typography.css"
7 |
8 | export default class Template extends React.Component {
9 | static propTypes = {
10 | children: PropTypes.func,
11 | }
12 |
13 | render() {
14 | return (
15 |
16 |
23 |
29 |
36 |
37 |
44 | Gatsby
45 |
46 |
47 |
48 |
49 |
57 | {this.props.children()}
58 |
59 |
60 | )
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Integrating Netlify Form Handling in Gatsby
2 |
3 | Example for integrating a basic contact form with Netlify’s form handling feature (based on the [default Gatsby starter](https://github.com/gatsbyjs/gatsby-starter-default))
4 |
5 | Demo: https://gatsby-form-example.netlify.com
6 |
7 | ## Deploy
8 |
9 | [](https://app.netlify.com/start/deploy?repository=https://github.com/imorente/gatsby-netlify-form-example)
10 |
11 | ## reCAPTCHA
12 |
13 | This example site uses [react-google-recaptcha](https://github.com/dozoisch/react-google-recaptcha) to render the reCAPTCHA widget.
14 |
15 | To make the reCAPTCHA example work in your own copy of this site, you’ll need to do the following:
16 | 1. [Sign up for a reCAPTCHA API key pair](http://www.google.com/recaptcha/admin) for your site.
17 | 2. [Log in to your Netlify account](https://app.netlify.com), and add the following
18 | environment variables to your site’s Settings > Build & deploy > Build environment variables:
19 | - `SITE_RECAPTCHA_KEY` with your reCAPTCHA site key.
20 | - `SITE_RECAPTCHA_SECRET` with your reCAPTCHA secret key.
21 |
22 | **Important**: the environment variables need to be called `SITE_RECAPTCHA_KEY` and `SITE_RECAPTCHA_SECRET` for the Netlify backend to find them. If you add a `GATSBY_` prefix to the variable names, the Netlify backend won't recognize them, the reCAPTCHA verification will fail, and your form submissions won't be stored.
23 |
24 | 3. Change the build command for your site to
25 | ```
26 | echo SITE_RECAPTCHA_KEY=$SITE_RECAPTCHA_KEY >> .env.production && gatsby build
27 | ```
28 | This will make the SITE_RECAPTCHA_KEY available to the Gatsby build in production.
29 |
30 | To see the reCAPTCHA widget locally, add `SITE_RECAPTCHA_KEY=your-reCAPTCHA-API-site-key`
31 | to your local [.env.development](https://www.gatsbyjs.org/docs/environment-variables/) file.
32 |
33 | ## Troubleshooting
34 |
35 | ### Forms stop working after upgrading to Gatsby v2
36 | This can be caused by the offline-plugin. [Workaround](https://github.com/gatsbyjs/gatsby/issues/7997#issuecomment-419749232) is to use `?no-cache=1` in the POST url to prevent the service worker from handling form submissions (Thanks to [@phmu_office](https://twitter.com/phmu_office/status/1047810173417472000) for the heads up ✨)
37 |
--------------------------------------------------------------------------------
/src/pages/contact.js:
--------------------------------------------------------------------------------
1 | import React from "react";
2 | import { navigateTo } from "gatsby-link";
3 |
4 | function encode(data) {
5 | return Object.keys(data)
6 | .map(key => encodeURIComponent(key) + "=" + encodeURIComponent(data[key]))
7 | .join("&");
8 | }
9 |
10 | export default class Contact extends React.Component {
11 | constructor(props) {
12 | super(props);
13 | this.state = {};
14 | }
15 |
16 | handleChange = e => {
17 | this.setState({ [e.target.name]: e.target.value });
18 | };
19 |
20 | handleSubmit = e => {
21 | e.preventDefault();
22 | const form = e.target;
23 | fetch("/", {
24 | method: "POST",
25 | headers: { "Content-Type": "application/x-www-form-urlencoded" },
26 | body: encode({
27 | "form-name": form.getAttribute("name"),
28 | ...this.state
29 | })
30 | })
31 | .then(() => navigateTo(form.getAttribute("action")))
32 | .catch(error => alert(error));
33 | };
34 |
35 | render() {
36 | return (
37 |