import { Component, linkEvent } from "inferno"; import { Helmet } from "inferno-helmet"; import { CreateSite, GetSiteResponse, LoginResponse, Register, } from "lemmy-js-client"; import { i18n } from "../../i18next"; import { UserService } from "../../services"; import { HttpService, RequestState } from "../../services/HttpService"; import { fetchThemeList, setIsoData } 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; registerRes: RequestState; themeList: string[]; siteRes: GetSiteResponse; } export class Setup extends Component { private isoData = setIsoData(this.context); state: State = { registerRes: { state: "empty" }, themeList: [], form: { show_nsfw: true, }, doneRegisteringUser: !!UserService.Instance.myUserInfo, siteRes: this.isoData.site_res, }; constructor(props: any, context: any) { super(props, context); this.handleCreateSite = this.handleCreateSite.bind(this); } async componentDidMount() { this.setState({ themeList: await fetchThemeList() }); } 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")}
); } async handleRegisterSubmit(i: Setup, event: any) { event.preventDefault(); i.setState({ registerRes: { state: "loading" } }); const { username, password_verify, password, email, show_nsfw, captcha_uuid, captcha_answer, honeypot, answer, } = i.state.form; if (username && password && password_verify) { const form: Register = { username, password, password_verify, email, show_nsfw, captcha_uuid, captcha_answer, honeypot, answer, }; i.setState({ registerRes: await HttpService.client.register(form), }); if (i.state.registerRes.state == "success") { const data = i.state.registerRes.data; UserService.Instance.login(data); if (UserService.Instance.jwtInfo) { i.setState({ doneRegisteringUser: true }); } } } } async handleCreateSite(form: CreateSite) { const createRes = await HttpService.client.createSite(form); if (createRes.state === "success") { this.props.history.replace("/"); location.reload(); } } handleRegisterUsernameChange(i: Setup, event: any) { i.state.form.username = event.target.value.trim(); 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); } }