Set default connect params in App constructor

Fixes default params not populated in Connect component.
This commit is contained in:
Simon Ser 2020-06-24 18:14:46 +02:00
parent 34baafc77f
commit 9692f97c11
No known key found for this signature in database
GPG key ID: 0FDE7BE0E88F5E48
2 changed files with 46 additions and 40 deletions

View file

@ -30,6 +30,7 @@ export default class App extends Component {
realname: null,
nick: null,
saslPlain: null,
autoconnect: false,
autojoin: [],
},
status: Status.DISCONNECTED,
@ -44,6 +45,32 @@ export default class App extends Component {
this.handleConnectSubmit = this.handleConnectSubmit.bind(this);
this.handleBufferListClick = this.handleBufferListClick.bind(this);
this.handleComposerSubmit = this.handleComposerSubmit.bind(this);
if (window.localStorage && localStorage.getItem("autoconnect")) {
var connectParams = JSON.parse(localStorage.getItem("autoconnect"));
this.state.connectParams = {
...this.state.connectParams,
...connectParams,
autoconnect: true,
};
} else {
var params = parseQueryString();
if (params.server) {
this.state.connectParams.serverURL = params.server;
} else {
var host = window.location.host || "localhost:8080";
var proto = "wss:";
if (window.location.protocol != "https:") {
proto = "ws:";
}
this.state.connectParams.serverURL = proto + "//" + host + "/socket";
}
if (params.channels) {
this.state.connectParams.autojoin = params.channels.split(",");
}
}
}
setBufferState(name, updater, callback) {
@ -262,8 +289,8 @@ export default class App extends Component {
}
handleConnectSubmit(connectParams) {
if (localStorage) {
if (connectParams.rememberMe) {
if (window.localStorage) {
if (connectParams.autoconnect) {
localStorage.setItem("autoconnect", JSON.stringify(connectParams));
} else {
localStorage.removeItem("autoconnect");
@ -284,7 +311,7 @@ export default class App extends Component {
var args = parts.slice(1);
switch (cmd) {
case "quit":
if (localStorage) {
if (window.localStorage) {
localStorage.removeItem("autoconnect");
}
this.client.close();
@ -364,36 +391,8 @@ export default class App extends Component {
}
componentDidMount() {
if (localStorage && localStorage.getItem("autoconnect")) {
var connectParams = JSON.parse(localStorage.getItem("autoconnect"));
this.connect(connectParams);
} else {
var params = parseQueryString();
var serverURL = params.server;
if (!serverURL) {
var host = window.location.host || "localhost:8080";
var proto = "wss:";
if (window.location.protocol != "https:") {
proto = "ws:";
}
serverURL = proto + "//" + host + "/socket";
}
var autojoin = [];
if (params.channels) {
autojoin = params.channels.split(",");
}
this.setState((state) => {
return {
connectParams: {
...state.connectParams,
serverURL,
autojoin,
},
};
});
if (this.state.connectParams.autoconnect) {
this.connect(this.state.connectParams);
}
}

View file

@ -23,7 +23,7 @@ export default class Connect extends Component {
...this.state,
serverURL: props.params.serverURL || "",
nick: props.params.nick || "",
rememberMe: props.params.rememberMe || false,
rememberMe: props.params.autoconnect || false,
username: props.params.username || "",
realname: props.params.realname || "",
autojoin: (props.params.autojoin || []).join(","),
@ -48,7 +48,7 @@ export default class Connect extends Component {
serverURL: this.state.serverURL,
serverPass: this.state.serverPass,
nick: this.state.nick,
rememberMe: this.state.rememberMe,
autoconnect: this.state.rememberMe,
username: this.state.username || this.state.nick,
realname: this.state.realname || this.state.nick,
saslPlain: null,
@ -74,6 +74,17 @@ export default class Connect extends Component {
}
render() {
var rememberMe = null;
if (window.localStorage) {
rememberMe = html`
<label>
<input type="checkbox" name="rememberMe" checked=${this.state.rememberMe} disabled=${this.props.disabled}/>
Remember me
</label>
<br/><br/>
`;
}
return html`
<form onChange=${this.handleChange} onSubmit=${this.handleSubmit}>
<h2>Connect to IRC</h2>
@ -90,11 +101,7 @@ export default class Connect extends Component {
</label>
<br/><br/>
<label>
<input type="checkbox" name="rememberMe" checked=${this.state.rememberMe} disabled=${this.props.disabled}/>
Remember me
</label>
<br/><br/>
${rememberMe}
<details>
<summary>Advanced options</summary>