|
| 1 | +import React from 'react'; |
| 2 | +import messages from 'lib/text'; |
| 3 | +import CezerinClient from 'cezerin-client'; |
| 4 | +import settings from 'lib/settings'; |
| 5 | +import * as auth from 'lib/auth'; |
| 6 | + |
| 7 | +import RaisedButton from 'material-ui/RaisedButton'; |
| 8 | +import Paper from 'material-ui/Paper'; |
| 9 | +import TextField from 'material-ui/TextField'; |
| 10 | + |
| 11 | +export default class LoginForm extends React.Component { |
| 12 | + constructor(props) { |
| 13 | + super(props); |
| 14 | + this.state = { |
| 15 | + email: localStorage.getItem('dashboard_email') || '', |
| 16 | + isFetching: false, |
| 17 | + isAuthorized: false, |
| 18 | + emailIsSent: false, |
| 19 | + error: null |
| 20 | + }; |
| 21 | + } |
| 22 | + |
| 23 | + handleChange = event => { |
| 24 | + this.setState({ |
| 25 | + email: event.target.value |
| 26 | + }); |
| 27 | + }; |
| 28 | + |
| 29 | + handleKeyPress = e => { |
| 30 | + if (e.keyCode === 13 || e.which === 13) { |
| 31 | + this.handleSubmit(); |
| 32 | + } |
| 33 | + }; |
| 34 | + |
| 35 | + handleSubmit = () => { |
| 36 | + this.setState({ |
| 37 | + isFetching: true, |
| 38 | + isAuthorized: false, |
| 39 | + emailIsSent: false, |
| 40 | + error: null |
| 41 | + }); |
| 42 | + |
| 43 | + CezerinClient.authorize(settings.apiBaseUrl, this.state.email) |
| 44 | + .then(authorizeResponse => { |
| 45 | + this.setState({ |
| 46 | + isFetching: false, |
| 47 | + isAuthorized: false, |
| 48 | + emailIsSent: authorizeResponse.json.sent, |
| 49 | + error: authorizeResponse.json.error |
| 50 | + }); |
| 51 | + }) |
| 52 | + .catch(error => { |
| 53 | + this.setState({ |
| 54 | + isFetching: false, |
| 55 | + isAuthorized: false, |
| 56 | + emailIsSent: false, |
| 57 | + error: error |
| 58 | + }); |
| 59 | + }); |
| 60 | + }; |
| 61 | + |
| 62 | + componentWillMount() { |
| 63 | + auth.checkTokenFromUrl(); |
| 64 | + } |
| 65 | + componentDidMount() {} |
| 66 | + |
| 67 | + render() { |
| 68 | + const { email, isFetching, isAuthorized, emailIsSent, error } = this.state; |
| 69 | + |
| 70 | + let response = null; |
| 71 | + if (isFetching) { |
| 72 | + response = ( |
| 73 | + <div className="loginSuccessResponse">{messages.messages_loading}</div> |
| 74 | + ); |
| 75 | + } else if (emailIsSent) { |
| 76 | + response = ( |
| 77 | + <div className="loginSuccessResponse">{messages.loginLinkSent}</div> |
| 78 | + ); |
| 79 | + } else if (emailIsSent === false && error) { |
| 80 | + response = <div className="loginErrorResponse">{error}</div>; |
| 81 | + } |
| 82 | + |
| 83 | + return ( |
| 84 | + <div className="row col-full-height center-xs middle-xs"> |
| 85 | + <div className="col-xs-12 col-sm-8 col-md-6 col-lg-4"> |
| 86 | + <Paper className="loginBox" zDepth={1}> |
| 87 | + <div className="loginTitle">{messages.loginTitle}</div> |
| 88 | + <div className="loginDescription">{messages.loginDescription}</div> |
| 89 | + <div className="loginInput"> |
| 90 | + <TextField |
| 91 | + type="email" |
| 92 | + value={email} |
| 93 | + onChange={this.handleChange} |
| 94 | + onKeyPress={this.handleKeyPress} |
| 95 | + label={messages.email} |
| 96 | + fullWidth={true} |
| 97 | + hintStyle={{ width: '100%' }} |
| 98 | + hintText={messages.email} |
| 99 | + /> |
| 100 | + </div> |
| 101 | + <RaisedButton |
| 102 | + label={messages.loginButton} |
| 103 | + primary={true} |
| 104 | + disabled={isFetching || emailIsSent} |
| 105 | + onClick={this.handleSubmit} |
| 106 | + /> |
| 107 | + {response} |
| 108 | + </Paper> |
| 109 | + </div> |
| 110 | + </div> |
| 111 | + ); |
| 112 | + } |
| 113 | +} |
0 commit comments