1 import { Component, linkEvent } from 'inferno';
2 import { Subscription } from 'rxjs';
3 import { retryWhen, delay, take } from 'rxjs/operators';
9 } from '../interfaces';
10 import { WebSocketService, UserService } from '../services';
11 import { msgOp } from '../utils';
12 import { i18n } from '../i18next';
13 import { T } from 'inferno-i18next';
17 registerForm: RegisterForm;
18 loginLoading: boolean;
19 registerLoading: boolean;
22 export class Login extends Component<any, State> {
23 private subscription: Subscription;
27 username_or_email: undefined,
33 password_verify: undefined,
38 registerLoading: false,
41 constructor(props: any, context: any) {
42 super(props, context);
44 this.state = this.emptyState;
46 this.subscription = WebSocketService.Instance.subject
56 msg => this.parseMessage(msg),
57 err => console.error(err),
58 () => console.log('complete')
62 componentWillUnmount() {
63 this.subscription.unsubscribe();
67 document.title = `${i18n.t('login')} - ${
68 WebSocketService.Instance.site.name
74 <div class="container">
76 <div class="col-12 col-lg-6 mb-4">{this.loginForm()}</div>
77 <div class="col-12 col-lg-6">{this.registerForm()}</div>
86 <form onSubmit={linkEvent(this, this.handleLoginSubmit)}>
88 <div class="form-group row">
89 <label class="col-sm-2 col-form-label">
90 <T i18nKey="email_or_username">#</T>
92 <div class="col-sm-10">
96 value={this.state.loginForm.username_or_email}
97 onInput={linkEvent(this, this.handleLoginUsernameChange)}
103 <div class="form-group row">
104 <label class="col-sm-2 col-form-label">
105 <T i18nKey="password">#</T>
107 <div class="col-sm-10">
110 value={this.state.loginForm.password}
111 onInput={linkEvent(this, this.handleLoginPasswordChange)}
117 <div class="form-group row">
118 <div class="col-sm-10">
119 <button type="submit" class="btn btn-secondary">
120 {this.state.loginLoading ? (
121 <svg class="icon icon-spinner spin">
122 <use xlinkHref="#icon-spinner"></use>
136 <form onSubmit={linkEvent(this, this.handleRegisterSubmit)}>
138 <T i18nKey="sign_up">#</T>
140 <div class="form-group row">
141 <label class="col-sm-2 col-form-label">
142 <T i18nKey="username">#</T>
144 <div class="col-sm-10">
148 value={this.state.registerForm.username}
149 onInput={linkEvent(this, this.handleRegisterUsernameChange)}
153 pattern="[a-zA-Z0-9_]+"
157 <div class="form-group row">
158 <label class="col-sm-2 col-form-label">
159 <T i18nKey="email">#</T>
161 <div class="col-sm-10">
165 placeholder={i18n.t('optional')}
166 value={this.state.registerForm.email}
167 onInput={linkEvent(this, this.handleRegisterEmailChange)}
172 <div class="form-group row">
173 <label class="col-sm-2 col-form-label">
174 <T i18nKey="password">#</T>
176 <div class="col-sm-10">
179 value={this.state.registerForm.password}
180 onInput={linkEvent(this, this.handleRegisterPasswordChange)}
186 <div class="form-group row">
187 <label class="col-sm-2 col-form-label">
188 <T i18nKey="verify_password">#</T>
190 <div class="col-sm-10">
193 value={this.state.registerForm.password_verify}
194 onInput={linkEvent(this, this.handleRegisterPasswordVerifyChange)}
200 <div class="form-group row">
201 <div class="col-sm-10">
202 <div class="form-check">
204 class="form-check-input"
206 checked={this.state.registerForm.show_nsfw}
207 onChange={linkEvent(this, this.handleRegisterShowNsfwChange)}
209 <label class="form-check-label">
210 <T i18nKey="show_nsfw">#</T>
215 <div class="form-group row">
216 <div class="col-sm-10">
217 <button type="submit" class="btn btn-secondary">
218 {this.state.registerLoading ? (
219 <svg class="icon icon-spinner spin">
220 <use xlinkHref="#icon-spinner"></use>
232 handleLoginSubmit(i: Login, event: any) {
233 event.preventDefault();
234 i.state.loginLoading = true;
236 WebSocketService.Instance.login(i.state.loginForm);
239 handleLoginUsernameChange(i: Login, event: any) {
240 i.state.loginForm.username_or_email = event.target.value;
244 handleLoginPasswordChange(i: Login, event: any) {
245 i.state.loginForm.password = event.target.value;
249 handleRegisterSubmit(i: Login, event: any) {
250 event.preventDefault();
251 i.state.registerLoading = true;
254 WebSocketService.Instance.register(i.state.registerForm);
257 handleRegisterUsernameChange(i: Login, event: any) {
258 i.state.registerForm.username = event.target.value;
262 handleRegisterEmailChange(i: Login, event: any) {
263 i.state.registerForm.email = event.target.value;
267 handleRegisterPasswordChange(i: Login, event: any) {
268 i.state.registerForm.password = event.target.value;
272 handleRegisterPasswordVerifyChange(i: Login, event: any) {
273 i.state.registerForm.password_verify = event.target.value;
277 handleRegisterShowNsfwChange(i: Login, event: any) {
278 i.state.registerForm.show_nsfw = event.target.checked;
282 parseMessage(msg: any) {
283 let op: UserOperation = msgOp(msg);
285 alert(i18n.t(msg.error));
286 this.state = this.emptyState;
287 this.setState(this.state);
290 if (op == UserOperation.Login) {
291 this.state = this.emptyState;
292 this.setState(this.state);
293 let res: LoginResponse = msg;
294 UserService.Instance.login(res);
295 this.props.history.push('/');
296 } else if (op == UserOperation.Register) {
297 this.state = this.emptyState;
298 this.setState(this.state);
299 let res: LoginResponse = msg;
300 UserService.Instance.login(res);
301 this.props.history.push('/communities');