react-static
Version:
A progressive static site generator for React
92 lines (79 loc) • 2.4 kB
JavaScript
import React, { Component } from 'react'
import { withRouter } from 'react-static'
import { auth, db } from '../firebase'
const updateByPropertyName = (propertyName, value) => () => ({
[propertyName]: value,
})
const INITIAL_STATE = {
username: '',
email: '',
passwordOne: '',
passwordTwo: '',
error: null,
}
class SignUpForm extends Component {
constructor (props) {
super(props)
this.state = { ...INITIAL_STATE }
}
onSubmit = event => {
const { username, email, passwordOne } = this.state
const { history } = this.props
auth.doCreateUserWithEmailAndPassword(email, passwordOne)
.then(authUser => {
db.doCreateUser(authUser.uid, username, email)
.then(() => {
this.setState(() => ({ ...INITIAL_STATE }))
history.push('/dashboard')
})
.catch(error => {
this.setState(updateByPropertyName('error', error))
})
})
.catch(error => {
this.setState(updateByPropertyName('error', error))
})
event.preventDefault()
}
render () {
const {
username, email, passwordOne, passwordTwo, error,
} = this.state
const isInvalid = passwordOne !== passwordTwo ||
passwordOne === '' || username === '' ||
email === ''
return (
<form onSubmit={this.onSubmit}>
<input
value={username}
onChange={event => this.setState(updateByPropertyName('username', event.target.value))}
type="text"
placeholder="Full Name"
/>
<input
value={email}
onChange={event => this.setState(updateByPropertyName('email', event.target.value))}
type="text"
placeholder="Email Address"
/>
<input
value={passwordOne}
onChange={event => this.setState(updateByPropertyName('passwordOne', event.target.value))}
type="password"
placeholder="Password"
/>
<input
value={passwordTwo}
onChange={event => this.setState(updateByPropertyName('passwordTwo', event.target.value))}
type="password"
placeholder="Confirm Password"
/>
<button disabled={isInvalid} type="Submit">
Sign Up
</button>
{ error && <p>{error.message}</p>}
</form>
)
}
}
export default withRouter(SignUpForm)