generate local errors for blank fields during login

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
This commit is contained in:
Michael Telatynski 2018-06-23 17:12:00 +01:00
parent 5b027c6410
commit 15e75ca7b3
No known key found for this signature in database
GPG key ID: 3F879DA5AD802A5E
2 changed files with 49 additions and 10 deletions

View file

@ -93,6 +93,13 @@ module.exports = React.createClass({
this._unmounted = true; this._unmounted = true;
}, },
onPasswordLoginError: function(errorText) {
this.setState({
errorText,
loginIncorrect: Boolean(errorText),
});
},
onPasswordLogin: function(username, phoneCountry, phoneNumber, password) { onPasswordLogin: function(username, phoneCountry, phoneNumber, password) {
this.setState({ this.setState({
busy: true, busy: true,
@ -357,6 +364,7 @@ module.exports = React.createClass({
return ( return (
<PasswordLogin <PasswordLogin
onSubmit={this.onPasswordLogin} onSubmit={this.onPasswordLogin}
onError={this.onPasswordLoginError}
initialUsername={this.state.username} initialUsername={this.state.username}
initialPhoneCountry={this.state.phoneCountry} initialPhoneCountry={this.state.phoneCountry}
initialPhoneNumber={this.state.phoneNumber} initialPhoneNumber={this.state.phoneNumber}

View file

@ -28,6 +28,7 @@ import SdkConfig from '../../../SdkConfig';
*/ */
class PasswordLogin extends React.Component { class PasswordLogin extends React.Component {
static defaultProps = { static defaultProps = {
onError: function() {},
onUsernameChanged: function() {}, onUsernameChanged: function() {},
onPasswordChanged: function() {}, onPasswordChanged: function() {},
onPhoneCountryChanged: function() {}, onPhoneCountryChanged: function() {},
@ -70,19 +71,48 @@ class PasswordLogin extends React.Component {
onSubmitForm(ev) { onSubmitForm(ev) {
ev.preventDefault(); ev.preventDefault();
if (this.state.loginType === PasswordLogin.LOGIN_FIELD_PHONE) {
this.props.onSubmit( let username = ''; // XXX: Synapse breaks if you send null here:
'', // XXX: Synapse breaks if you send null here: let phoneCountry = null;
this.state.phoneCountry, let phoneNumber = null;
this.state.phoneNumber, let error;
this.state.password,
); switch (this.state.loginType) {
case PasswordLogin.LOGIN_FIELD_EMAIL:
username = this.state.username;
if (!username) {
error = _t('The email field must not be blank.');
}
break;
case PasswordLogin.LOGIN_FIELD_MXID:
username = this.state.username;
if (!username) {
error = _t('The user name field must not be blank.');
}
break;
case PasswordLogin.LOGIN_FIELD_PHONE:
phoneCountry = this.state.phoneCountry;
phoneNumber = this.state.phoneNumber;
if (!phoneNumber) {
error = _t('The phone number field must not be blank.');
}
break;
}
if (error) {
this.props.onError(error);
return; return;
} }
if (!this.state.password) {
this.props.onError(_t('The password field must not be blank.'));
return;
}
this.props.onSubmit( this.props.onSubmit(
this.state.username, username,
null, phoneCountry,
null, phoneNumber,
this.state.password, this.state.password,
); );
} }
@ -258,6 +288,7 @@ PasswordLogin.LOGIN_FIELD_PHONE = "login_field_phone";
PasswordLogin.propTypes = { PasswordLogin.propTypes = {
onSubmit: PropTypes.func.isRequired, // fn(username, password) onSubmit: PropTypes.func.isRequired, // fn(username, password)
onError: PropTypes.func,
onForgotPasswordClick: PropTypes.func, // fn() onForgotPasswordClick: PropTypes.func, // fn()
initialUsername: PropTypes.string, initialUsername: PropTypes.string,
initialPhoneCountry: PropTypes.string, initialPhoneCountry: PropTypes.string,