gamja-old/components/connect-form.js

231 lines
4.4 KiB
JavaScript
Raw Normal View History

2021-07-04 15:28:45 -04:00
import { html, Component, createRef } from "../lib/index.js";
import linkify from "../lib/linkify.js";
export default class ConnectForm extends Component {
state = {
2021-05-31 12:04:02 -04:00
url: "",
pass: "",
nick: "",
password: "",
rememberMe: false,
username: "",
realname: "",
autojoin: true,
};
2021-07-04 15:28:45 -04:00
nickInput = createRef();
constructor(props) {
super(props);
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
if (props.params) {
this.state = {
...this.state,
2021-05-31 12:04:02 -04:00
url: props.params.url || "",
nick: props.params.nick || "",
rememberMe: props.params.autoconnect || false,
username: props.params.username || "",
realname: props.params.realname || "",
};
}
}
handleChange(event) {
2021-06-10 12:11:11 -04:00
let target = event.target;
let value = target.type == "checkbox" ? target.checked : target.value;
this.setState({ [target.name]: value });
}
handleSubmit(event) {
event.preventDefault();
if (this.props.connecting) {
return;
}
2021-06-10 12:11:11 -04:00
let params = {
2021-05-31 12:04:02 -04:00
url: this.state.url,
pass: this.state.pass,
nick: this.state.nick,
autoconnect: this.state.rememberMe,
username: this.state.username,
realname: this.state.realname,
saslPlain: null,
autojoin: [],
};
if (this.state.password) {
params.saslPlain = {
2021-05-31 12:04:02 -04:00
username: params.username || params.nick,
password: this.state.password,
};
} else if (this.props.auth === "external") {
params.saslExternal = true;
}
if (this.state.autojoin) {
params.autojoin = this.props.params.autojoin || [];
}
this.props.onSubmit(params);
}
2021-07-04 15:28:45 -04:00
componentDidMount() {
if (this.nickInput.current) {
this.nickInput.current.focus();
}
}
render() {
2021-06-10 12:11:11 -04:00
let disabled = this.props.connecting;
2021-06-10 12:11:11 -04:00
let serverURL = null;
if (!this.props.params || !this.props.params.url) {
serverURL = html`
<label>
Server URL:<br/>
2021-07-04 15:51:42 -04:00
<input
type="text"
name="url"
value=${this.state.url}
disabled=${disabled}
inputmode="url"
/>
</label>
<br/><br/>
`;
}
2021-06-10 12:11:11 -04:00
let status = null;
if (this.props.connecting) {
status = html`
<p>Connecting...</p>
`;
} else if (this.props.error) {
status = html`
<p class="error-text">${linkify(this.props.error)}</p>
`;
}
2021-06-10 12:11:11 -04:00
let auth = null;
if (this.props.auth !== "disabled" && this.props.auth !== "external") {
auth = html`
<label>
Password:<br/>
<input
type="password"
name="password"
value=${this.state.password}
disabled=${disabled}
required=${this.props.auth === "mandatory"}
placeholder=${this.props.auth !== "mandatory" ? "(optional)" : ""}
/>
</label>
<br/><br/>
`;
}
let autojoin = null;
let channels = this.props.params.autojoin || [];
if (channels.length > 0) {
let s = channels.length > 1 ? "s" : "";
autojoin = html`
<label>
<input
type="checkbox"
name="autojoin"
checked=${this.state.autojoin}
/>
Auto-join channel${s} <strong>${channels.join(', ')}</strong>
</label>
<br/><br/>
`;
}
return html`
<form onChange=${this.handleChange} onSubmit=${this.handleSubmit}>
<h2>Connect to IRC</h2>
<label>
Nickname:<br/>
2021-07-04 15:51:42 -04:00
<input
type="username"
name="nick"
value=${this.state.nick}
disabled=${disabled}
ref=${this.nickInput}
required
/>
</label>
<br/><br/>
${auth}
${autojoin}
<label>
2021-07-04 15:51:42 -04:00
<input
type="checkbox"
name="rememberMe"
checked=${this.state.rememberMe}
disabled=${disabled}
/>
Remember me
</label>
<br/><br/>
<details>
<summary role="button">Advanced options</summary>
<br/>
${serverURL}
<label>
Username:<br/>
2021-07-04 15:51:42 -04:00
<input
type="username"
name="username"
value=${this.state.username}
disabled=${disabled}
placeholder="Same as nickname"
/>
</label>
<br/><br/>
<label>
Real name:<br/>
2021-07-04 15:51:42 -04:00
<input
type="text"
name="realname"
value=${this.state.realname}
disabled=${disabled}
placeholder="Same as nickname"
/>
</label>
<br/><br/>
<label>
Server password:<br/>
2021-07-04 15:51:42 -04:00
<input
type="text"
name="pass"
value=${this.state.pass}
disabled=${disabled}
placeholder="None"
/>
</label>
<br/><br/>
</details>
<br/>
<button disabled=${disabled}>Connect</button>
${status}
</form>
`;
}
}