2016-10-21 17:20:36 +00:00
|
|
|
import React, {Component} from 'react';
|
2016-10-21 18:58:06 +00:00
|
|
|
import Link from '../base/Link';
|
2016-10-21 17:20:36 +00:00
|
|
|
import Modal from '../base/Modal';
|
|
|
|
import ModalBody from '../base/ModalBody';
|
|
|
|
import ModalHeader from '../base/ModalHeader';
|
|
|
|
import ModalFooter from '../base/ModalFooter';
|
|
|
|
import * as session from '../../../backend/sync/session';
|
2016-11-07 20:24:38 +00:00
|
|
|
import {showModal} from './index';
|
2016-10-21 17:20:36 +00:00
|
|
|
import SignupModal from './SignupModal';
|
|
|
|
import * as sync from '../../../backend/sync';
|
2016-10-29 05:40:09 +00:00
|
|
|
import {trackEvent} from '../../../backend/ganalytics';
|
2016-10-21 17:20:36 +00:00
|
|
|
|
|
|
|
class LoginModal extends Component {
|
|
|
|
constructor (props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
step: 1,
|
2016-10-21 18:58:06 +00:00
|
|
|
loading: false,
|
2016-11-07 20:24:38 +00:00
|
|
|
error: '',
|
|
|
|
title: '',
|
|
|
|
message: '',
|
2016-10-21 17:20:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async _handleLogin (e) {
|
|
|
|
e.preventDefault();
|
2016-10-21 18:58:06 +00:00
|
|
|
this.setState({error: '', loading: true});
|
2016-10-21 17:20:36 +00:00
|
|
|
|
|
|
|
const email = this._emailInput.value;
|
|
|
|
const password = this._passwordInput.value;
|
|
|
|
|
|
|
|
try {
|
|
|
|
await session.login(email, password);
|
2016-10-25 01:23:49 +00:00
|
|
|
|
|
|
|
// Clear all existing sync data that might be there and enable sync
|
|
|
|
await sync.resetLocalData();
|
2016-10-28 21:27:05 +00:00
|
|
|
sync.doInitialSync();
|
2016-10-25 01:23:49 +00:00
|
|
|
|
|
|
|
this.setState({step: 2, loading: false});
|
2016-10-21 17:20:36 +00:00
|
|
|
} catch (e) {
|
2016-10-29 05:40:09 +00:00
|
|
|
this.setState({error: e.message, loading: false});
|
2016-10-21 17:20:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_handleSignup (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
this.modal.hide();
|
2016-11-07 20:24:38 +00:00
|
|
|
showModal(SignupModal);
|
|
|
|
trackEvent('Auth', 'Switch', 'To Signup');
|
2016-10-21 17:20:36 +00:00
|
|
|
}
|
|
|
|
|
2016-11-07 20:24:38 +00:00
|
|
|
show ({title, message}) {
|
|
|
|
this.setState({step: 1, error: '', title, message});
|
2016-10-21 17:20:36 +00:00
|
|
|
this.modal.show();
|
|
|
|
setTimeout(() => this._emailInput.focus(), 100);
|
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
2016-11-07 20:24:38 +00:00
|
|
|
const {step, title, message} = this.state;
|
|
|
|
if (step === 1) {
|
2016-10-21 17:20:36 +00:00
|
|
|
return (
|
|
|
|
<Modal ref={m => this.modal = m} {...this.props}>
|
|
|
|
<form onSubmit={this._handleLogin.bind(this)}>
|
2016-11-07 20:24:38 +00:00
|
|
|
<ModalHeader>{title || "Login to Your Account"}</ModalHeader>
|
2016-10-21 17:20:36 +00:00
|
|
|
<ModalBody className="pad changelog">
|
2016-11-07 20:24:38 +00:00
|
|
|
{message ? (
|
|
|
|
<p className="notice info">{message}</p>
|
|
|
|
) : null}
|
2016-10-21 17:20:36 +00:00
|
|
|
<label htmlFor="login-email">Email</label>
|
|
|
|
<div className="form-control form-control--outlined">
|
|
|
|
<input type="email"
|
|
|
|
required="required"
|
|
|
|
id="login-email"
|
|
|
|
name="login-email"
|
|
|
|
placeholder="me@mydomain.com"
|
|
|
|
ref={n => this._emailInput = n}/>
|
|
|
|
</div>
|
|
|
|
<label htmlFor="login-password">Password</label>
|
|
|
|
<div className="form-control form-control--outlined">
|
|
|
|
<input type="password"
|
|
|
|
required="required"
|
|
|
|
id="login-password"
|
|
|
|
name="login-password"
|
|
|
|
placeholder="•••••••••••••••••"
|
|
|
|
ref={n => this._passwordInput = n}/>
|
|
|
|
</div>
|
|
|
|
{this.state.error ? (
|
|
|
|
<div className="danger pad-top">** {this.state.error}</div>
|
|
|
|
) : null}
|
|
|
|
</ModalBody>
|
|
|
|
<ModalFooter>
|
|
|
|
<div className="margin-left">
|
|
|
|
Don't have an account yet?
|
|
|
|
{" "}
|
|
|
|
<a href="#" onClick={this._handleSignup.bind(this)}>Signup</a>
|
|
|
|
</div>
|
|
|
|
<button type="submit" className="btn">
|
2016-10-21 18:58:06 +00:00
|
|
|
{this.state.loading ? <i className="fa fa-spin fa-refresh margin-right-sm"></i> : null}
|
2016-10-21 17:20:36 +00:00
|
|
|
Login
|
|
|
|
</button>
|
|
|
|
</ModalFooter>
|
|
|
|
</form>
|
|
|
|
</Modal>
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
return (
|
|
|
|
<Modal ref={m => this.modal = m} {...this.props}>
|
|
|
|
<ModalHeader>Login Success</ModalHeader>
|
|
|
|
<ModalBody className="pad">
|
|
|
|
<h1>Enjoy your stay!</h1>
|
|
|
|
<p>
|
2016-10-21 18:58:06 +00:00
|
|
|
If you have any questions or concerns, send you email to
|
|
|
|
{" "}
|
|
|
|
<Link href="mailto:support@insomnia.rest">
|
|
|
|
support@insomnia.rest
|
|
|
|
</Link>
|
2016-10-21 17:20:36 +00:00
|
|
|
</p>
|
|
|
|
</ModalBody>
|
|
|
|
<ModalFooter>
|
|
|
|
<button type="submit"
|
|
|
|
className="btn"
|
|
|
|
onClick={e => this.modal.hide()}>
|
|
|
|
Close
|
|
|
|
</button>
|
|
|
|
</ModalFooter>
|
|
|
|
</Modal>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LoginModal.propTypes = {};
|
|
|
|
|
|
|
|
export default LoginModal;
|