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';
5 import { Register, LoginResponse, UserOperation } from 'lemmy-js-client';
6 import { WebSocketService, UserService } from '../services';
7 import { wsUserOp, wsJsonToRes, toast, wsClient } from '../utils';
8 import { SiteForm } from './site-form';
9 import { i18n } from '../i18next';
13 doneRegisteringUser: boolean;
17 export class Setup extends Component<any, State> {
18 private subscription: Subscription;
20 private emptyState: State = {
24 password_verify: undefined,
26 // The first admin signup doesn't need a captcha
30 doneRegisteringUser: false,
34 constructor(props: any, context: any) {
35 super(props, context);
37 this.state = this.emptyState;
39 this.subscription = WebSocketService.Instance.subject
40 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
42 msg => this.parseMessage(msg),
43 err => console.error(err),
44 () => console.log('complete')
48 componentWillUnmount() {
49 this.subscription.unsubscribe();
52 get documentTitle(): string {
53 return `${i18n.t('setup')} - Lemmy`;
58 <div class="container">
59 <Helmet title={this.documentTitle} />
61 <div class="col-12 offset-lg-3 col-lg-6">
62 <h3>{i18n.t('lemmy_instance_setup')}</h3>
63 {!this.state.doneRegisteringUser ? (
76 <form onSubmit={linkEvent(this, this.handleRegisterSubmit)}>
77 <h5>{i18n.t('setup_admin')}</h5>
78 <div class="form-group row">
79 <label class="col-sm-2 col-form-label" htmlFor="username">
82 <div class="col-sm-10">
87 value={this.state.userForm.username}
88 onInput={linkEvent(this, this.handleRegisterUsernameChange)}
92 pattern="[a-zA-Z0-9_]+"
96 <div class="form-group row">
97 <label class="col-sm-2 col-form-label" htmlFor="email">
101 <div class="col-sm-10">
106 placeholder={i18n.t('optional')}
107 value={this.state.userForm.email}
108 onInput={linkEvent(this, this.handleRegisterEmailChange)}
113 <div class="form-group row">
114 <label class="col-sm-2 col-form-label" htmlFor="password">
117 <div class="col-sm-10">
121 value={this.state.userForm.password}
122 onInput={linkEvent(this, this.handleRegisterPasswordChange)}
128 <div class="form-group row">
129 <label class="col-sm-2 col-form-label" htmlFor="verify-password">
130 {i18n.t('verify_password')}
132 <div class="col-sm-10">
136 value={this.state.userForm.password_verify}
137 onInput={linkEvent(this, this.handleRegisterPasswordVerifyChange)}
143 <div class="form-group row">
144 <div class="col-sm-10">
145 <button type="submit" class="btn btn-secondary">
146 {this.state.userLoading ? (
147 <svg class="icon icon-spinner spin">
148 <use xlinkHref="#icon-spinner"></use>
160 handleRegisterSubmit(i: Setup, event: any) {
161 event.preventDefault();
162 i.state.userLoading = true;
164 event.preventDefault();
165 WebSocketService.Instance.send(wsClient.register(i.state.userForm));
168 handleRegisterUsernameChange(i: Setup, event: any) {
169 i.state.userForm.username = event.target.value;
173 handleRegisterEmailChange(i: Setup, event: any) {
174 i.state.userForm.email = event.target.value;
178 handleRegisterPasswordChange(i: Setup, event: any) {
179 i.state.userForm.password = event.target.value;
183 handleRegisterPasswordVerifyChange(i: Setup, event: any) {
184 i.state.userForm.password_verify = event.target.value;
188 parseMessage(msg: any) {
189 let op = wsUserOp(msg);
191 toast(i18n.t(msg.error), 'danger');
192 this.state.userLoading = false;
193 this.setState(this.state);
195 } else if (op == UserOperation.Register) {
196 let data = wsJsonToRes<LoginResponse>(msg).data;
197 this.state.userLoading = false;
198 this.state.doneRegisteringUser = true;
199 UserService.Instance.login(data);
200 this.setState(this.state);
201 } else if (op == UserOperation.CreateSite) {
202 window.location.href = '/';