1 import { Component, linkEvent } from 'inferno';
2 import { Subscription } from "rxjs";
3 import { retryWhen, delay, take } from 'rxjs/operators';
4 import { LoginForm, RegisterForm, LoginResponse, UserOperation } from '../interfaces';
5 import { WebSocketService, UserService } from '../services';
6 import { msgOp } from '../utils';
10 registerForm: RegisterForm;
11 loginLoading: boolean;
12 registerLoading: boolean;
15 let emptyState: State = {
17 username_or_email: undefined,
23 password_verify: undefined,
27 registerLoading: false
30 export class Login extends Component<any, State> {
31 private subscription: Subscription;
33 constructor(props: any, context: any) {
34 super(props, context);
36 this.state = emptyState;
38 this.subscription = WebSocketService.Instance.subject
39 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
41 (msg) => this.parseMessage(msg),
42 (err) => console.error(err),
43 () => console.log("complete")
47 componentWillUnmount() {
48 this.subscription.unsubscribe();
53 <div class="container">
55 <div class="col-12 col-lg-6 mb-4">
58 <div class="col-12 col-lg-6">
69 <form onSubmit={linkEvent(this, this.handleLoginSubmit)}>
71 <div class="form-group row">
72 <label class="col-sm-2 col-form-label">Email or Username</label>
73 <div class="col-sm-10">
74 <input type="text" class="form-control" value={this.state.loginForm.username_or_email} onInput={linkEvent(this, this.handleLoginUsernameChange)} required minLength={3} />
77 <div class="form-group row">
78 <label class="col-sm-2 col-form-label">Password</label>
79 <div class="col-sm-10">
80 <input type="password" value={this.state.loginForm.password} onInput={linkEvent(this, this.handleLoginPasswordChange)} class="form-control" required />
83 <div class="form-group row">
84 <div class="col-sm-10">
85 <button type="submit" class="btn btn-secondary">{this.state.loginLoading ?
86 <svg class="icon icon-spinner spin"><use xlinkHref="#icon-spinner"></use></svg> : 'Login'}</button>
90 Forgot your password or deleted your account? Reset your password. TODO
96 <form onSubmit={linkEvent(this, this.handleRegisterSubmit)}>
98 <div class="form-group row">
99 <label class="col-sm-2 col-form-label">Username</label>
100 <div class="col-sm-10">
101 <input type="text" class="form-control" value={this.state.registerForm.username} onInput={linkEvent(this, this.handleRegisterUsernameChange)} required minLength={3} maxLength={20} pattern="[a-zA-Z0-9_]+" />
104 <div class="form-group row">
105 <label class="col-sm-2 col-form-label">Email</label>
106 <div class="col-sm-10">
107 <input type="email" class="form-control" placeholder="Optional" value={this.state.registerForm.email} onInput={linkEvent(this, this.handleRegisterEmailChange)} minLength={3} />
110 <div class="form-group row">
111 <label class="col-sm-2 col-form-label">Password</label>
112 <div class="col-sm-10">
113 <input type="password" value={this.state.registerForm.password} onInput={linkEvent(this, this.handleRegisterPasswordChange)} class="form-control" required />
116 <div class="form-group row">
117 <label class="col-sm-2 col-form-label">Verify Password</label>
118 <div class="col-sm-10">
119 <input type="password" value={this.state.registerForm.password_verify} onInput={linkEvent(this, this.handleRegisterPasswordVerifyChange)} class="form-control" required />
122 <div class="form-group row">
123 <div class="col-sm-10">
124 <button type="submit" class="btn btn-secondary">{this.state.registerLoading ?
125 <svg class="icon icon-spinner spin"><use xlinkHref="#icon-spinner"></use></svg> : 'Sign Up'}</button>
133 handleLoginSubmit(i: Login, event: any) {
134 event.preventDefault();
135 i.state.loginLoading = true;
137 WebSocketService.Instance.login(i.state.loginForm);
140 handleLoginUsernameChange(i: Login, event: any) {
141 i.state.loginForm.username_or_email = event.target.value;
145 handleLoginPasswordChange(i: Login, event: any) {
146 i.state.loginForm.password = event.target.value;
150 handleRegisterSubmit(i: Login, event: any) {
151 event.preventDefault();
152 i.state.registerLoading = true;
154 event.preventDefault();
155 WebSocketService.Instance.register(i.state.registerForm);
158 handleRegisterUsernameChange(i: Login, event: any) {
159 i.state.registerForm.username = event.target.value;
163 handleRegisterEmailChange(i: Login, event: any) {
164 i.state.registerForm.email = event.target.value;
168 handleRegisterPasswordChange(i: Login, event: any) {
169 i.state.registerForm.password = event.target.value;
173 handleRegisterPasswordVerifyChange(i: Login, event: any) {
174 i.state.registerForm.password_verify = event.target.value;
178 parseMessage(msg: any) {
179 let op: UserOperation = msgOp(msg);
182 this.state.loginLoading = false;
183 this.state.registerLoading = false;
184 this.setState(this.state);
187 if (op == UserOperation.Register || op == UserOperation.Login) {
188 this.state.loginLoading = false;
189 this.state.registerLoading = false;
190 let res: LoginResponse = msg;
191 UserService.Instance.login(res);
192 this.props.history.push('/');