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,
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();
55 get documentTitle(): string {
56 return `${i18n.t('setup')} - Lemmy`;
61 <div class="container">
62 <Helmet title={this.documentTitle} />
64 <div class="col-12 offset-lg-3 col-lg-6">
65 <h3>{i18n.t('lemmy_instance_setup')}</h3>
66 {!this.state.doneRegisteringUser ? (
79 <form onSubmit={linkEvent(this, this.handleRegisterSubmit)}>
80 <h5>{i18n.t('setup_admin')}</h5>
81 <div class="form-group row">
82 <label class="col-sm-2 col-form-label" htmlFor="username">
85 <div class="col-sm-10">
90 value={this.state.userForm.username}
91 onInput={linkEvent(this, this.handleRegisterUsernameChange)}
95 pattern="[a-zA-Z0-9_]+"
99 <div class="form-group row">
100 <label class="col-sm-2 col-form-label" htmlFor="email">
104 <div class="col-sm-10">
109 placeholder={i18n.t('optional')}
110 value={this.state.userForm.email}
111 onInput={linkEvent(this, this.handleRegisterEmailChange)}
116 <div class="form-group row">
117 <label class="col-sm-2 col-form-label" htmlFor="password">
120 <div class="col-sm-10">
124 value={this.state.userForm.password}
125 onInput={linkEvent(this, this.handleRegisterPasswordChange)}
131 <div class="form-group row">
132 <label class="col-sm-2 col-form-label" htmlFor="verify-password">
133 {i18n.t('verify_password')}
135 <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: WebSocketJsonResponse) {
192 let res = wsJsonToRes(msg);
194 toast(i18n.t(msg.error), 'danger');
195 this.state.userLoading = false;
196 this.setState(this.state);
198 } else if (res.op == UserOperation.Register) {
199 let data = res.data as LoginResponse;
200 this.state.userLoading = false;
201 this.state.doneRegisteringUser = true;
202 UserService.Instance.login(data);
203 this.setState(this.state);
204 } else if (res.op == UserOperation.CreateSite) {
205 this.props.history.push('/');