react-static
Version:
A progressive static site generator for React
68 lines (55 loc) • 1.59 kB
JavaScript
import React, { Component } from 'react'
import { withRouter } from 'react-static'
import { auth } from '../firebase'
const updateByPropertyName = (propertyName, value) => () => ({
[propertyName]: value,
})
const INITIAL_STATE = {
email: '',
password: '',
error: null,
}
class SignInForm extends Component {
constructor (props) {
super(props)
this.state = { ...INITIAL_STATE }
}
onSubmit = event => {
const { email, password } = this.state
const { history } = this.props
auth.doSignInWithEmailAndPassword(email, password)
.then(() => {
this.setState(() => ({ ...INITIAL_STATE }))
history.push('/dashboard')
})
.catch(error => {
this.setState(updateByPropertyName('error', error))
})
event.preventDefault()
}
render () {
const { email, password, error } = this.state
const isInvalid = password === '' || email === ''
return (
<form onSubmit={this.onSubmit}>
<input
value={email}
onChange={event => this.setState(updateByPropertyName('email', event.target.value))}
type="text"
placeholder="Email Address"
/>
<input
value={password}
onChange={event => this.setState(updateByPropertyName('password', event.target.value))}
type="password"
placeholder="password"
/>
<button disabled={isInvalid} type="submit">
Sign In
</button>
{ error && <p>{error.message}</p> }
</form>
)
}
}
export default withRouter(SignInForm)