lemmy-ui/src/shared/components/home/setup.tsx

202 lines
5.8 KiB
TypeScript
Raw Normal View History

2021-02-22 02:39:04 +00:00
import { Component, linkEvent } from "inferno";
import { Helmet } from "inferno-helmet";
import { LoginResponse, Register, UserOperation } from "lemmy-js-client";
2021-02-22 02:39:04 +00:00
import { Subscription } from "rxjs";
import { delay, retryWhen, take } from "rxjs/operators";
import { i18n } from "../../i18next";
import { UserService, WebSocketService } from "../../services";
import { toast, wsClient, wsJsonToRes, wsUserOp } from "../../utils";
import { Spinner } from "../common/icon";
2021-02-22 02:39:04 +00:00
import { SiteForm } from "./site-form";
interface State {
2020-12-24 01:58:27 +00:00
userForm: Register;
doneRegisteringUser: boolean;
userLoading: boolean;
}
export class Setup extends Component<any, State> {
private subscription: Subscription;
private emptyState: State = {
userForm: {
username: undefined,
password: undefined,
password_verify: undefined,
show_nsfw: true,
// The first admin signup doesn't need a captcha
2021-02-22 02:39:04 +00:00
captcha_uuid: "",
captcha_answer: "",
},
doneRegisteringUser: false,
userLoading: false,
};
constructor(props: any, context: any) {
super(props, context);
this.state = this.emptyState;
this.subscription = WebSocketService.Instance.subject
.pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
.subscribe(
msg => this.parseMessage(msg),
err => console.error(err),
2021-02-22 02:39:04 +00:00
() => console.log("complete")
);
}
componentWillUnmount() {
this.subscription.unsubscribe();
}
get documentTitle(): string {
2021-02-22 02:39:04 +00:00
return `${i18n.t("setup")} - Lemmy`;
}
render() {
return (
<div class="container">
<Helmet title={this.documentTitle} />
<div class="row">
<div class="col-12 offset-lg-3 col-lg-6">
2021-02-22 02:39:04 +00:00
<h3>{i18n.t("lemmy_instance_setup")}</h3>
{!this.state.doneRegisteringUser ? (
this.registerUser()
) : (
<SiteForm />
)}
</div>
</div>
</div>
);
}
registerUser() {
return (
<form onSubmit={linkEvent(this, this.handleRegisterSubmit)}>
2021-02-22 02:39:04 +00:00
<h5>{i18n.t("setup_admin")}</h5>
<div class="form-group row">
<label class="col-sm-2 col-form-label" htmlFor="username">
2021-02-22 02:39:04 +00:00
{i18n.t("username")}
</label>
<div class="col-sm-10">
<input
type="text"
class="form-control"
id="username"
value={this.state.userForm.username}
onInput={linkEvent(this, this.handleRegisterUsernameChange)}
required
minLength={3}
pattern="[a-zA-Z0-9_]+"
/>
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label" htmlFor="email">
2021-02-22 02:39:04 +00:00
{i18n.t("email")}
</label>
<div class="col-sm-10">
<input
type="email"
id="email"
class="form-control"
2021-02-22 02:39:04 +00:00
placeholder={i18n.t("optional")}
value={this.state.userForm.email}
onInput={linkEvent(this, this.handleRegisterEmailChange)}
minLength={3}
/>
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label" htmlFor="password">
2021-02-22 02:39:04 +00:00
{i18n.t("password")}
</label>
<div class="col-sm-10">
<input
type="password"
id="password"
value={this.state.userForm.password}
onInput={linkEvent(this, this.handleRegisterPasswordChange)}
class="form-control"
required
maxLength={60}
/>
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label" htmlFor="verify-password">
2021-02-22 02:39:04 +00:00
{i18n.t("verify_password")}
</label>
<div class="col-sm-10">
<input
type="password"
id="verify-password"
value={this.state.userForm.password_verify}
onInput={linkEvent(this, this.handleRegisterPasswordVerifyChange)}
class="form-control"
required
maxLength={60}
/>
</div>
</div>
<div class="form-group row">
<div class="col-sm-10">
<button type="submit" class="btn btn-secondary">
2021-02-22 02:39:04 +00:00
{this.state.userLoading ? <Spinner /> : i18n.t("sign_up")}
</button>
</div>
</div>
</form>
);
}
handleRegisterSubmit(i: Setup, event: any) {
event.preventDefault();
i.state.userLoading = true;
i.setState(i.state);
event.preventDefault();
WebSocketService.Instance.send(wsClient.register(i.state.userForm));
}
handleRegisterUsernameChange(i: Setup, event: any) {
i.state.userForm.username = event.target.value;
i.setState(i.state);
}
handleRegisterEmailChange(i: Setup, event: any) {
i.state.userForm.email = event.target.value;
i.setState(i.state);
}
handleRegisterPasswordChange(i: Setup, event: any) {
i.state.userForm.password = event.target.value;
i.setState(i.state);
}
handleRegisterPasswordVerifyChange(i: Setup, event: any) {
i.state.userForm.password_verify = event.target.value;
i.setState(i.state);
}
2020-12-24 01:58:27 +00:00
parseMessage(msg: any) {
let op = wsUserOp(msg);
if (msg.error) {
2021-02-22 02:39:04 +00:00
toast(i18n.t(msg.error), "danger");
this.state.userLoading = false;
this.setState(this.state);
return;
2020-12-24 01:58:27 +00:00
} else if (op == UserOperation.Register) {
let data = wsJsonToRes<LoginResponse>(msg).data;
this.state.userLoading = false;
this.state.doneRegisteringUser = true;
UserService.Instance.login(data);
this.setState(this.state);
2020-12-24 01:58:27 +00:00
} else if (op == UserOperation.CreateSite) {
2021-02-22 02:39:04 +00:00
window.location.href = "/";
}
}
}