1 import { Component, linkEvent } from "inferno";
2 import { Helmet } from "inferno-helmet";
3 import { Subscription } from "rxjs";
4 import { retryWhen, delay, take } from "rxjs/operators";
5 import { Register, LoginResponse, UserOperation } from "lemmy-js-client";
6 import { WebSocketService, UserService } from "../services";
7 import { wsUserOp, wsJsonToRes, toast, wsClient } from "../utils";
8 import { SiteForm } from "./site-form";
9 import { Spinner } from "./icon";
10 import { i18n } from "../i18next";
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)}
93 pattern="[a-zA-Z0-9_]+"
97 <div class="form-group row">
98 <label class="col-sm-2 col-form-label" htmlFor="email">
102 <div class="col-sm-10">
107 placeholder={i18n.t("optional")}
108 value={this.state.userForm.email}
109 onInput={linkEvent(this, this.handleRegisterEmailChange)}
114 <div class="form-group row">
115 <label class="col-sm-2 col-form-label" htmlFor="password">
118 <div class="col-sm-10">
122 value={this.state.userForm.password}
123 onInput={linkEvent(this, this.handleRegisterPasswordChange)}
130 <div class="form-group row">
131 <label class="col-sm-2 col-form-label" htmlFor="verify-password">
132 {i18n.t("verify_password")}
134 <div class="col-sm-10">
138 value={this.state.userForm.password_verify}
139 onInput={linkEvent(this, this.handleRegisterPasswordVerifyChange)}
146 <div class="form-group row">
147 <div class="col-sm-10">
148 <button type="submit" class="btn btn-secondary">
149 {this.state.userLoading ? <Spinner /> : i18n.t("sign_up")}
157 handleRegisterSubmit(i: Setup, event: any) {
158 event.preventDefault();
159 i.state.userLoading = true;
161 event.preventDefault();
162 WebSocketService.Instance.send(wsClient.register(i.state.userForm));
165 handleRegisterUsernameChange(i: Setup, event: any) {
166 i.state.userForm.username = event.target.value;
170 handleRegisterEmailChange(i: Setup, event: any) {
171 i.state.userForm.email = event.target.value;
175 handleRegisterPasswordChange(i: Setup, event: any) {
176 i.state.userForm.password = event.target.value;
180 handleRegisterPasswordVerifyChange(i: Setup, event: any) {
181 i.state.userForm.password_verify = event.target.value;
185 parseMessage(msg: any) {
186 let op = wsUserOp(msg);
188 toast(i18n.t(msg.error), "danger");
189 this.state.userLoading = false;
190 this.setState(this.state);
192 } else if (op == UserOperation.Register) {
193 let data = wsJsonToRes<LoginResponse>(msg).data;
194 this.state.userLoading = false;
195 this.state.doneRegisteringUser = true;
196 UserService.Instance.login(data);
197 this.setState(this.state);
198 } else if (op == UserOperation.CreateSite) {
199 window.location.href = "/";