receptor/src/ui/connection.js

116 lines
3.4 KiB
JavaScript
Raw Normal View History

import React, { Component } from 'react';
2017-09-08 04:06:49 +02:00
import { connect } from 'react-redux';
import {
2017-09-08 06:55:47 +02:00
Alert,
Card,
CardHeader,
CardBlock,
FormGroup,
2017-09-08 06:55:47 +02:00
Progress,
Label,
Input
} from 'reactstrap';
import { initialize } from '..';
2017-09-08 06:55:47 +02:00
import { SOCKET_STATE } from '../actions/socket';
2017-10-13 06:34:26 +02:00
const getURI = (uri, password) => ({ uri, password });
2017-09-10 12:45:45 +02:00
2017-09-08 04:06:49 +02:00
class ConnectionOverlay extends Component {
constructor() {
super();
2017-09-10 12:45:45 +02:00
this.connect = this.connect.bind(this);
2017-12-30 16:43:45 +01:00
this.componentDidMount = this.componentDidMount.bind(this);
2018-02-26 19:51:02 +01:00
this.state = {}
2017-12-30 16:43:45 +01:00
}
componentDidMount() {
const { socket } = this.props;
if (socket.state === SOCKET_STATE.DISCONNECTED) {
const uri = localStorage.getItem("autoconnect");
const password = localStorage.getItem("password");
this.state = {
uri: uri || "ws://127.0.0.1:8412",
password,
autoconnect: !!uri
};
if (uri) {
initialize(getURI(this.state.uri, this.state.password));
}
2017-09-08 07:07:31 +02:00
}
}
connect() {
if (this.state.autoconnect) {
localStorage.setItem("autoconnect", this.state.uri);
2017-09-10 12:45:45 +02:00
localStorage.setItem("password", this.state.password);
2017-09-08 07:07:31 +02:00
}
2017-09-10 12:45:45 +02:00
initialize(getURI(this.state.uri, this.state.password));
}
render() {
2017-09-08 04:06:49 +02:00
const { socket } = this.props;
2017-09-08 06:55:47 +02:00
if (socket.state === SOCKET_STATE.CONNECTED) {
2017-09-08 04:06:49 +02:00
return null;
}
2017-09-08 06:55:47 +02:00
if (socket.state === SOCKET_STATE.CONNECTING) {
return (
<div className="connection-overlay">
<Card>
<CardHeader>Connect to synapse</CardHeader>
<CardBlock>
<p className="text-center">Connecting...</p>
<Progress value={100} animated />
</CardBlock>
</Card>
</div>
);
}
2017-09-10 12:45:45 +02:00
const { uri, password, autoconnect } = this.state;
return (
<div className="connection-overlay">
<Card>
<CardHeader>Connect to synapse</CardHeader>
<CardBlock>
2017-09-08 06:55:47 +02:00
{socket.reason && <Alert color="info">{socket.reason}</Alert>}
<FormGroup>
<Label for="socket-uri">Server URI</Label>
<Input
id="socket-uri"
value={uri}
2017-09-10 12:45:45 +02:00
onKeyPress={e => e.keyCode === 13 && this.connect()}
onChange={e => this.setState({ uri: e.target.value })}
/>
</FormGroup>
2017-09-10 12:45:45 +02:00
<FormGroup>
<Label for="socket-password">Server Password</Label>
<Input
id="socket-password"
value={password}
type="password"
onKeyPress={e => e.keyCode === 13 && this.connect()}
onChange={e => this.setState({ password: e.target.value })}
/>
</FormGroup>
<FormGroup>
<Label for="autoconnect" check>
<Input
id="autoconnect"
type="checkbox"
checked={autoconnect}
onChange={e => this.setState({ autoconnect: !autoconnect })}
/> Autoconnect to this server
</Label>
</FormGroup>
<button
className="btn btn-primary"
2017-09-10 12:45:45 +02:00
onClick={this.connect}
2017-09-08 06:55:47 +02:00
>{socket.reason ? "Reconnect" : "Connect"}</button>
</CardBlock>
</Card>
</div>
);
}
}
2017-09-08 04:06:49 +02:00
export default connect(state => ({ socket: state.socket }))(ConnectionOverlay);