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)}
129 <div class="form-group row">
130 <label class="col-sm-2 col-form-label" htmlFor="verify-password">
131 {i18n.t("verify_password")}
133 <div class="col-sm-10">
137 value={this.state.userForm.password_verify}
138 onInput={linkEvent(this, this.handleRegisterPasswordVerifyChange)}
144 <div class="form-group row">
145 <div class="col-sm-10">
146 <button type="submit" class="btn btn-secondary">
147 {this.state.userLoading ? <Spinner /> : i18n.t("sign_up")}
155 handleRegisterSubmit(i: Setup, event: any) {
156 event.preventDefault();
157 i.state.userLoading = true;
159 event.preventDefault();
160 WebSocketService.Instance.send(wsClient.register(i.state.userForm));
163 handleRegisterUsernameChange(i: Setup, event: any) {
164 i.state.userForm.username = event.target.value;
168 handleRegisterEmailChange(i: Setup, event: any) {
169 i.state.userForm.email = event.target.value;
173 handleRegisterPasswordChange(i: Setup, event: any) {
174 i.state.userForm.password = event.target.value;
178 handleRegisterPasswordVerifyChange(i: Setup, event: any) {
179 i.state.userForm.password_verify = event.target.value;
183 parseMessage(msg: any) {
184 let op = wsUserOp(msg);
186 toast(i18n.t(msg.error), "danger");
187 this.state.userLoading = false;
188 this.setState(this.state);
190 } else if (op == UserOperation.Register) {
191 let data = wsJsonToRes<LoginResponse>(msg).data;
192 this.state.userLoading = false;
193 this.state.doneRegisteringUser = true;
194 UserService.Instance.login(data);
195 this.setState(this.state);
196 } else if (op == UserOperation.CreateSite) {
197 window.location.href = "/";