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
26 registerLoading: false
29 export class Login extends Component<any, State> {
30 private subscription: Subscription;
32 constructor(props: any, context: any) {
33 super(props, context);
35 this.state = 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();
52 <div class="container">
54 <div class="col-12 col-lg-6 mb-4">
57 <div class="col-12 col-lg-6">
68 <form onSubmit={linkEvent(this, this.handleLoginSubmit)}>
70 <div class="form-group row">
71 <label class="col-sm-2 col-form-label">Email or Username</label>
72 <div class="col-sm-10">
73 <input type="text" class="form-control" value={this.state.loginForm.username_or_email} onInput={linkEvent(this, this.handleLoginUsernameChange)} required minLength={3} />
76 <div class="form-group row">
77 <label class="col-sm-2 col-form-label">Password</label>
78 <div class="col-sm-10">
79 <input type="password" value={this.state.loginForm.password} onInput={linkEvent(this, this.handleLoginPasswordChange)} class="form-control" required />
82 <div class="form-group row">
83 <div class="col-sm-10">
84 <button type="submit" class="btn btn-secondary">{this.state.loginLoading ?
85 <svg class="icon icon-spinner spin"><use xlinkHref="#icon-spinner"></use></svg> : 'Login'}</button>
89 Forgot your password or deleted your account? Reset your password. TODO
95 <form onSubmit={linkEvent(this, this.handleRegisterSubmit)}>
97 <div class="form-group row">
98 <label class="col-sm-2 col-form-label">Username</label>
99 <div class="col-sm-10">
100 <input type="text" class="form-control" value={this.state.registerForm.username} onInput={linkEvent(this, this.handleRegisterUsernameChange)} required minLength={3} pattern="[a-zA-Z0-9_]+" />
103 <div class="form-group row">
104 <label class="col-sm-2 col-form-label">Email</label>
105 <div class="col-sm-10">
106 <input type="email" class="form-control" placeholder="Optional" value={this.state.registerForm.email} onInput={linkEvent(this, this.handleRegisterEmailChange)} minLength={3} />
109 <div class="form-group row">
110 <label class="col-sm-2 col-form-label">Password</label>
111 <div class="col-sm-10">
112 <input type="password" value={this.state.registerForm.password} onInput={linkEvent(this, this.handleRegisterPasswordChange)} class="form-control" required />
115 <div class="form-group row">
116 <label class="col-sm-2 col-form-label">Verify Password</label>
117 <div class="col-sm-10">
118 <input type="password" value={this.state.registerForm.password_verify} onInput={linkEvent(this, this.handleRegisterPasswordVerifyChange)} class="form-control" required />
121 <div class="form-group row">
122 <div class="col-sm-10">
123 <button type="submit" class="btn btn-secondary">{this.state.registerLoading ?
124 <svg class="icon icon-spinner spin"><use xlinkHref="#icon-spinner"></use></svg> : 'Sign Up'}</button>
132 handleLoginSubmit(i: Login, event: any) {
133 event.preventDefault();
134 i.state.loginLoading = true;
136 WebSocketService.Instance.login(i.state.loginForm);
139 handleLoginUsernameChange(i: Login, event: any) {
140 i.state.loginForm.username_or_email = event.target.value;
144 handleLoginPasswordChange(i: Login, event: any) {
145 i.state.loginForm.password = event.target.value;
149 handleRegisterSubmit(i: Login, event: any) {
150 i.state.registerLoading = true;
152 event.preventDefault();
153 WebSocketService.Instance.register(i.state.registerForm);
156 handleRegisterUsernameChange(i: Login, event: any) {
157 i.state.registerForm.username = event.target.value;
161 handleRegisterEmailChange(i: Login, event: any) {
162 i.state.registerForm.email = event.target.value;
166 handleRegisterPasswordChange(i: Login, event: any) {
167 i.state.registerForm.password = event.target.value;
171 handleRegisterPasswordVerifyChange(i: Login, event: any) {
172 i.state.registerForm.password_verify = event.target.value;
176 parseMessage(msg: any) {
177 let op: UserOperation = msgOp(msg);
180 this.state.loginLoading = false;
181 this.state.registerLoading = false;
182 this.setState(this.state);
185 if (op == UserOperation.Register || op == UserOperation.Login) {
186 this.state.loginLoading = false;
187 this.state.registerLoading = false;
188 let res: LoginResponse = msg;
189 UserService.Instance.login(res);
190 this.props.history.push('/');