making host selectable from the login screen

This commit is contained in:
Gene S
2017-04-20 17:50:23 -07:00
parent 0a3dfa8d9a
commit 9fc05d97d9
+69 -13
View File
@@ -26,9 +26,10 @@
// Name of this application, used in the User-Agent // Name of this application, used in the User-Agent
var APP_NAME = "TinodeReact/0.10"; var APP_NAME = "TinodeReact/0.10";
// Host name and port to connect to var KNOWN_HOSTS = {hosted: "api.tinode.co", local: "localhost:6060"};
var HOST = "api.tinode.co"; // Default host name and port to connect to.
// var HOST = "localhost:6060"; var DEFAULT_HOST = KNOWN_HOSTS.hosted;
// var DEFAULT_HOST = KNOWN_HOSTS.local;
// API key. Use https://github.com/tinode/chat/tree/master/keygen to generate your own // API key. Use https://github.com/tinode/chat/tree/master/keygen to generate your own
var API_KEY = "AQEAAAABAAD_rAp4DJh05a1HAwFT3A6K"; var API_KEY = "AQEAAAABAAD_rAp4DJh05a1HAwFT3A6K";
@@ -69,6 +70,17 @@
'data:image/' + photo.type + ';base64,' + photo.data : null; 'data:image/' + photo.type + ';base64,' + photo.data : null;
} }
// Detect server address from the URL
function detectServerAddress() {
var host = DEFAULT_HOST;
if (window.location.protocol === 'file:' || window.location.hostname === 'localhost') {
host = KNOWN_HOSTS.local;
} else if (window.location.hostname) {
host = window.location.hostname + (window.location.port ? ':' + window.location.port : '');
}
return host;
}
/* BEGIN Popup/dropdown menu */ /* BEGIN Popup/dropdown menu */
var ContextMenu = React.createClass({ var ContextMenu = React.createClass({
componentWillUnmount: function() { componentWillUnmount: function() {
@@ -189,12 +201,45 @@
}; };
/* END Popup/dropdown menu */ /* END Popup/dropdown menu */
/* BEGIN Combobox for selecting host name */
var HostSelector = React.createClass({
getInitialState: function() {
return {
hostName: this.props.serverAddress
};
},
handleHostNameChange: function(e) {
this.setState({hostName: e.target.value});
this.props.onServerAddressChange(e.target.value.trim());
},
render: function() {
var hostOptions = [];
for (var key in KNOWN_HOSTS) {
var item = KNOWN_HOSTS[key];
hostOptions.push(
<option key={item} value={item} />
);
}
return (
<div>
<label htmlFor="host-name">Server address:</label>
<input type="search" id="host-name" placeholder={this.props.hostName} list="known-hosts"
value={this.state.hostName} onChange={this.handleHostNameChange} />
<datalist id="known-hosts">
{hostOptions}
</datalist>
</div>);
}
});
/* END Combobox for selecting host name */
/* BEGIN Login: a login form */ /* BEGIN Login: a login form */
var LoginView = React.createClass({ var LoginView = React.createClass({
getInitialState: function() { getInitialState: function() {
return { return {
login: this.props.login, login: this.props.login,
password: '', password: '',
hostName: this.props.serverAddress,
errorCleared: false errorCleared: false
}; };
}, },
@@ -212,9 +257,14 @@
} }
this.setState({password: e.target.value}); this.setState({password: e.target.value});
}, },
handleServerAddressChange: function(name) {
this.setState({hostName: name});
},
handleSubmit: function(e) { handleSubmit: function(e) {
e.preventDefault(); e.preventDefault();
this.setState({errorCleared: false}); this.setState({errorCleared: false});
console.log("LoginView handleSubmit '" + this.state.hostName + "'");
this.props.onServerAddressChange(this.state.hostName);
this.props.onLogin(this.state.login.trim(), this.state.password.trim()); this.props.onLogin(this.state.login.trim(), this.state.password.trim());
}, },
render: function() { render: function() {
@@ -235,6 +285,8 @@
value={this.state.password} value={this.state.password}
onChange={this.handlePasswordChange} onChange={this.handlePasswordChange}
required /> required />
<HostSelector serverAddress={this.props.serverAddress}
onServerAddressChange={this.handleServerAddressChange} />
<div className="dialog-buttons"> <div className="dialog-buttons">
<button className={submitClasses} type="submit">Sign in</button> <button className={submitClasses} type="submit">Sign in</button>
</div> </div>
@@ -242,7 +294,7 @@
</div> </div>
); );
} }
}) });
/* END Login */ /* END Login */
/* BEGIN Account registration */ /* BEGIN Account registration */
@@ -388,8 +440,8 @@
e.preventDefault(); e.preventDefault();
this.props.onUpdate(this.state.hostName, this.state.transport); this.props.onUpdate(this.state.hostName, this.state.transport);
}, },
handleHostNameChange: function(e) { handleHostNameChange: function(name) {
this.setState({hostName: e.target.value}); this.setState({hostName: name});
}, },
handleTransportSelected: function(e) { handleTransportSelected: function(e) {
this.setState({transport: e.currentTarget.value}); this.setState({transport: e.currentTarget.value});
@@ -413,9 +465,9 @@
return ( return (
<form id="settings-form" onSubmit={this.handleSubmit}> <form id="settings-form" onSubmit={this.handleSubmit}>
<p>This form does not do anything yet</p> <p>This form does not do anything yet</p>
<label htmlFor="host-name">Host:</label> <HostSelector
<input type="text" id="host-name" placeholder="api.tinode.co" serverAddress={this.props.serverAddress}
value={this.state.hostName} onChange={this.handleHostNameChange} required autoFocus /> onHostNameChange={this.handleHostNameChange} />
<p>Wire transport:</p> <p>Wire transport:</p>
<ul> <ul>
{transportOptions} {transportOptions}
@@ -495,7 +547,7 @@
var SidepanelView = React.createClass({ var SidepanelView = React.createClass({
getInitialState: function() { getInitialState: function() {
return { return {
showErrorText: true, showErrorText: true
}; };
}, },
handleClearErrorText: function() { handleClearErrorText: function() {
@@ -542,7 +594,9 @@
{this.props.state === 'login' ? {this.props.state === 'login' ?
<LoginView login={this.props.login} <LoginView login={this.props.login}
disabled={this.props.loginDisabled} disabled={this.props.loginDisabled}
serverAddress={this.props.serverAddress}
onLogin={this.handleLoginRequested} onLogin={this.handleLoginRequested}
onServerAddressChange={this.props.onConnectionSettings}
onClearError={this.handleClearErrorText} /> : onClearError={this.handleClearErrorText} /> :
this.props.state === 'register' ? this.props.state === 'register' ?
@@ -1325,7 +1379,7 @@
getInitialState: function() { getInitialState: function() {
return { return {
transport: null, transport: null,
serverAddress: HOST, serverAddress: detectServerAddress(),
sidePanelSelected: 'login', sidePanelSelected: 'login',
sidePanelTitle: null, sidePanelTitle: null,
sidePanelAvatar: null, sidePanelAvatar: null,
@@ -1370,7 +1424,7 @@
if (Tinode.isConnected()) { if (Tinode.isConnected()) {
this.doLogin(login, password); this.doLogin(login, password);
} else { } else {
Tinode.connect().catch(function(err) { Tinode.connect(this.state.serverAddress).catch(function(err) {
// Socket error // Socket error
instance.setState({loginDisabled: false, errorText: err.message}); instance.setState({loginDisabled: false, errorText: err.message});
}); });
@@ -1471,7 +1525,7 @@
// Actual registration of a new account. // Actual registration of a new account.
handleNewAccountRequest: function(login_, password_, public_, private_) { handleNewAccountRequest: function(login_, password_, public_, private_) {
var instance = this; var instance = this;
Tinode.connect().then(function() { Tinode.connect(this.state.serverAddress).then(function() {
return Tinode.createUserBasic(login_, password_, return Tinode.createUserBasic(login_, password_,
{login: "basic", public: public_, private: private_}); {login: "basic", public: public_, private: private_});
}).catch(function(err) { }).catch(function(err) {
@@ -1484,6 +1538,8 @@
}, },
// User updated connection parameters - transport and server address // User updated connection parameters - transport and server address
handleConnectionSettings: function(serverAddress, transport) { handleConnectionSettings: function(serverAddress, transport) {
transport = transport || this.state.transport;
serverAddress = serverAddress || this.state.serverAddress;
this.setState({serverAddress: serverAddress, transport: transport, sidePanelSelected: 'login'}); this.setState({serverAddress: serverAddress, transport: transport, sidePanelSelected: 'login'});
this.tnSetup(serverAddress, transport); this.tnSetup(serverAddress, transport);
}, },