import { Component, linkEvent } from 'inferno'; import { Helmet } from 'inferno-helmet'; import { Subscription } from 'rxjs'; import { retryWhen, delay, take } from 'rxjs/operators'; import { RegisterForm, LoginResponse, UserOperation, WebSocketJsonResponse, } from 'lemmy-js-client'; import { WebSocketService, UserService } from '../services'; import { wsJsonToRes, toast } from '../utils'; import { SiteForm } from './site-form'; import { i18n } from '../i18next'; interface State { userForm: RegisterForm; doneRegisteringUser: boolean; userLoading: boolean; } export class Setup extends Component { private subscription: Subscription; private emptyState: State = { userForm: { username: undefined, password: undefined, password_verify: undefined, admin: true, show_nsfw: true, // The first admin signup doesn't need a captcha captcha_uuid: '', captcha_answer: '', }, doneRegisteringUser: false, userLoading: false, }; 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') ); } componentWillUnmount() { this.subscription.unsubscribe(); } get documentTitle(): string { return `${i18n.t('setup')} - Lemmy`; } render() { return (

{i18n.t('lemmy_instance_setup')}

{!this.state.doneRegisteringUser ? ( this.registerUser() ) : ( )}
); } registerUser() { return (
{i18n.t('setup_admin')}
); } handleRegisterSubmit(i: Setup, event: any) { event.preventDefault(); i.state.userLoading = true; i.setState(i.state); event.preventDefault(); WebSocketService.Instance.register(i.state.userForm); } handleRegisterUsernameChange(i: Setup, event: any) { i.state.userForm.username = event.target.value; i.setState(i.state); } handleRegisterEmailChange(i: Setup, event: any) { i.state.userForm.email = event.target.value; i.setState(i.state); } handleRegisterPasswordChange(i: Setup, event: any) { i.state.userForm.password = event.target.value; i.setState(i.state); } handleRegisterPasswordVerifyChange(i: Setup, event: any) { i.state.userForm.password_verify = event.target.value; i.setState(i.state); } parseMessage(msg: WebSocketJsonResponse) { let res = wsJsonToRes(msg); if (msg.error) { toast(i18n.t(msg.error), 'danger'); this.state.userLoading = false; this.setState(this.state); return; } else if (res.op == UserOperation.Register) { let data = res.data as LoginResponse; this.state.userLoading = false; this.state.doneRegisteringUser = true; UserService.Instance.login(data); this.setState(this.state); } else if (res.op == UserOperation.CreateSite) { this.props.history.push('/'); } } }