1 import { Component, linkEvent } from 'inferno';
2 import { Subscription } from "rxjs";
3 import { retryWhen, delay, take } from 'rxjs/operators';
4 import { RegisterForm, LoginResponse, UserOperation } from '../interfaces';
5 import { WebSocketService, UserService } from '../services';
6 import { msgOp } from '../utils';
7 import { SiteForm } from './site-form';
8 import { i18n } from '../i18next';
9 import { T } from 'inferno-i18next';
12 userForm: RegisterForm;
13 doneRegisteringUser: boolean;
17 export class Setup extends Component<any, State> {
18 private subscription: Subscription;
20 private emptyState: State = {
24 password_verify: undefined,
27 doneRegisteringUser: false,
32 constructor(props: any, context: any) {
33 super(props, context);
35 this.state = this.emptyState;
37 this.subscription = WebSocketService.Instance.subject
38 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
40 (msg) => this.parseMessage(msg),
41 (err) => console.error(err),
42 () => console.log("complete")
46 componentWillUnmount() {
47 this.subscription.unsubscribe();
51 document.title = `${i18n.t('setup')} - Lemmy`;
56 <div class="container">
58 <div class="col-12 offset-lg-3 col-lg-6">
59 <h3><T i18nKey="lemmy_instance_setup">#</T></h3>
60 {!this.state.doneRegisteringUser ? this.registerUser() : <SiteForm />}
69 <form onSubmit={linkEvent(this, this.handleRegisterSubmit)}>
70 <h5><T i18nKey="setup_admin">#</T></h5>
71 <div class="form-group row">
72 <label class="col-sm-2 col-form-label"><T i18nKey="username">#</T></label>
73 <div class="col-sm-10">
74 <input type="text" class="form-control" value={this.state.userForm.username} onInput={linkEvent(this, this.handleRegisterUsernameChange)} required minLength={3} maxLength={20} pattern="[a-zA-Z0-9_]+" />
77 <div class="form-group row">
78 <label class="col-sm-2 col-form-label"><T i18nKey="email">#</T></label>
79 <div class="col-sm-10">
80 <input type="email" class="form-control" placeholder={i18n.t('optional')} value={this.state.userForm.email} onInput={linkEvent(this, this.handleRegisterEmailChange)} minLength={3} />
83 <div class="form-group row">
84 <label class="col-sm-2 col-form-label"><T i18nKey="password">#</T></label>
85 <div class="col-sm-10">
86 <input type="password" value={this.state.userForm.password} onInput={linkEvent(this, this.handleRegisterPasswordChange)} class="form-control" required />
89 <div class="form-group row">
90 <label class="col-sm-2 col-form-label"><T i18nKey="verify_password">#</T></label>
91 <div class="col-sm-10">
92 <input type="password" value={this.state.userForm.password_verify} onInput={linkEvent(this, this.handleRegisterPasswordVerifyChange)} class="form-control" required />
95 <div class="form-group row">
96 <div class="col-sm-10">
97 <button type="submit" class="btn btn-secondary">{this.state.userLoading ?
98 <svg class="icon icon-spinner spin"><use xlinkHref="#icon-spinner"></use></svg> : i18n.t('sign_up')}</button>
107 handleRegisterSubmit(i: Setup, event: any) {
108 event.preventDefault();
109 i.state.userLoading = true;
111 event.preventDefault();
112 WebSocketService.Instance.register(i.state.userForm);
115 handleRegisterUsernameChange(i: Setup, event: any) {
116 i.state.userForm.username = event.target.value;
120 handleRegisterEmailChange(i: Setup, event: any) {
121 i.state.userForm.email = event.target.value;
125 handleRegisterPasswordChange(i: Setup, event: any) {
126 i.state.userForm.password = event.target.value;
130 handleRegisterPasswordVerifyChange(i: Setup, event: any) {
131 i.state.userForm.password_verify = event.target.value;
135 parseMessage(msg: any) {
136 let op: UserOperation = msgOp(msg);
139 this.state.userLoading = false;
140 this.setState(this.state);
142 } else if (op == UserOperation.Register) {
143 this.state.userLoading = false;
144 this.state.doneRegisteringUser = true;
145 let res: LoginResponse = msg;
146 UserService.Instance.login(res);
148 this.setState(this.state);
149 } else if (op == UserOperation.CreateSite) {
150 this.props.history.push('/');