Files
ThreadNet-Web/src/components/views/login/PasswordLogin.js
T

180 lines
6.5 KiB
JavaScript
Raw Normal View History

/*
2016-01-07 04:06:39 +00:00
Copyright 2015, 2016 OpenMarket Ltd
2017-03-14 11:50:13 +00:00
Copyright 2017 Vector Creations Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
2016-08-03 15:59:17 +01:00
import React from 'react';
import ReactDOM from 'react-dom';
import classNames from 'classnames';
2017-03-14 11:50:13 +00:00
import sdk from '../../../index';
2016-08-03 15:59:17 +01:00
import {field_input_incorrect} from '../../../UiEffects';
/**
* A pure UI component which displays a username/password form.
*/
module.exports = React.createClass({displayName: 'PasswordLogin',
propTypes: {
2016-01-12 17:20:16 +00:00
onSubmit: React.PropTypes.func.isRequired, // fn(username, password)
onForgotPasswordClick: React.PropTypes.func, // fn()
initialUsername: React.PropTypes.string,
2017-03-14 11:50:13 +00:00
initialPhoneCountry: React.PropTypes.string,
initialPhoneNumber: React.PropTypes.string,
initialPassword: React.PropTypes.string,
onUsernameChanged: React.PropTypes.func,
2017-03-14 11:50:13 +00:00
onPhoneCountryChanged: React.PropTypes.func,
onPhoneNumberChanged: React.PropTypes.func,
onPasswordChanged: React.PropTypes.func,
2016-08-03 15:59:17 +01:00
loginIncorrect: React.PropTypes.bool,
},
getDefaultProps: function() {
return {
onUsernameChanged: function() {},
onPasswordChanged: function() {},
2017-03-14 11:50:13 +00:00
onPhoneCountryChanged: function() {},
onPhoneNumberChanged: function() {},
initialUsername: "",
2017-03-14 11:50:13 +00:00
initialPhoneCountry: "",
initialPhoneNumber: "",
initialPassword: "",
2016-08-03 15:59:17 +01:00
loginIncorrect: false,
};
},
getInitialState: function() {
return {
username: this.props.initialUsername,
password: this.props.initialPassword,
2017-03-14 11:50:13 +00:00
phoneCountry: this.props.initialPhoneCountry,
phoneNumber: this.props.initialPhoneNumber,
loginType: "mxid",
};
},
2016-08-03 15:59:17 +01:00
componentWillMount: function() {
this._passwordField = null;
},
componentWillReceiveProps: function(nextProps) {
if (!this.props.loginIncorrect && nextProps.loginIncorrect) {
field_input_incorrect(this._passwordField);
}
},
onSubmitForm: function(ev) {
ev.preventDefault();
2017-03-14 11:50:13 +00:00
this.props.onSubmit(
this.state.username,
this.state.phoneCountry,
this.state.phoneNumber,
this.state.password,
);
},
onUsernameChanged: function(ev) {
this.setState({username: ev.target.value});
this.props.onUsernameChanged(ev.target.value);
},
onLoginTypeChange: function(loginType) {
this.setState({loginType: loginType});
},
2017-03-14 11:50:13 +00:00
onPhoneCountryChanged: function(country) {
this.setState({phoneCountry: country});
this.props.onPhoneCountryChanged(country);
},
onPhoneNumberChanged: function(ev) {
this.setState({phoneNumber: ev.target.value});
this.props.onPhoneNumberChanged(ev.target.value);
},
onPasswordChanged: function(ev) {
this.setState({password: ev.target.value});
this.props.onPasswordChanged(ev.target.value);
},
render: function() {
2016-01-12 17:20:16 +00:00
var forgotPasswordJsx;
if (this.props.onForgotPasswordClick) {
forgotPasswordJsx = (
<a className="mx_Login_forgot" onClick={this.props.onForgotPasswordClick} href="#">
Forgot your password?
</a>
);
}
2016-08-03 15:59:17 +01:00
const pwFieldClass = classNames({
mx_Login_field: true,
error: this.props.loginIncorrect,
});
2017-03-14 11:50:13 +00:00
const CountryDropdown = sdk.getComponent('views.login.CountryDropdown');
const Dropdown = sdk.getComponent('elements.Dropdown');
const loginType = {
'email':
2017-03-14 11:50:13 +00:00
<input className="mx_Login_field mx_Login_username" type="text"
2016-10-14 15:34:44 +01:00
name="username" // make it a little easier for browser's remember-password
value={this.state.username} onChange={this.onUsernameChanged}
placeholder="Email or user name" autoFocus />,
'mxid':
<input className="mx_Login_field mx_Login_username" type="text"
name="username" // make it a little easier for browser's remember-password
value={this.state.username} onChange={this.onUsernameChanged}
placeholder="Email or user name" autoFocus />,
'phone': <div className="mx_Login_phoneSection">
<CountryDropdown ref="phone_country" onOptionChange={this.onPhoneCountryChanged}
className="mx_Login_phoneCountry"
value={this.state.phoneCountry}
/>
<input type="text" ref="phoneNumber"
onChange={this.onPhoneNumberChanged}
placeholder="Mobile phone number"
className="mx_Login_phoneNumberField mx_Login_field"
value={this.state.phoneNumber}
name="phoneNumber"
/>
</div>
}[this.state.loginType];
return (
<div>
<form onSubmit={this.onSubmitForm}>
<div className="mx_Login_type_container">
<label className="mx_Login_type_label">I want to sign in with my</label>
<Dropdown className="mx_Login_type_dropdown" value={this.state.loginType} onOptionChange={this.onLoginTypeChange}>
<span key="mxid">Matrix ID</span>
<span key="email">Email</span>
<span key="phone">Phone</span>
</Dropdown>
2017-03-14 11:50:13 +00:00
</div>
{loginType}
2016-08-03 15:59:17 +01:00
<input className={pwFieldClass} ref={(e) => {this._passwordField = e;}} type="password"
2016-10-14 15:34:44 +01:00
name="password"
value={this.state.password} onChange={this.onPasswordChanged}
placeholder="Password" />
<br />
2016-01-12 17:20:16 +00:00
{forgotPasswordJsx}
2016-09-29 17:38:52 +01:00
<input className="mx_Login_submit" type="submit" value="Sign in" />
</form>
</div>
);
}
2016-08-03 15:59:17 +01:00
});