2021-02-22 02:39:04 +00:00
|
|
|
import { Component, linkEvent } from "inferno";
|
|
|
|
import { Helmet } from "inferno-helmet";
|
2022-06-21 21:42:29 +00:00
|
|
|
import {
|
2023-06-14 12:20:40 +00:00
|
|
|
CreateSite,
|
2022-11-09 19:53:07 +00:00
|
|
|
GetSiteResponse,
|
2022-06-21 21:42:29 +00:00
|
|
|
LoginResponse,
|
|
|
|
Register,
|
|
|
|
} from "lemmy-js-client";
|
2021-07-17 20:42:55 +00:00
|
|
|
import { i18n } from "../../i18next";
|
2023-06-14 12:20:40 +00:00
|
|
|
import { UserService } from "../../services";
|
|
|
|
import { HttpService, RequestState } from "../../services/HttpService";
|
|
|
|
import { fetchThemeList, setIsoData } from "../../utils";
|
2021-07-17 20:42:55 +00:00
|
|
|
import { Spinner } from "../common/icon";
|
2021-02-22 02:39:04 +00:00
|
|
|
import { SiteForm } from "./site-form";
|
2020-09-06 16:15:25 +00:00
|
|
|
|
|
|
|
interface State {
|
2023-01-04 16:56:24 +00:00
|
|
|
form: {
|
|
|
|
username?: string;
|
|
|
|
email?: string;
|
|
|
|
password?: string;
|
|
|
|
password_verify?: string;
|
|
|
|
show_nsfw: boolean;
|
|
|
|
captcha_uuid?: string;
|
|
|
|
captcha_answer?: string;
|
|
|
|
honeypot?: string;
|
|
|
|
answer?: string;
|
|
|
|
};
|
2020-09-06 16:15:25 +00:00
|
|
|
doneRegisteringUser: boolean;
|
2023-06-14 12:20:40 +00:00
|
|
|
registerRes: RequestState<LoginResponse>;
|
|
|
|
themeList: string[];
|
2022-11-09 19:53:07 +00:00
|
|
|
siteRes: GetSiteResponse;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class Setup extends Component<any, State> {
|
2022-11-09 19:53:07 +00:00
|
|
|
private isoData = setIsoData(this.context);
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2023-01-04 16:56:24 +00:00
|
|
|
state: State = {
|
2023-06-14 12:20:40 +00:00
|
|
|
registerRes: { state: "empty" },
|
|
|
|
themeList: [],
|
2023-01-04 16:56:24 +00:00
|
|
|
form: {
|
2020-09-06 16:15:25 +00:00
|
|
|
show_nsfw: true,
|
2023-01-04 16:56:24 +00:00
|
|
|
},
|
|
|
|
doneRegisteringUser: !!UserService.Instance.myUserInfo,
|
2022-11-09 19:53:07 +00:00
|
|
|
siteRes: this.isoData.site_res,
|
2020-09-06 16:15:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props: any, context: any) {
|
|
|
|
super(props, context);
|
|
|
|
|
2023-06-14 12:20:40 +00:00
|
|
|
this.handleCreateSite = this.handleCreateSite.bind(this);
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2023-06-14 12:20:40 +00:00
|
|
|
async componentDidMount() {
|
|
|
|
this.setState({ themeList: await fetchThemeList() });
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get documentTitle(): string {
|
2021-02-22 02:39:04 +00:00
|
|
|
return `${i18n.t("setup")} - Lemmy`;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
2022-10-03 18:16:36 +00:00
|
|
|
<div className="container-lg">
|
2020-09-06 16:15:25 +00:00
|
|
|
<Helmet title={this.documentTitle} />
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="row">
|
|
|
|
<div className="col-12 offset-lg-3 col-lg-6">
|
2021-02-22 02:39:04 +00:00
|
|
|
<h3>{i18n.t("lemmy_instance_setup")}</h3>
|
2020-09-06 16:15:25 +00:00
|
|
|
{!this.state.doneRegisteringUser ? (
|
|
|
|
this.registerUser()
|
|
|
|
) : (
|
2023-06-14 12:20:40 +00:00
|
|
|
<SiteForm
|
|
|
|
showLocal
|
|
|
|
onSaveSite={this.handleCreateSite}
|
|
|
|
siteRes={this.state.siteRes}
|
|
|
|
themeList={this.state.themeList}
|
2023-06-16 15:52:47 +00:00
|
|
|
loading={false}
|
2023-06-14 12:20:40 +00:00
|
|
|
/>
|
2020-09-06 16:15:25 +00:00
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
registerUser() {
|
|
|
|
return (
|
|
|
|
<form onSubmit={linkEvent(this, this.handleRegisterSubmit)}>
|
2021-02-22 02:39:04 +00:00
|
|
|
<h5>{i18n.t("setup_admin")}</h5>
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="form-group row">
|
|
|
|
<label className="col-sm-2 col-form-label" htmlFor="username">
|
2021-02-22 02:39:04 +00:00
|
|
|
{i18n.t("username")}
|
2020-09-06 16:15:25 +00:00
|
|
|
</label>
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="col-sm-10">
|
2020-09-06 16:15:25 +00:00
|
|
|
<input
|
|
|
|
type="text"
|
2022-09-22 15:03:35 +00:00
|
|
|
className="form-control"
|
2020-09-06 16:15:25 +00:00
|
|
|
id="username"
|
2023-01-04 16:56:24 +00:00
|
|
|
value={this.state.form.username}
|
2020-09-06 16:15:25 +00:00
|
|
|
onInput={linkEvent(this, this.handleRegisterUsernameChange)}
|
|
|
|
required
|
|
|
|
minLength={3}
|
|
|
|
pattern="[a-zA-Z0-9_]+"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="form-group row">
|
|
|
|
<label className="col-sm-2 col-form-label" htmlFor="email">
|
2021-02-22 02:39:04 +00:00
|
|
|
{i18n.t("email")}
|
2020-09-06 16:15:25 +00:00
|
|
|
</label>
|
|
|
|
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="col-sm-10">
|
2020-09-06 16:15:25 +00:00
|
|
|
<input
|
|
|
|
type="email"
|
|
|
|
id="email"
|
2022-09-22 15:03:35 +00:00
|
|
|
className="form-control"
|
2021-02-22 02:39:04 +00:00
|
|
|
placeholder={i18n.t("optional")}
|
2023-01-04 16:56:24 +00:00
|
|
|
value={this.state.form.email}
|
2020-09-06 16:15:25 +00:00
|
|
|
onInput={linkEvent(this, this.handleRegisterEmailChange)}
|
|
|
|
minLength={3}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="form-group row">
|
|
|
|
<label className="col-sm-2 col-form-label" htmlFor="password">
|
2021-02-22 02:39:04 +00:00
|
|
|
{i18n.t("password")}
|
2020-09-06 16:15:25 +00:00
|
|
|
</label>
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="col-sm-10">
|
2020-09-06 16:15:25 +00:00
|
|
|
<input
|
|
|
|
type="password"
|
|
|
|
id="password"
|
2023-01-04 16:56:24 +00:00
|
|
|
value={this.state.form.password}
|
2020-09-06 16:15:25 +00:00
|
|
|
onInput={linkEvent(this, this.handleRegisterPasswordChange)}
|
2022-09-22 15:03:35 +00:00
|
|
|
className="form-control"
|
2020-09-06 16:15:25 +00:00
|
|
|
required
|
2021-11-16 14:46:22 +00:00
|
|
|
autoComplete="new-password"
|
|
|
|
minLength={10}
|
2021-03-01 17:38:03 +00:00
|
|
|
maxLength={60}
|
2020-09-06 16:15:25 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="form-group row">
|
|
|
|
<label className="col-sm-2 col-form-label" htmlFor="verify-password">
|
2021-02-22 02:39:04 +00:00
|
|
|
{i18n.t("verify_password")}
|
2020-09-06 16:15:25 +00:00
|
|
|
</label>
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="col-sm-10">
|
2020-09-06 16:15:25 +00:00
|
|
|
<input
|
|
|
|
type="password"
|
|
|
|
id="verify-password"
|
2023-01-04 16:56:24 +00:00
|
|
|
value={this.state.form.password_verify}
|
2020-09-06 16:15:25 +00:00
|
|
|
onInput={linkEvent(this, this.handleRegisterPasswordVerifyChange)}
|
2022-09-22 15:03:35 +00:00
|
|
|
className="form-control"
|
2020-09-06 16:15:25 +00:00
|
|
|
required
|
2021-11-16 14:46:22 +00:00
|
|
|
autoComplete="new-password"
|
|
|
|
minLength={10}
|
2021-03-01 17:38:03 +00:00
|
|
|
maxLength={60}
|
2020-09-06 16:15:25 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="form-group row">
|
|
|
|
<div className="col-sm-10">
|
|
|
|
<button type="submit" className="btn btn-secondary">
|
2023-06-14 12:20:40 +00:00
|
|
|
{this.state.registerRes.state == "loading" ? (
|
|
|
|
<Spinner />
|
|
|
|
) : (
|
|
|
|
i18n.t("sign_up")
|
|
|
|
)}
|
2020-09-06 16:15:25 +00:00
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-06-14 12:20:40 +00:00
|
|
|
async handleRegisterSubmit(i: Setup, event: any) {
|
2020-09-06 16:15:25 +00:00
|
|
|
event.preventDefault();
|
2023-06-14 12:20:40 +00:00
|
|
|
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) {
|
2023-06-05 21:31:12 +00:00
|
|
|
const form: Register = {
|
2023-06-14 12:20:40 +00:00
|
|
|
username,
|
|
|
|
password,
|
|
|
|
password_verify,
|
|
|
|
email,
|
|
|
|
show_nsfw,
|
|
|
|
captcha_uuid,
|
|
|
|
captcha_answer,
|
|
|
|
honeypot,
|
|
|
|
answer,
|
2023-01-04 16:56:24 +00:00
|
|
|
};
|
2023-06-14 12:20:40 +00:00
|
|
|
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();
|
2023-01-04 16:56:24 +00:00
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleRegisterUsernameChange(i: Setup, event: any) {
|
2023-06-14 13:47:33 +00:00
|
|
|
i.state.form.username = event.target.value.trim();
|
2020-09-06 16:15:25 +00:00
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleRegisterEmailChange(i: Setup, event: any) {
|
2023-01-04 16:56:24 +00:00
|
|
|
i.state.form.email = event.target.value;
|
2020-09-06 16:15:25 +00:00
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleRegisterPasswordChange(i: Setup, event: any) {
|
2023-01-04 16:56:24 +00:00
|
|
|
i.state.form.password = event.target.value;
|
2020-09-06 16:15:25 +00:00
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleRegisterPasswordVerifyChange(i: Setup, event: any) {
|
2023-01-04 16:56:24 +00:00
|
|
|
i.state.form.password_verify = event.target.value;
|
2020-09-06 16:15:25 +00:00
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
}
|