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,
28 doneRegisteringUser: false,
32 constructor(props: any, context: any) {
33 super(props, context);
35 this.state = this.emptyState;
37 this.subscription = WebSocketService.Instance.subject
47 msg => this.parseMessage(msg),
48 err => console.error(err),
49 () => console.log('complete')
53 componentWillUnmount() {
54 this.subscription.unsubscribe();
58 document.title = `${i18n.t('setup')} - Lemmy`;
63 <div class="container">
65 <div class="col-12 offset-lg-3 col-lg-6">
67 <T i18nKey="lemmy_instance_setup">#</T>
69 {!this.state.doneRegisteringUser ? (
82 <form onSubmit={linkEvent(this, this.handleRegisterSubmit)}>
84 <T i18nKey="setup_admin">#</T>
86 <div class="form-group row">
87 <label class="col-sm-2 col-form-label">
88 <T i18nKey="username">#</T>
90 <div class="col-sm-10">
94 value={this.state.userForm.username}
95 onInput={linkEvent(this, this.handleRegisterUsernameChange)}
99 pattern="[a-zA-Z0-9_]+"
103 <div class="form-group row">
104 <label class="col-sm-2 col-form-label">
105 <T i18nKey="email">#</T>
107 <div class="col-sm-10">
111 placeholder={i18n.t('optional')}
112 value={this.state.userForm.email}
113 onInput={linkEvent(this, this.handleRegisterEmailChange)}
118 <div class="form-group row">
119 <label class="col-sm-2 col-form-label">
120 <T i18nKey="password">#</T>
122 <div class="col-sm-10">
125 value={this.state.userForm.password}
126 onInput={linkEvent(this, this.handleRegisterPasswordChange)}
132 <div class="form-group row">
133 <label class="col-sm-2 col-form-label">
134 <T i18nKey="verify_password">#</T>
136 <div class="col-sm-10">
139 value={this.state.userForm.password_verify}
140 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 ? (
150 <svg class="icon icon-spinner spin">
151 <use xlinkHref="#icon-spinner"></use>
163 handleRegisterSubmit(i: Setup, event: any) {
164 event.preventDefault();
165 i.state.userLoading = true;
167 event.preventDefault();
168 WebSocketService.Instance.register(i.state.userForm);
171 handleRegisterUsernameChange(i: Setup, event: any) {
172 i.state.userForm.username = event.target.value;
176 handleRegisterEmailChange(i: Setup, event: any) {
177 i.state.userForm.email = event.target.value;
181 handleRegisterPasswordChange(i: Setup, event: any) {
182 i.state.userForm.password = event.target.value;
186 handleRegisterPasswordVerifyChange(i: Setup, event: any) {
187 i.state.userForm.password_verify = event.target.value;
191 parseMessage(msg: any) {
192 let op: UserOperation = msgOp(msg);
194 alert(i18n.t(msg.error));
195 this.state.userLoading = false;
196 this.setState(this.state);
198 } else if (op == UserOperation.Register) {
199 this.state.userLoading = false;
200 this.state.doneRegisteringUser = true;
201 let res: LoginResponse = msg;
202 UserService.Instance.login(res);
204 this.setState(this.state);
205 } else if (op == UserOperation.CreateSite) {
206 this.props.history.push('/');