2017-02-28 21:32:23 +00:00
|
|
|
import React, {PureComponent} from 'react';
|
2017-03-03 01:44:07 +00:00
|
|
|
import autobind from 'autobind-decorator';
|
2017-03-08 05:52:17 +00:00
|
|
|
import Link from '../base/link';
|
|
|
|
import Modal from '../base/modal';
|
|
|
|
import ModalBody from '../base/modal-body';
|
|
|
|
import ModalHeader from '../base/modal-header';
|
|
|
|
import ModalFooter from '../base/modal-footer';
|
2016-11-09 03:18:25 +00:00
|
|
|
import * as session from '../../../sync/session';
|
|
|
|
import * as sync from '../../../sync';
|
2016-10-21 17:20:36 +00:00
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
@autobind
|
2017-02-28 21:32:23 +00:00
|
|
|
class LoginModal extends PureComponent {
|
2017-03-03 01:44:07 +00:00
|
|
|
constructor (props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
step: 1,
|
|
|
|
loading: false,
|
|
|
|
error: '',
|
|
|
|
title: '',
|
2017-03-03 20:09:08 +00:00
|
|
|
message: ''
|
2017-03-03 01:44:07 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
_setModalRef (n) {
|
|
|
|
this.modal = n;
|
|
|
|
}
|
|
|
|
|
|
|
|
_setPasswordInputRef (n) {
|
|
|
|
this._passwordInput = n;
|
|
|
|
}
|
2016-10-21 17:20:36 +00:00
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
_setEmailInputRef (n) {
|
2017-03-03 20:09:08 +00:00
|
|
|
this._emailInput = n;
|
|
|
|
}
|
2017-01-11 03:18:15 +00:00
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
async _handleLogin (e) {
|
2016-10-21 17:20:36 +00:00
|
|
|
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
|
2016-11-07 23:36:40 +00:00
|
|
|
process.nextTick(async () => {
|
|
|
|
await sync.resetLocalData();
|
|
|
|
await 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
|
|
|
}
|
2017-03-03 20:09:08 +00:00
|
|
|
}
|
2016-10-21 17:20:36 +00:00
|
|
|
|
2016-11-16 17:18:39 +00:00
|
|
|
show (options = {}) {
|
|
|
|
const {title, message} = options;
|
2016-11-18 00:11:08 +00:00
|
|
|
this.setState({step: 1, error: '', loading: false, title, message});
|
2016-10-21 17:20:36 +00:00
|
|
|
this.modal.show();
|
|
|
|
setTimeout(() => this._emailInput.focus(), 100);
|
|
|
|
}
|
|
|
|
|
2017-01-11 03:18:15 +00:00
|
|
|
hide () {
|
|
|
|
this.modal.hide();
|
|
|
|
}
|
|
|
|
|
2016-10-21 17:20:36 +00:00
|
|
|
render () {
|
2017-01-11 03:18:15 +00:00
|
|
|
const {step, title, message, loading, error} = this.state;
|
2016-11-18 00:05:00 +00:00
|
|
|
let inner;
|
2016-11-07 20:24:38 +00:00
|
|
|
if (step === 1) {
|
2016-11-18 00:05:00 +00:00
|
|
|
inner = [
|
2017-03-03 20:09:08 +00:00
|
|
|
<ModalHeader key="header">{title || 'Login to Your Account'}</ModalHeader>,
|
2016-11-18 00:05:00 +00:00
|
|
|
<ModalBody key="body" className="pad">
|
2017-03-03 01:44:07 +00:00
|
|
|
{message ? <p className="notice info">{message}</p> : null}
|
2016-12-01 03:54:26 +00:00
|
|
|
<div className="form-control form-control--outlined no-pad-top">
|
2016-11-30 03:55:27 +00:00
|
|
|
<label>Email
|
|
|
|
<input
|
|
|
|
type="email"
|
|
|
|
required="required"
|
|
|
|
placeholder="me@mydomain.com"
|
2017-02-28 21:32:23 +00:00
|
|
|
ref={this._setEmailInputRef}
|
2016-11-30 03:55:27 +00:00
|
|
|
/>
|
|
|
|
</label>
|
2016-11-18 00:05:00 +00:00
|
|
|
</div>
|
|
|
|
<div className="form-control form-control--outlined">
|
2016-11-30 03:55:27 +00:00
|
|
|
<label>Password
|
|
|
|
<input type="password"
|
|
|
|
required="required"
|
|
|
|
placeholder="•••••••••••••••••"
|
2017-03-03 01:44:07 +00:00
|
|
|
ref={this._setPasswordInputRef}/>
|
2016-11-30 03:55:27 +00:00
|
|
|
</label>
|
2016-11-18 00:05:00 +00:00
|
|
|
</div>
|
2017-01-11 03:18:15 +00:00
|
|
|
{error ? <div className="danger pad-top">** {error}</div> : null}
|
2016-11-18 00:05:00 +00:00
|
|
|
</ModalBody>,
|
|
|
|
<ModalFooter key="footer">
|
|
|
|
<div className="margin-left">
|
|
|
|
Don't have an account yet?
|
2017-03-03 20:09:08 +00:00
|
|
|
{' '}
|
2016-12-21 23:37:48 +00:00
|
|
|
<Link href="https://insomnia.rest/app/">Signup</Link>
|
2016-11-18 00:05:00 +00:00
|
|
|
</div>
|
|
|
|
<button type="submit" className="btn">
|
2017-01-11 03:18:15 +00:00
|
|
|
{loading ? <i className="fa fa-spin fa-refresh margin-right-sm"></i> : null}
|
2016-11-18 00:05:00 +00:00
|
|
|
Login
|
|
|
|
</button>
|
|
|
|
</ModalFooter>
|
2017-03-03 20:09:08 +00:00
|
|
|
];
|
2016-10-21 17:20:36 +00:00
|
|
|
} else {
|
2016-11-18 00:05:00 +00:00
|
|
|
inner = [
|
|
|
|
<ModalHeader key="header">Login Success</ModalHeader>,
|
2016-12-01 03:54:26 +00:00
|
|
|
<ModalBody key="body" className="pad no-pad-top">
|
2016-11-18 00:05:00 +00:00
|
|
|
<h1>Enjoy your stay!</h1>
|
|
|
|
<p>
|
|
|
|
If you have any questions or concerns, send you email to
|
2017-03-03 20:09:08 +00:00
|
|
|
{' '}
|
2016-11-23 19:33:24 +00:00
|
|
|
<Link href="https://insomnia.rest/documentation/support-and-feedback/">
|
2016-11-18 00:05:00 +00:00
|
|
|
support@insomnia.rest
|
|
|
|
</Link>
|
|
|
|
</p>
|
|
|
|
</ModalBody>,
|
|
|
|
<ModalFooter key="footer">
|
2017-03-03 01:44:07 +00:00
|
|
|
<button type="button" className="btn" onClick={this.hide}>
|
2016-11-18 00:05:00 +00:00
|
|
|
Close
|
|
|
|
</button>
|
|
|
|
</ModalFooter>
|
2017-03-03 20:09:08 +00:00
|
|
|
];
|
2016-11-18 00:05:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2016-12-01 03:54:26 +00:00
|
|
|
<form onSubmit={this._handleLogin}>
|
2017-01-11 03:18:15 +00:00
|
|
|
<Modal ref={this._setModalRef} {...this.props}>
|
2016-11-18 00:05:00 +00:00
|
|
|
{inner}
|
2016-10-21 17:20:36 +00:00
|
|
|
</Modal>
|
2016-11-18 00:05:00 +00:00
|
|
|
</form>
|
2017-03-03 20:09:08 +00:00
|
|
|
);
|
2016-10-21 17:20:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LoginModal.propTypes = {};
|
|
|
|
|
|
|
|
export default LoginModal;
|