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 { i18n } from '../i18next';
13 doneRegisteringUser: boolean;
17 export class Setup extends Component<any, State> {
18 private subscription: Subscription;
20 private emptyState: State = {
24 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 ? (
148 <svg class="icon icon-spinner spin">
149 <use xlinkHref="#icon-spinner"></use>
161 handleRegisterSubmit(i: Setup, event: any) {
162 event.preventDefault();
163 i.state.userLoading = true;
165 event.preventDefault();
166 WebSocketService.Instance.send(wsClient.register(i.state.userForm));
169 handleRegisterUsernameChange(i: Setup, event: any) {
170 i.state.userForm.username = event.target.value;
174 handleRegisterEmailChange(i: Setup, event: any) {
175 i.state.userForm.email = event.target.value;
179 handleRegisterPasswordChange(i: Setup, event: any) {
180 i.state.userForm.password = event.target.value;
184 handleRegisterPasswordVerifyChange(i: Setup, event: any) {
185 i.state.userForm.password_verify = event.target.value;
189 parseMessage(msg: any) {
190 let op = wsUserOp(msg);
192 toast(i18n.t(msg.error), 'danger');
193 this.state.userLoading = false;
194 this.setState(this.state);
196 } else if (op == UserOperation.Register) {
197 let data = wsJsonToRes<LoginResponse>(msg).data;
198 this.state.userLoading = false;
199 this.state.doneRegisteringUser = true;
200 UserService.Instance.login(data);
201 this.setState(this.state);
202 } else if (op == UserOperation.CreateSite) {
203 window.location.href = '/';