1 import { Component, linkEvent } from "inferno";
2 import { Helmet } from "inferno-helmet";
3 import { LoginResponse, Register, UserOperation } from "lemmy-js-client";
4 import { Subscription } from "rxjs";
5 import { delay, retryWhen, take } from "rxjs/operators";
6 import { i18n } from "../../i18next";
7 import { UserService, WebSocketService } from "../../services";
8 import { toast, wsClient, wsJsonToRes, wsUserOp } from "../../utils";
9 import { Spinner } from "../common/icon";
10 import { SiteForm } from "./site-form";
14 doneRegisteringUser: boolean;
18 export class Setup extends Component<any, State> {
19 private subscription: Subscription;
21 private emptyState: State = {
25 password_verify: undefined,
27 // The first admin signup doesn't need a captcha
31 doneRegisteringUser: false,
35 constructor(props: any, context: any) {
36 super(props, context);
38 this.state = this.emptyState;
40 this.subscription = WebSocketService.Instance.subject
41 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
43 msg => this.parseMessage(msg),
44 err => console.error(err),
45 () => console.log("complete")
49 componentWillUnmount() {
50 this.subscription.unsubscribe();
53 get documentTitle(): string {
54 return `${i18n.t("setup")} - Lemmy`;
59 <div class="container">
60 <Helmet title={this.documentTitle} />
62 <div class="col-12 offset-lg-3 col-lg-6">
63 <h3>{i18n.t("lemmy_instance_setup")}</h3>
64 {!this.state.doneRegisteringUser ? (
77 <form onSubmit={linkEvent(this, this.handleRegisterSubmit)}>
78 <h5>{i18n.t("setup_admin")}</h5>
79 <div class="form-group row">
80 <label class="col-sm-2 col-form-label" htmlFor="username">
83 <div class="col-sm-10">
88 value={this.state.userForm.username}
89 onInput={linkEvent(this, this.handleRegisterUsernameChange)}
92 pattern="[a-zA-Z0-9_]+"
96 <div class="form-group row">
97 <label class="col-sm-2 col-form-label" htmlFor="email">
101 <div class="col-sm-10">
106 placeholder={i18n.t("optional")}
107 value={this.state.userForm.email}
108 onInput={linkEvent(this, this.handleRegisterEmailChange)}
113 <div class="form-group row">
114 <label class="col-sm-2 col-form-label" htmlFor="password">
117 <div class="col-sm-10">
121 value={this.state.userForm.password}
122 onInput={linkEvent(this, this.handleRegisterPasswordChange)}
125 autoComplete="new-password"
131 <div class="form-group row">
132 <label class="col-sm-2 col-form-label" htmlFor="verify-password">
133 {i18n.t("verify_password")}
135 <div class="col-sm-10">
139 value={this.state.userForm.password_verify}
140 onInput={linkEvent(this, this.handleRegisterPasswordVerifyChange)}
143 autoComplete="new-password"
149 <div class="form-group row">
150 <div class="col-sm-10">
151 <button type="submit" class="btn btn-secondary">
152 {this.state.userLoading ? <Spinner /> : i18n.t("sign_up")}
160 handleRegisterSubmit(i: Setup, event: any) {
161 event.preventDefault();
162 i.state.userLoading = true;
164 event.preventDefault();
165 WebSocketService.Instance.send(wsClient.register(i.state.userForm));
168 handleRegisterUsernameChange(i: Setup, event: any) {
169 i.state.userForm.username = event.target.value;
173 handleRegisterEmailChange(i: Setup, event: any) {
174 i.state.userForm.email = event.target.value;
178 handleRegisterPasswordChange(i: Setup, event: any) {
179 i.state.userForm.password = event.target.value;
183 handleRegisterPasswordVerifyChange(i: Setup, event: any) {
184 i.state.userForm.password_verify = event.target.value;
188 parseMessage(msg: any) {
189 let op = wsUserOp(msg);
191 toast(i18n.t(msg.error), "danger");
192 this.state.userLoading = false;
193 this.setState(this.state);
195 } else if (op == UserOperation.Register) {
196 let data = wsJsonToRes<LoginResponse>(msg).data;
197 this.state.userLoading = false;
198 this.state.doneRegisteringUser = true;
199 UserService.Instance.login(data);
200 this.setState(this.state);
201 } else if (op == UserOperation.CreateSite) {
202 window.location.href = "/";