import { Component, linkEvent } from "inferno"; import { Helmet } from "inferno-helmet"; import { GetSiteResponse, LoginResponse, Register, UserOperation, wsJsonToRes, wsUserOp, } from "lemmy-js-client"; import { Subscription } from "rxjs"; import { delay, retryWhen, take } from "rxjs/operators"; import { i18n } from "../../i18next"; import { UserService, WebSocketService } from "../../services"; import { setIsoData, toast, wsClient } from "../../utils"; import { Spinner } from "../common/icon"; import { SiteForm } from "./site-form"; interface State { form: { username?: string; email?: string; password?: string; password_verify?: string; show_nsfw: boolean; captcha_uuid?: string; captcha_answer?: string; honeypot?: string; answer?: string; }; doneRegisteringUser: boolean; userLoading: boolean; siteRes: GetSiteResponse; } export class Setup extends Component { private subscription: Subscription; private isoData = setIsoData(this.context); state: State = { form: { show_nsfw: true, }, doneRegisteringUser: !!UserService.Instance.myUserInfo, userLoading: false, siteRes: this.isoData.site_res, }; constructor(props: any, context: any) { super(props, context); 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.setState({ userLoading: true }); event.preventDefault(); const cForm = i.state.form; if (cForm.username && cForm.password && cForm.password_verify) { const form: Register = { username: cForm.username, password: cForm.password, password_verify: cForm.password_verify, email: cForm.email, show_nsfw: cForm.show_nsfw, captcha_uuid: cForm.captcha_uuid, captcha_answer: cForm.captcha_answer, honeypot: cForm.honeypot, answer: cForm.answer, }; WebSocketService.Instance.send(wsClient.register(form)); } } handleRegisterUsernameChange(i: Setup, event: any) { i.state.form.username = event.target.value; i.setState(i.state); } handleRegisterEmailChange(i: Setup, event: any) { i.state.form.email = event.target.value; i.setState(i.state); } handleRegisterPasswordChange(i: Setup, event: any) { i.state.form.password = event.target.value; i.setState(i.state); } handleRegisterPasswordVerifyChange(i: Setup, event: any) { i.state.form.password_verify = event.target.value; i.setState(i.state); } parseMessage(msg: any) { const op = wsUserOp(msg); if (msg.error) { toast(i18n.t(msg.error), "danger"); this.setState({ userLoading: false }); return; } else if (op == UserOperation.Register) { const data = wsJsonToRes(msg); this.setState({ userLoading: false }); UserService.Instance.login(data); if (UserService.Instance.jwtInfo) { this.setState({ doneRegisteringUser: true }); } } else if (op == UserOperation.CreateSite) { window.location.href = "/"; } } }