Convert signup form to component

This commit is contained in:
Barry Low committed 2018-11-12 18:44:54 -08:00
1 parent ffa97b4b8c
commit d5c5969f8c
5 files changed
+279 -28

No files matched your search

@@ -0,0 +1,127 @@
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import { withStyles } from '@material-ui/core/styles'
import withWidth from '@material-ui/core/withWidth'
import { Mutation } from 'react-apollo'
import Typography from '@material-ui/core/Typography'
import Grid from '@material-ui/core/Grid'
import Button from '@material-ui/core/Button'
import InputBase from '@material-ui/core/InputBase'
import styles from './styles'
import NEWSLETTER_SIGNUP from '../graphql/NewsletterSignup'
import { isEmpty } from '../../../utils/js-helpers'
export class SignupForm extends Component {
constructor (props) {
super(props)
this.handleChange = this.handleChange.bind(this)
this.handleSubmit = this.handleSubmit.bind(this)
this.validate = this.validate.bind(this)
this.state = {
email: '',
emailError: ''
}
}
render () {
const { classes, width } = this.props
return (
<Mutation
mutation={NEWSLETTER_SIGNUP}
>
{(NewsletterSignup, { loading, error, data }) => {
let successMessage = false
if (data.NewsletterSignup === 'SUCESS') {
successMessage = true
}
return (
<Grid container className={classes.signupRight} direction='column'>
<Typography variant='h4' className={classes.textWhite} gutterBottom>Stay in touch!</Typography>
<Typography variant='subtitle1' className={classes.textWhite} gutterBottom>Opt-in our mailing list to stay up to date with the Advanced Algos Project.</Typography>
<form id='email-signup' action='#' className='form-inline' autoComplete='off'>
<Typography
variant='body2'
className={classes.textSuccess}
gutterBottom
style={successMessage ? 'display: block' : 'display: none'}
>
Thank you for your interest! We'll keep you informed
</Typography>
<Grid container className={classes.signupContainer} justify={width === 'xs' ? 'center' : 'flex-start'}>
<InputBase
id='email'
placeholder='Enter your email'
classes={{
root: classes.signupInputRoot,
input: classes.signupInput
}}
onChange={this.handleChange}
/>
<Button
id='email-submit'
className={classes.signupInputSubmit}
onClick={e => {
this.handleSubmit(e, NewsletterSignup, this.state.email)
}}
>
Submit
</Button>
</Grid>
</form>
</Grid>
)
}}
</Mutation>
)
}
validate (data) {
const errors = {}
switch (data.id) {
case 'email':
if (data.value === '') {
this.setState({ emailError: 'Please enter an email address' })
} else {
this.setState({ emailError: '' })
}
break
default:
}
return errors
}
handleChange (e) {
let id = e.target.id
let value = e.target.value
let error = this.validate({ id, value })
switch (id) {
case 'email':
if (!isEmpty(error)) this.setState({ emailError: error })
this.setState({ emaail: value })
break
default:
}
}
async handleSubmit (e, NewsletterSignup, email) {
e.preventDefault()
const signupResponse = await NewsletterSignup({ variables: { email } })
console.log('NewsletterSignup: ', signupResponse)
this.setState({ email: '' })
}
}
SignupForm.propTypes = {
classes: PropTypes.object,
width: PropTypes.string
}
const SignupFormWithStyles = withStyles(styles)(SignupForm)
export default withWidth()(SignupFormWithStyles)
@@ -0,0 +1,68 @@
import React from 'react'
import PropTypes from 'prop-types'
import { BannerTopBar } from '../../common'
import SignupForm from './SignupForm'
export class SignupVerifyResponse extends React.Component {
componentDidMount () {
const { mutate } = this.props
mutate()
};
render () {
const { loading, error, data } = this.props
let BannerTitle = ''
let BannerText = ''
let displayForm = false
console.log('SignupVerifyResponse:', loading, error, data)
if (error) {
if (error.message === 'GraphQL error: Error: Token Expired. Please resubmit email address.') {
BannerText = [
'Newsletter Signup Error: Verification Token Expired',
<br key='EmailSignupConfirm-error-br' />,
'Please try signing up again! We apologize for the inconvenience,',
<br key='EmailSignupConfirm-error-br2' />,
'but we highly value your seurity and privacy.'
]
} else {
BannerText = ['Newsletter Signup Error: ', <br key='EmailSignupConfirm-error-br' />, error.message]
}
}
if (loading && !error) {
BannerText = ['Processing newsletter sign-up...', <br key='EmailSignupConfirm-br' />, 'Thank you for opting-in to recieve updates about the Advanced Algos project!']
}
if (data !== undefined && data !== null) {
console.log(data)
return (
<React.Fragment>
<BannerTopBar
size='big'
title={BannerTitle}
text={BannerText}
backgroundUrl='https://aacorporatesitedevelop.azurewebsites.net/img/photos/superalgos-platform.jpg'
/>
{displayForm && <SignupForm />}
</React.Fragment>
)
} else {
return (
<BannerTopBar
size='big'
title=''
text='Newsletter Signup Error'
backgroundUrl='https://aacorporatesitedevelop.azurewebsites.net/img/photos/superalgos-platform.jpg'
/>
)
}
}
}
SignupVerifyResponse.propTypes = {
mutate: PropTypes.string,
loading: PropTypes.bool,
error: PropTypes.object,
data: PropTypes.object
}
export default SignupVerifyResponse
@@ -0,0 +1,65 @@
import lightGreen from '@material-ui/core/colors/lightGreen'
const styles = theme => ({
textLight: {
color: theme.palette.primary.light
},
textWhite: {
color: theme.palette.common.white
},
textSuccess: {
color: lightGreen[400]
},
copyright: {
paddingTop: theme.spacing.unit * 3
},
linkLight: {
color: theme.palette.secondary.main,
textDecoration: 'none'
},
signupRight: {
marginTop: theme.spacing.unit * 2,
[theme.breakpoints.down('xs')]: {
textAlign: 'center'
}
},
signupContainer: {
marginBottom: theme.spacing.unit * 2,
[theme.breakpoints.down('xs')]: {
textAlign: 'center'
}
},
signupInput: {
borderRadius: 4,
backgroundColor: theme.palette.common.white,
border: '1px solid #ced4da',
fontSize: 16,
padding: '10px 12px',
transition: theme.transitions.create(['border-color', 'box-shadow']),
marginRight: theme.spacing.unit * 1
},
signupInputSubmit: {
boxShadow: 'none',
textTransform: 'none',
fontSize: 16,
lineHeight: '1.1875em',
color: theme.palette.common.white,
padding: '10px 12px',
backgroundColor: '#5bc0de',
borderColor: '#46b8da',
'&:hover': {
backgroundColor: '#31b0d5',
borderColor: '#269abc'
},
'&:active': {
boxShadow: 'none',
backgroundColor: '#0062cc',
borderColor: '#005cbf'
},
'&:focus': {
boxShadow: '0 0 0 0.2rem rgba(0,123,255,.5)'
}
}
})
export default styles
@@ -0,0 +1,10 @@
/* eslint-disable no-unused-vars */
import gql from 'graphql-tag'
const NEWSLETTER_SIGNUP = gql`
mutation NewsletterSignup($email: String!) {
master_NewsletterSignup(email: $email)
}
`
export default NEWSLETTER_SIGNUP
+9 -28
View File
@@ -4,12 +4,12 @@ import queryString from 'query-string'
import { Mutation } from 'react-apollo'
import { BannerTopBar } from '../common'
import SignupVerifyResponse from './components/SignupVerifyResponse'
import NEWSLETTER_SIGNUP_VERIFY from './graphql/NewsletterSignupVerify'
export const EmailSignupConfirm = ({ tokenParam }) => {
let token
let BannerTitle = ''
let BannerText = ''
if (tokenParam !== '' && tokenParam !== null) {
const values = queryString.parse(tokenParam)
@@ -38,33 +38,14 @@ export const EmailSignupConfirm = ({ tokenParam }) => {
variables={{ token: token }}
>
{(NewsletterSignupVerify, { loading, error, data }) => {
console.log(loading, error, data)
if (error) {
BannerText = ['Newsletter Signup Error: ', <br key='EmailSignupConfirm-error-br' />, JSON.stringify(error)]
}
if (loading && !error) {
BannerText = ['Processing newsletter sign-up...', <br key='EmailSignupConfirm-br' />, 'Thank you for opting-in to recieve updates about the Advanced Algos project!']
}
if (data !== undefined && data !== null) {
console.log(data)
return (
<BannerTopBar
size='big'
title={BannerTitle}
text={BannerText}
backgroundUrl='https://aacorporatesitedevelop.azurewebsites.net/img/photos/superalgos-platform.jpg'
/>
)
} else {
return (
<BannerTopBar
size='big'
title=''
text='Newsletter Signup Error'
backgroundUrl='https://aacorporatesitedevelop.azurewebsites.net/img/photos/superalgos-platform.jpg'
/>
)
}
return (
<SignupVerifyResponse
mutate={NewsletterSignupVerify}
loading={loading}
error={error}
data={data}
/>
)
}
}
</Mutation>