fathom/assets/js/components/LoginForm.js

76 lines
1.8 KiB
JavaScript
Raw Normal View History

2016-11-23 18:40:35 +00:00
'use strict';
import { h, render, Component } from 'preact';
import Client from '../lib/client.js';
import Notification from '../components/Notification.js';
2018-05-01 14:06:17 +00:00
import { bind } from 'decko';
2016-11-23 18:40:35 +00:00
class LoginForm extends Component {
constructor(props) {
super(props)
this.state = {
2016-11-23 18:40:35 +00:00
email: '',
password: '',
message: ''
}
2016-11-23 18:40:35 +00:00
}
@bind
2016-11-23 18:40:35 +00:00
handleSubmit(e) {
e.preventDefault();
this.setState({ message: '' });
2016-11-23 18:40:35 +00:00
Client.request('session', {
2016-11-23 18:40:35 +00:00
method: "POST",
data: {
email: this.state.email,
password: this.state.password,
}
}).then((r) => {
this.props.onSuccess()
}).catch((e) => {
this.setState({
message: e.message,
password: ''
});
});
2016-11-23 18:40:35 +00:00
}
@bind
updatePassword(e) {
this.setState({ password: e.target.value });
}
@bind
updateEmail(e) {
this.setState({ email: e.target.value });
}
render(props, state) {
2016-11-23 18:40:35 +00:00
return (
<div class="block">
<h2>Login</h2>
2018-04-24 07:45:46 +00:00
<p>Please enter your login credentials to access your Fathom dashboard.</p>
2016-11-23 18:40:35 +00:00
<form method="POST" onSubmit={this.handleSubmit}>
<div class="small-margin">
2016-11-23 18:40:35 +00:00
<label>Email address</label>
<input type="email" name="email" placeholder="Email address" value={this.state.email} onInput={this.updateEmail} required="required" />
2016-11-23 18:40:35 +00:00
</div>
<div class="small-margin">
2016-11-23 18:40:35 +00:00
<label>Password</label>
<input type="password" name="password" placeholder="**********" value={this.state.password} onInput={this.updatePassword} required="required" />
2016-11-23 18:40:35 +00:00
</div>
<div class="small-margin">
2016-11-23 18:40:35 +00:00
<input type="submit" value="Sign in" />
</div>
</form>
{(state.message ? <Notification message={state.message} kind="" /> : '')}
2016-11-23 18:40:35 +00:00
</div>
2016-11-23 18:40:35 +00:00
)
}
}
export default LoginForm