|
1 |
| -import React from 'react'; |
| 1 | +import React, { Component } from 'react'; |
| 2 | +import { withRouter } from 'react-router-dom'; |
| 3 | +import { compose } from 'recompose'; |
2 | 4 |
|
3 |
| -const SignIn = () => ( |
| 5 | +import { SignUpLink } from '../SignUp'; |
| 6 | +import { withFirebase } from '../Firebase'; |
| 7 | +import * as ROUTES from '../../constants/routes'; |
| 8 | + |
| 9 | +const SignInPage = () => ( |
4 | 10 | <div>
|
5 | 11 | <h1>SignIn</h1>
|
| 12 | + <SignInForm /> |
| 13 | + <SignUpLink /> |
6 | 14 | </div>
|
7 | 15 | );
|
8 | 16 |
|
9 |
| -export default SignIn; |
| 17 | +const INITIAL_STATE = { |
| 18 | + email: '', |
| 19 | + password: '', |
| 20 | + error: null, |
| 21 | +}; |
| 22 | + |
| 23 | +class SignInFormBase extends Component { |
| 24 | + constructor(props) { |
| 25 | + super(props); |
| 26 | + |
| 27 | + this.state = { ...INITIAL_STATE }; |
| 28 | + } |
| 29 | + |
| 30 | + onSubmit = event => { |
| 31 | + const { email, password } = this.state; |
| 32 | + |
| 33 | + this.props.firebase |
| 34 | + .doSignInWithEmailAndPassword(email, password) |
| 35 | + .then(() => { |
| 36 | + this.setState({ ...INITIAL_STATE }); |
| 37 | + this.props.history.push(ROUTES.HOME); |
| 38 | + }) |
| 39 | + .catch(error => { |
| 40 | + this.setState({ error }); |
| 41 | + }); |
| 42 | + |
| 43 | + event.preventDefault(); |
| 44 | + }; |
| 45 | + |
| 46 | + onChange = event => { |
| 47 | + this.setState({ [event.target.name]: event.target.value }); |
| 48 | + }; |
| 49 | + |
| 50 | + render() { |
| 51 | + const { email, password, error } = this.state; |
| 52 | + |
| 53 | + const isInvalid = password === '' || email === ''; |
| 54 | + |
| 55 | + return ( |
| 56 | + <form onSubmit={this.onSubmit}> |
| 57 | + <input |
| 58 | + name="email" |
| 59 | + value={email} |
| 60 | + onChange={this.onChange} |
| 61 | + type="text" |
| 62 | + placeholder="Email Address" |
| 63 | + /> |
| 64 | + <input |
| 65 | + name="password" |
| 66 | + value={password} |
| 67 | + onChange={this.onChange} |
| 68 | + type="password" |
| 69 | + placeholder="Password" |
| 70 | + /> |
| 71 | + <button disabled={isInvalid} type="submit"> |
| 72 | + Sign In |
| 73 | + </button> |
| 74 | + |
| 75 | + {error && <p>{error.message}</p>} |
| 76 | + </form> |
| 77 | + ); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +const SignInForm = compose( |
| 82 | + withRouter, |
| 83 | + withFirebase, |
| 84 | +)(SignInFormBase); |
| 85 | + |
| 86 | +export default SignInPage; |
| 87 | + |
| 88 | +export { SignInForm }; |
0 commit comments