1 import { Component, linkEvent } from 'inferno';
2 import { Subscription } from 'rxjs';
3 import { retryWhen, delay, take } from 'rxjs/operators';
9 } from '../interfaces';
10 import { WebSocketService, UserService } from '../services';
11 import { wsJsonToRes, toast } from '../utils';
12 import { SiteForm } from './site-form';
13 import { i18n } from '../i18next';
14 import { T } from 'inferno-i18next';
17 userForm: RegisterForm;
18 doneRegisteringUser: boolean;
22 export class Setup extends Component<any, State> {
23 private subscription: Subscription;
25 private emptyState: State = {
29 password_verify: undefined,
33 doneRegisteringUser: false,
37 constructor(props: any, context: any) {
38 super(props, context);
40 this.state = this.emptyState;
42 this.subscription = WebSocketService.Instance.subject
43 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
45 msg => this.parseMessage(msg),
46 err => console.error(err),
47 () => console.log('complete')
51 componentWillUnmount() {
52 this.subscription.unsubscribe();
56 document.title = `${i18n.t('setup')} - Lemmy`;
61 <div class="container">
63 <div class="col-12 offset-lg-3 col-lg-6">
65 { i18n.t('lemmy_instance_setup') }
67 {!this.state.doneRegisteringUser ? (
80 <form onSubmit={linkEvent(this, this.handleRegisterSubmit)}>
81 <h4>{ i18n.t('setup_admin')}</h4>
82 <div class="form-group row">
83 <label class="col-sm-2 col-form-label" for="username">
84 { i18n.t('username') }
86 <div class="col-sm-10">
91 value={this.state.userForm.username}
92 onInput={linkEvent(this, this.handleRegisterUsernameChange)}
96 pattern="[a-zA-Z0-9_]+"
100 <div class="form-group row">
101 <label class="col-sm-2 col-form-label" for="email">
105 <div class="col-sm-10">
110 placeholder={i18n.t('optional')}
111 value={this.state.userForm.email}
112 onInput={linkEvent(this, this.handleRegisterEmailChange)}
117 <div class="form-group row">
118 <label class="col-sm-2 col-form-label" for="password">
119 { i18n.t('password')}
121 <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" for="verify-password">
134 { i18n.t('verify_password') }
136 <div class="col-sm-10">
140 value={this.state.userForm.password_verify}
141 onInput={linkEvent(this, this.handleRegisterPasswordVerifyChange)}
147 <div class="form-group row">
148 <div class="col-sm-10">
149 <button type="submit" class="btn btn-secondary">
150 {this.state.userLoading ? (
151 <svg class="icon icon-spinner spin">
152 <use xlinkHref="#icon-spinner"></use>
164 handleRegisterSubmit(i: Setup, event: any) {
165 event.preventDefault();
166 i.state.userLoading = true;
168 event.preventDefault();
169 WebSocketService.Instance.register(i.state.userForm);
172 handleRegisterUsernameChange(i: Setup, event: any) {
173 i.state.userForm.username = event.target.value;
177 handleRegisterEmailChange(i: Setup, event: any) {
178 i.state.userForm.email = event.target.value;
182 handleRegisterPasswordChange(i: Setup, event: any) {
183 i.state.userForm.password = event.target.value;
187 handleRegisterPasswordVerifyChange(i: Setup, event: any) {
188 i.state.userForm.password_verify = event.target.value;
192 parseMessage(msg: WebSocketJsonResponse) {
193 let res = wsJsonToRes(msg);
195 toast(i18n.t(msg.error), 'danger');
196 this.state.userLoading = false;
197 this.setState(this.state);
199 } else if (res.op == UserOperation.Register) {
200 let data = res.data as LoginResponse;
201 this.state.userLoading = false;
202 this.state.doneRegisteringUser = true;
203 UserService.Instance.login(data);
204 this.setState(this.state);
205 } else if (res.op == UserOperation.CreateSite) {
206 this.props.history.push('/');