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, toast } 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
38 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
40 msg => this.parseMessage(msg),
41 err => console.error(err),
42 () => console.log('complete')
46 componentWillUnmount() {
47 this.subscription.unsubscribe();
51 document.title = `${i18n.t('setup')} - Lemmy`;
56 <div class="container">
58 <div class="col-12 offset-lg-3 col-lg-6">
60 <T i18nKey="lemmy_instance_setup">#</T>
62 {!this.state.doneRegisteringUser ? (
75 <form onSubmit={linkEvent(this, this.handleRegisterSubmit)}>
77 <T i18nKey="setup_admin">#</T>
79 <div class="form-group row">
80 <label class="col-sm-2 col-form-label">
81 <T i18nKey="username">#</T>
83 <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">
98 <T i18nKey="email">#</T>
100 <div class="col-sm-10">
104 placeholder={i18n.t('optional')}
105 value={this.state.userForm.email}
106 onInput={linkEvent(this, this.handleRegisterEmailChange)}
111 <div class="form-group row">
112 <label class="col-sm-2 col-form-label">
113 <T i18nKey="password">#</T>
115 <div class="col-sm-10">
118 value={this.state.userForm.password}
119 onInput={linkEvent(this, this.handleRegisterPasswordChange)}
125 <div class="form-group row">
126 <label class="col-sm-2 col-form-label">
127 <T i18nKey="verify_password">#</T>
129 <div class="col-sm-10">
132 value={this.state.userForm.password_verify}
133 onInput={linkEvent(this, this.handleRegisterPasswordVerifyChange)}
139 <div class="form-group row">
140 <div class="col-sm-10">
141 <button type="submit" class="btn btn-secondary">
142 {this.state.userLoading ? (
143 <svg class="icon icon-spinner spin">
144 <use xlinkHref="#icon-spinner"></use>
156 handleRegisterSubmit(i: Setup, event: any) {
157 event.preventDefault();
158 i.state.userLoading = true;
160 event.preventDefault();
161 WebSocketService.Instance.register(i.state.userForm);
164 handleRegisterUsernameChange(i: Setup, event: any) {
165 i.state.userForm.username = event.target.value;
169 handleRegisterEmailChange(i: Setup, event: any) {
170 i.state.userForm.email = event.target.value;
174 handleRegisterPasswordChange(i: Setup, event: any) {
175 i.state.userForm.password = event.target.value;
179 handleRegisterPasswordVerifyChange(i: Setup, event: any) {
180 i.state.userForm.password_verify = event.target.value;
184 parseMessage(msg: any) {
185 let op: UserOperation = msgOp(msg);
187 toast(i18n.t(msg.error), 'danger');
188 this.state.userLoading = false;
189 this.setState(this.state);
191 } else if (op == UserOperation.Register) {
192 this.state.userLoading = false;
193 this.state.doneRegisteringUser = true;
194 let res: LoginResponse = msg;
195 UserService.Instance.login(res);
197 this.setState(this.state);
198 } else if (op == UserOperation.CreateSite) {
199 this.props.history.push('/');