import { Component, linkEvent } from 'inferno'; import { Subscription } from "rxjs"; import { retryWhen, delay, take } from 'rxjs/operators'; import { LoginForm, RegisterForm, LoginResponse, UserOperation } from '../interfaces'; import { WebSocketService, UserService } from '../services'; import { msgOp } from '../utils'; interface State { loginForm: LoginForm; registerForm: RegisterForm; } let emptyState: State = { loginForm: { username_or_email: undefined, password: undefined }, registerForm: { username: undefined, password: undefined, password_verify: undefined } } export class Login extends Component { private subscription: Subscription; constructor(props, context) { super(props, context); this.state = emptyState; this.subscription = WebSocketService.Instance.subject .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10)))) .subscribe( (msg) => this.parseMessage(msg), (err) => console.error(err), () => console.log("complete") ); } componentWillUnmount() { this.subscription.unsubscribe(); } render() { return (
{this.loginForm()}
{this.registerForm()}
) } loginForm() { return (

Login

Forgot your password or deleted your account? Reset your password. TODO
); } registerForm() { return (

Sign Up

); } handleLoginSubmit(i: Login, event) { event.preventDefault(); WebSocketService.Instance.login(i.state.loginForm); } handleLoginUsernameChange(i: Login, event) { i.state.loginForm.username_or_email = event.target.value; i.setState(i.state); } handleLoginPasswordChange(i: Login, event) { i.state.loginForm.password = event.target.value; i.setState(i.state); } handleRegisterSubmit(i: Login, event) { event.preventDefault(); WebSocketService.Instance.register(i.state.registerForm); } handleRegisterUsernameChange(i: Login, event) { i.state.registerForm.username = event.target.value; i.setState(i.state); } handleRegisterEmailChange(i: Login, event) { i.state.registerForm.email = event.target.value; i.setState(i.state); } handleRegisterPasswordChange(i: Login, event) { i.state.registerForm.password = event.target.value; i.setState(i.state); } handleRegisterPasswordVerifyChange(i: Login, event) { i.state.registerForm.password_verify = event.target.value; i.setState(i.state); } parseMessage(msg: any) { let op: UserOperation = msgOp(msg); if (msg.error) { alert(msg.error); return; } else { if (op == UserOperation.Register || op == UserOperation.Login) { let res: LoginResponse = msg; UserService.Instance.login(msg); this.props.history.push('/'); } } } }