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 } from '../utils';
7 import { SiteForm } from './site-form';
10 userForm: RegisterForm;
11 doneRegisteringUser: boolean;
15 export class Setup extends Component<any, State> {
16 private subscription: Subscription;
18 private emptyState: State = {
22 password_verify: undefined,
26 doneRegisteringUser: false,
31 constructor(props: any, context: any) {
32 super(props, context);
34 this.state = this.emptyState;
36 this.subscription = WebSocketService.Instance.subject
37 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
39 (msg) => this.parseMessage(msg),
40 (err) => console.error(err),
41 () => console.log("complete")
45 componentWillUnmount() {
46 this.subscription.unsubscribe();
50 document.title = "Setup - Lemmy";
55 <div class="container">
57 <div class="col-12 offset-lg-3 col-lg-6">
58 <h3>Lemmy Instance Setup</h3>
59 {!this.state.doneRegisteringUser ? this.registerUser() : <SiteForm />}
68 <form onSubmit={linkEvent(this, this.handleRegisterSubmit)}>
69 <h5>Set up Site Administrator</h5>
70 <div class="form-group row">
71 <label class="col-sm-2 col-form-label">Username</label>
72 <div class="col-sm-10">
73 <input type="text" class="form-control" value={this.state.userForm.username} onInput={linkEvent(this, this.handleRegisterUsernameChange)} required minLength={3} maxLength={20} pattern="[a-zA-Z0-9_]+" />
76 <div class="form-group row">
77 <label class="col-sm-2 col-form-label">Email</label>
78 <div class="col-sm-10">
79 <input type="email" class="form-control" placeholder="Optional" value={this.state.userForm.email} onInput={linkEvent(this, this.handleRegisterEmailChange)} minLength={3} />
82 <div class="form-group row">
83 <label class="col-sm-2 col-form-label">Password</label>
84 <div class="col-sm-10">
85 <input type="password" value={this.state.userForm.password} onInput={linkEvent(this, this.handleRegisterPasswordChange)} class="form-control" required />
88 <div class="form-group row">
89 <label class="col-sm-2 col-form-label">Verify Password</label>
90 <div class="col-sm-10">
91 <input type="password" value={this.state.userForm.password_verify} onInput={linkEvent(this, this.handleRegisterPasswordVerifyChange)} class="form-control" required />
94 <div class="form-group row">
95 <div class="col-sm-10">
96 <button type="submit" class="btn btn-secondary">{this.state.userLoading ?
97 <svg class="icon icon-spinner spin"><use xlinkHref="#icon-spinner"></use></svg> : 'Sign Up'}</button>
106 handleRegisterSubmit(i: Setup, event: any) {
107 event.preventDefault();
108 i.state.userLoading = true;
110 event.preventDefault();
111 WebSocketService.Instance.register(i.state.userForm);
114 handleRegisterUsernameChange(i: Setup, event: any) {
115 i.state.userForm.username = event.target.value;
119 handleRegisterEmailChange(i: Setup, event: any) {
120 i.state.userForm.email = event.target.value;
124 handleRegisterPasswordChange(i: Setup, event: any) {
125 i.state.userForm.password = event.target.value;
129 handleRegisterPasswordVerifyChange(i: Setup, event: any) {
130 i.state.userForm.password_verify = event.target.value;
134 parseMessage(msg: any) {
135 let op: UserOperation = msgOp(msg);
138 this.state.userLoading = false;
139 this.setState(this.state);
141 } else if (op == UserOperation.Register) {
142 this.state.userLoading = false;
143 this.state.doneRegisteringUser = true;
144 let res: LoginResponse = msg;
145 UserService.Instance.login(res);
147 this.setState(this.state);
148 } else if (op == UserOperation.CreateSite) {
149 this.props.history.push('/');