Skip to content

Commit

Permalink
Merge pull request #19 from v4iv/development
Browse files Browse the repository at this point in the history
feat: Contact Form using Netlify
  • Loading branch information
v4iv authored Mar 9, 2019
2 parents cb5f78c + 2a6e539 commit b10c22a
Show file tree
Hide file tree
Showing 14 changed files with 135 additions and 86 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2018 Vaibhav Sharma
Copyright (c) 2019 Vaibhav Sharma

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ It follows the [JAMstack architecture](https://jamstack.org) by using Git as a s
* Disqus and Share Support
* Elastic-Lunr Search (NEW)
* Pagination (NEW)
* Contact Form (Netlify Forms)
* Easy Configuration using `config.js` file

## Prerequisite
Expand Down
2 changes: 1 addition & 1 deletion data/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports = {
userTwitter: 'vaibhaved',
userLocation: 'Delhi NCR, India',
userDescription: '',
copyright: 'Copyright © Gatsby Starter Business 2018. All Rights Reserved.', // Copyright string for the footer of the website and RSS feed.
copyright: 'Copyright © Gatsby Starter Business 2018-2019. All Rights Reserved.', // Copyright string for the footer of the website and RSS feed.
themeColor: '#00d1b2', // Used for setting manifest and progress theme colors.
backgroundColor: '#ffffff', // Used for setting manifest background color.
}
4 changes: 0 additions & 4 deletions src/cms/preview-templates/ContactPagePreview.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@ import PropTypes from 'prop-types'
import ContactPageTemplate from '../../components/ContactPageTemplate'

const ContactPagePreview = ({ entry, getAsset }) => {
const entryContacts = entry.getIn(['data', 'contacts'])
const contacts = entryContacts ? entryContacts.toJS() : []

return (
<ContactPageTemplate
title={entry.getIn(['data', 'title'])}
subtitle={entry.getIn(['data', 'subtitle'])}
meta_title={entry.getIn(['data', 'meta_title'])}
meta_description={entry.getIn(['data', 'meta_description'])}
contacts={contacts}
/>
)
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/AboutPageTemplate/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const AboutPageTemplate = ({title, content, contentComponent}) => {

return (
<div>
<section className='hero is-primary is-bold'>
<section className='hero is-primary is-bold is-medium'>
<div className='hero-body'>
<div className='container'>
<div className='columns'>
Expand Down
20 changes: 0 additions & 20 deletions src/components/Contact/index.js

This file was deleted.

152 changes: 111 additions & 41 deletions src/components/ContactPageTemplate/index.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,127 @@
import React from 'react'
import React, {Component} from 'react'
import { navigate } from 'gatsby-link'
import Helmet from 'react-helmet'
import Contact from '../Contact'
import PropTypes from 'prop-types'

const ContactPageTemplate = ({
title,
subtitle,
meta_title,
meta_description,
contacts,
}) => {
return (
<div>
<Helmet>
<title>{meta_title}</title>
<meta name='description' content={meta_description} />
</Helmet>
<section className='hero is-primary is-bold'>
<div className='hero-body'>
<div className='container'>
<div className='columns'>
<div className='column is-10 is-offset-1'>
<div className='section'>
<h1 className='title'>
{title}
</h1>
<h2 className='subtitle'>
{subtitle}
</h2>
const encode = (data) => {
return Object.keys(data)
.map(key => encodeURIComponent(key) + '=' + encodeURIComponent(data[key]))
.join('&')
}

class ContactPageTemplate extends Component {
constructor (props) {
super(props)
this.state = { isValidated: false }
}

handleChange = e => {
this.setState({ [e.target.name]: e.target.value })
}

handleSubmit = e => {
e.preventDefault()
const form = e.target
// eslint-disable-next-line
fetch('/?no-cache=1', {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: encode({
'form-name': form.getAttribute('name'),
...this.state,
}),
})
.then(() => navigate(form.getAttribute('action')))
// eslint-disable-next-line
.catch(error => alert(error))
}

render () {
const {title, subtitle, meta_title, meta_description} = this.props
return (
<div>
<Helmet>
<title>{meta_title}</title>
<meta name='description' content={meta_description} />
</Helmet>
<section className='hero is-primary is-bold is-medium'>
<div className='hero-body'>
<div className='container'>
<div className='columns'>
<div className='column is-10 is-offset-1'>
<div className='section'>
<h1 className='title'>
{title}
</h1>
<h2 className='subtitle'>
{subtitle}
</h2>
</div>
</div>
</div>
</div>
</div>
</div>
</section>
<section className='section'>
<div className='container'>
{contacts.map((contact, id) =>
<Contact key={id} email={contact.email} description={contact.description} />
)}
</div>
</section>
</div>
)
}
</section>
<section className='section'>
<div className='container'>
<form
name='contact'
method='post'
action='/contact/success'
encType='application/x-www-form-urlencoded'
data-netlify='true'
data-netlify-honeypot='bot-field'
onSubmit={this.handleSubmit}
>
<input type='hidden' name='form-name' value='contact' />
<div hidden>
<label>
Don’t fill this out:{' '}
<input name='bot-field' onChange={this.handleChange} />
</label>
</div>
<div className='field'>
<label className='label'>Name</label>
<div className='control'>
<input className='input' type='text' placeholder='Full Name' name='name' id='name' onChange={this.handleChange} />
</div>
</div>

<div className='field'>
<label className='label'>Email</label>
<div className='control'>
<input className='input' type='email' placeholder='Email' name='email' id='email' onChange={this.handleChange} />
</div>
</div>

<div className='field'>
<label className='label'>Message</label>
<div className='control'>
<textarea className='textarea' placeholder='Message' name='message' id='message' rows='6' onChange={this.handleChange} />
</div>
</div>

<div className='field is-grouped is-pulled-right'>
<div className='control'>
<button className='button is-text' type='reset'>Cancel</button>
</div>
<div className='control'>
<button className='button is-primary' type='submit'>Submit</button>
</div>
</div>
</form>
</div>
</section>
</div>
)
}
};

ContactPageTemplate.propTypes = {
title: PropTypes.string,
subtitle: PropTypes.string,
meta_title: PropTypes.string,
meta_description: PropTypes.string,
contacts: PropTypes.array,

}

export default ContactPageTemplate
1 change: 1 addition & 0 deletions src/components/Footer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const Footer = () => {
<p>
{config.copyright}
</p>
<p>Powered by <a href='https://www.gatsbyjs.org'>Gatsby</a> and <a href='https://www.netlifycms.org'>Netlify CMS</a> | <a href='https://github.com/v4iv/gatsby-starter-business'>Github Repository</a></p>
</div>
</div>
</footer>
Expand Down
2 changes: 1 addition & 1 deletion src/components/HomePageTemplate/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const HomePageTemplate = ({
<title>{meta_title}</title>
<meta name='description' content={meta_description} />
</Helmet>
<section className='hero is-primary is-bold'>
<section className='hero is-primary is-bold is-medium'>
<div className='hero-body'>
<div className='container'>
<div className='columns'>
Expand Down
2 changes: 1 addition & 1 deletion src/components/PricingPageTemplate/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const PricingPageTemplate = ({
<title>{meta_title}</title>
<meta name='description' content={meta_description} />
</Helmet>
<section className='hero is-primary is-bold'>
<section className='hero is-primary is-bold is-medium'>
<div className='hero-body'>
<div className='container'>
<div className='columns'>
Expand Down
11 changes: 1 addition & 10 deletions src/pages/contact/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,4 @@ meta_description: >-
ridiculus mus. Aenean eu leo quam. Pellentesque ornare sem lacinia quam
venenatis vestibulum. Sed posuere consectetur est at lobortis. Cras mattis
consectetur purus sit amet fermentum.
contacts:
- email: support@lorem.com
description: >-
Donec scelerisque magna nec condimentum porttitor. Aliquam vel diam sed diam luctus pretium.
Sed quis egestas libero. Vestibulum nec venenatis ligula.
- email: contact@lorem.com
description: >-
Fusce porttitor vulputate enim, nec blandit magna gravida et. Etiam et dignissim ligula.
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
---
---
15 changes: 15 additions & 0 deletions src/pages/contact/success/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from 'react'

const SuccessPage = () => {
return (
<section className='hero is-primary is-bold is-large'>
<div className='hero-body has-text-centered'>
<h1 className='title'>
Success
</h1>
</div>
</section>
)
}

export default SuccessPage
2 changes: 1 addition & 1 deletion src/pages/tags/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const TagsPage = ({
}) => (
<div>
<Helmet title={`Tags | ${title}`} />
<section className='hero is-primary is-bold'>
<section className='hero is-primary is-bold is-medium'>
<div className='hero-body'>
<div className='container'>
<div className='columns'>
Expand Down
5 changes: 0 additions & 5 deletions src/templates/contact-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ const ContactPage = ({data}) => {
subtitle={frontmatter.subtitle}
meta_title={frontmatter.meta_title}
meta_description={frontmatter.meta_description}
contacts={frontmatter.contacts}
/>
)
}
Expand All @@ -35,10 +34,6 @@ export const contactPageQuery = graphql`
meta_title
meta_description
heading
contacts {
email
description
}
}
}
}
Expand Down

0 comments on commit b10c22a

Please sign in to comment.