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';
10 } from '../interfaces';
11 import { WebSocketService, UserService } from '../services';
12 import { wsJsonToRes, toast } from '../utils';
13 import { SiteForm } from './site-form';
14 import { i18n } from '../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,
32 // The first admin signup doesn't need a captcha
36 doneRegisteringUser: false,
40 constructor(props: any, context: any) {
41 super(props, context);
43 this.state = this.emptyState;
45 this.subscription = WebSocketService.Instance.subject
46 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
48 msg => this.parseMessage(msg),
49 err => console.error(err),
50 () => console.log('complete')
54 componentWillUnmount() {
55 this.subscription.unsubscribe();
58 get documentTitle(): string {
59 return `${i18n.t('setup')} - Lemmy`;
64 <div class="container">
65 <Helmet title={this.documentTitle} />
67 <div class="col-12 offset-lg-3 col-lg-6">
68 <h3>{i18n.t('lemmy_instance_setup')}</h3>
69 {!this.state.doneRegisteringUser ? (
82 <form onSubmit={linkEvent(this, this.handleRegisterSubmit)}>
83 <h5>{i18n.t('setup_admin')}</h5>
84 <div class="form-group row">
85 <label class="col-sm-2 col-form-label" htmlFor="username">
88 <div class="col-sm-10">
93 value={this.state.userForm.username}
94 onInput={linkEvent(this, this.handleRegisterUsernameChange)}
98 pattern="[a-zA-Z0-9_]+"
102 <div class="form-group row">
103 <label class="col-sm-2 col-form-label" htmlFor="email">
107 <div class="col-sm-10">
112 placeholder={i18n.t('optional')}
113 value={this.state.userForm.email}
114 onInput={linkEvent(this, this.handleRegisterEmailChange)}
119 <div class="form-group row">
120 <label class="col-sm-2 col-form-label" htmlFor="password">
123 <div class="col-sm-10">
127 value={this.state.userForm.password}
128 onInput={linkEvent(this, this.handleRegisterPasswordChange)}
134 <div class="form-group row">
135 <label class="col-sm-2 col-form-label" htmlFor="verify-password">
136 {i18n.t('verify_password')}
138 <div class="col-sm-10">
142 value={this.state.userForm.password_verify}
143 onInput={linkEvent(this, this.handleRegisterPasswordVerifyChange)}
149 <div class="form-group row">
150 <div class="col-sm-10">
151 <button type="submit" class="btn btn-secondary">
152 {this.state.userLoading ? (
153 <svg class="icon icon-spinner spin">
154 <use xlinkHref="#icon-spinner"></use>
166 handleRegisterSubmit(i: Setup, event: any) {
167 event.preventDefault();
168 i.state.userLoading = true;
170 event.preventDefault();
171 WebSocketService.Instance.register(i.state.userForm);
174 handleRegisterUsernameChange(i: Setup, event: any) {
175 i.state.userForm.username = event.target.value;
179 handleRegisterEmailChange(i: Setup, event: any) {
180 i.state.userForm.email = event.target.value;
184 handleRegisterPasswordChange(i: Setup, event: any) {
185 i.state.userForm.password = event.target.value;
189 handleRegisterPasswordVerifyChange(i: Setup, event: any) {
190 i.state.userForm.password_verify = event.target.value;
194 parseMessage(msg: WebSocketJsonResponse) {
195 let res = wsJsonToRes(msg);
197 toast(i18n.t(msg.error), 'danger');
198 this.state.userLoading = false;
199 this.setState(this.state);
201 } else if (res.op == UserOperation.Register) {
202 let data = res.data as LoginResponse;
203 this.state.userLoading = false;
204 this.state.doneRegisteringUser = true;
205 UserService.Instance.login(data);
206 this.setState(this.state);
207 } else if (res.op == UserOperation.CreateSite) {
208 this.props.history.push('/');