import { Component, linkEvent } from 'inferno'; import { Subscription } from 'rxjs'; import { retryWhen, delay, take } from 'rxjs/operators'; import { LoginForm, RegisterForm, LoginResponse, UserOperation, PasswordResetForm, GetSiteResponse, } from '../interfaces'; import { WebSocketService, UserService } from '../services'; import { msgOp, validEmail, toast } from '../utils'; import { i18n } from '../i18next'; import { T } from 'inferno-i18next'; interface State { loginForm: LoginForm; registerForm: RegisterForm; loginLoading: boolean; registerLoading: boolean; enable_nsfw: boolean; } export class Login extends Component { private subscription: Subscription; emptyState: State = { loginForm: { username_or_email: undefined, password: undefined, }, registerForm: { username: undefined, password: undefined, password_verify: undefined, admin: false, show_nsfw: false, }, loginLoading: false, registerLoading: false, enable_nsfw: undefined, }; constructor(props: any, context: any) { super(props, context); this.state = this.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') ); WebSocketService.Instance.getSite(); } componentWillUnmount() { this.subscription.unsubscribe(); } render() { return (
{this.loginForm()}
{this.registerForm()}
); } loginForm() { return (
Login
); } registerForm() { return (
#
{this.state.enable_nsfw && (
)}
); } handleLoginSubmit(i: Login, event: any) { event.preventDefault(); i.state.loginLoading = true; i.setState(i.state); WebSocketService.Instance.login(i.state.loginForm); } handleLoginUsernameChange(i: Login, event: any) { i.state.loginForm.username_or_email = event.target.value; i.setState(i.state); } handleLoginPasswordChange(i: Login, event: any) { i.state.loginForm.password = event.target.value; i.setState(i.state); } handleRegisterSubmit(i: Login, event: any) { event.preventDefault(); i.state.registerLoading = true; i.setState(i.state); WebSocketService.Instance.register(i.state.registerForm); } handleRegisterUsernameChange(i: Login, event: any) { i.state.registerForm.username = event.target.value; i.setState(i.state); } handleRegisterEmailChange(i: Login, event: any) { i.state.registerForm.email = event.target.value; if (i.state.registerForm.email == '') { i.state.registerForm.email = undefined; } i.setState(i.state); } handleRegisterPasswordChange(i: Login, event: any) { i.state.registerForm.password = event.target.value; i.setState(i.state); } handleRegisterPasswordVerifyChange(i: Login, event: any) { i.state.registerForm.password_verify = event.target.value; i.setState(i.state); } handleRegisterShowNsfwChange(i: Login, event: any) { i.state.registerForm.show_nsfw = event.target.checked; i.setState(i.state); } handlePasswordReset(i: Login) { event.preventDefault(); let resetForm: PasswordResetForm = { email: i.state.loginForm.username_or_email, }; WebSocketService.Instance.passwordReset(resetForm); } parseMessage(msg: any) { let op: UserOperation = msgOp(msg); if (msg.error) { toast(i18n.t(msg.error), 'danger'); this.state = this.emptyState; this.setState(this.state); return; } else { if (op == UserOperation.Login) { this.state = this.emptyState; this.setState(this.state); let res: LoginResponse = msg; UserService.Instance.login(res); toast(i18n.t('logged_in')); this.props.history.push('/'); } else if (op == UserOperation.Register) { this.state = this.emptyState; this.setState(this.state); let res: LoginResponse = msg; UserService.Instance.login(res); this.props.history.push('/communities'); } else if (op == UserOperation.PasswordReset) { toast(i18n.t('reset_password_mail_sent')); } else if (op == UserOperation.GetSite) { let res: GetSiteResponse = msg; this.state.enable_nsfw = res.site.enable_nsfw; this.setState(this.state); document.title = `${i18n.t('login')} - ${ WebSocketService.Instance.site.name }`; } } } }