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';
7 import { i18n } from '../i18next';
8 import { T } from 'inferno-i18next';
12 registerForm: RegisterForm;
13 loginLoading: boolean;
14 registerLoading: boolean;
18 export class Login extends Component<any, State> {
19 private subscription: Subscription;
23 username_or_email: undefined,
29 password_verify: undefined,
33 registerLoading: false,
36 constructor(props: any, context: any) {
37 super(props, context);
39 this.state = this.emptyState;
41 this.subscription = WebSocketService.Instance.subject
42 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
44 (msg) => this.parseMessage(msg),
45 (err) => console.error(err),
46 () => console.log("complete")
50 componentWillUnmount() {
51 this.subscription.unsubscribe();
55 document.title = `${i18n.t('login')} - ${WebSocketService.Instance.site.name}`;
60 <div class="container">
62 <div class="col-12 col-lg-6 mb-4">
65 <div class="col-12 col-lg-6">
76 <form onSubmit={linkEvent(this, this.handleLoginSubmit)}>
78 <div class="form-group row">
79 <label class="col-sm-2 col-form-label"><T i18nKey="email_or_username">#</T></label>
80 <div class="col-sm-10">
81 <input type="text" class="form-control" value={this.state.loginForm.username_or_email} onInput={linkEvent(this, this.handleLoginUsernameChange)} required minLength={3} />
84 <div class="form-group row">
85 <label class="col-sm-2 col-form-label"><T i18nKey="password">#</T></label>
86 <div class="col-sm-10">
87 <input type="password" value={this.state.loginForm.password} onInput={linkEvent(this, this.handleLoginPasswordChange)} class="form-control" required />
90 <div class="form-group row">
91 <div class="col-sm-10">
92 <button type="submit" class="btn btn-secondary">{this.state.loginLoading ?
93 <svg class="icon icon-spinner spin"><use xlinkHref="#icon-spinner"></use></svg> : i18n.t('login')}</button>
102 <form onSubmit={linkEvent(this, this.handleRegisterSubmit)}>
103 <h5><T i18nKey="sign_up">#</T></h5>
104 <div class="form-group row">
105 <label class="col-sm-2 col-form-label"><T i18nKey="username">#</T></label>
106 <div class="col-sm-10">
107 <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_]+" />
110 <div class="form-group row">
111 <label class="col-sm-2 col-form-label"><T i18nKey="email">#</T></label>
112 <div class="col-sm-10">
113 <input type="email" class="form-control" placeholder={i18n.t('optional')} value={this.state.registerForm.email} onInput={linkEvent(this, this.handleRegisterEmailChange)} minLength={3} />
116 <div class="form-group row">
117 <label class="col-sm-2 col-form-label"><T i18nKey="password">#</T></label>
118 <div class="col-sm-10">
119 <input type="password" value={this.state.registerForm.password} onInput={linkEvent(this, this.handleRegisterPasswordChange)} class="form-control" required />
122 <div class="form-group row">
123 <label class="col-sm-2 col-form-label"><T i18nKey="verify_password">#</T></label>
124 <div class="col-sm-10">
125 <input type="password" value={this.state.registerForm.password_verify} onInput={linkEvent(this, this.handleRegisterPasswordVerifyChange)} class="form-control" required />
128 <div class="form-group row">
129 <div class="col-sm-10">
130 <button type="submit" class="btn btn-secondary">{this.state.registerLoading ?
131 <svg class="icon icon-spinner spin"><use xlinkHref="#icon-spinner"></use></svg> : i18n.t('sign_up')}</button>
139 handleLoginSubmit(i: Login, event: any) {
140 event.preventDefault();
141 i.state.loginLoading = true;
143 WebSocketService.Instance.login(i.state.loginForm);
146 handleLoginUsernameChange(i: Login, event: any) {
147 i.state.loginForm.username_or_email = event.target.value;
151 handleLoginPasswordChange(i: Login, event: any) {
152 i.state.loginForm.password = event.target.value;
156 handleRegisterSubmit(i: Login, event: any) {
157 event.preventDefault();
158 i.state.registerLoading = true;
161 WebSocketService.Instance.register(i.state.registerForm);
164 handleRegisterUsernameChange(i: Login, event: any) {
165 i.state.registerForm.username = event.target.value;
169 handleRegisterEmailChange(i: Login, event: any) {
170 i.state.registerForm.email = event.target.value;
174 handleRegisterPasswordChange(i: Login, event: any) {
175 i.state.registerForm.password = event.target.value;
179 handleRegisterPasswordVerifyChange(i: Login, event: any) {
180 i.state.registerForm.password_verify = event.target.value;
184 parseMessage(msg: any) {
185 let op: UserOperation = msgOp(msg);
187 alert(i18n.t(msg.error));
188 this.state = this.emptyState;
189 this.setState(this.state);
192 if (op == UserOperation.Login) {
193 this.state = this.emptyState;
194 this.setState(this.state);
195 let res: LoginResponse = msg;
196 UserService.Instance.login(res);
197 this.props.history.push('/');
198 } else if (op == UserOperation.Register) {
199 this.state = this.emptyState;
200 this.setState(this.state);
201 let res: LoginResponse = msg;
202 UserService.Instance.login(res);
203 this.props.history.push('/communities');