2021-02-22 02:39:04 +00:00
|
|
|
import { Component } from "inferno";
|
|
|
|
import { Subscription } from "rxjs";
|
|
|
|
import { CommunityForm } from "./community-form";
|
|
|
|
import { HtmlTags } from "./html-tags";
|
|
|
|
import { Spinner } from "./icon";
|
2020-09-06 16:15:25 +00:00
|
|
|
import {
|
2020-12-24 01:58:27 +00:00
|
|
|
CommunityView,
|
|
|
|
SiteView,
|
2021-02-22 02:39:04 +00:00
|
|
|
} from "lemmy-js-client";
|
2020-09-07 22:24:48 +00:00
|
|
|
import {
|
|
|
|
setIsoData,
|
|
|
|
toast,
|
|
|
|
wsSubscribe,
|
|
|
|
isBrowser,
|
2021-02-22 02:39:04 +00:00
|
|
|
} from "../utils";
|
2021-03-03 16:59:57 +00:00
|
|
|
import { UserService } from "../services";
|
2021-02-22 02:39:04 +00:00
|
|
|
import { i18n } from "../i18next";
|
2020-09-06 16:15:25 +00:00
|
|
|
|
|
|
|
interface CreateCommunityState {
|
2020-12-24 01:58:27 +00:00
|
|
|
site_view: SiteView;
|
2020-09-07 22:24:48 +00:00
|
|
|
loading: boolean;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class CreateCommunity extends Component<any, CreateCommunityState> {
|
2020-09-07 22:24:48 +00:00
|
|
|
private isoData = setIsoData(this.context);
|
2020-09-06 16:15:25 +00:00
|
|
|
private subscription: Subscription;
|
|
|
|
private emptyState: CreateCommunityState = {
|
2020-12-24 01:58:27 +00:00
|
|
|
site_view: this.isoData.site_res.site_view,
|
2020-09-07 22:24:48 +00:00
|
|
|
loading: true,
|
2020-09-06 16:15:25 +00:00
|
|
|
};
|
|
|
|
constructor(props: any, context: any) {
|
|
|
|
super(props, context);
|
|
|
|
this.handleCommunityCreate = this.handleCommunityCreate.bind(this);
|
|
|
|
this.state = this.emptyState;
|
|
|
|
|
2020-09-07 22:24:48 +00:00
|
|
|
this.parseMessage = this.parseMessage.bind(this);
|
|
|
|
this.subscription = wsSubscribe(this.parseMessage);
|
|
|
|
|
2020-09-09 20:33:40 +00:00
|
|
|
if (!UserService.Instance.user && isBrowser()) {
|
2021-02-22 02:39:04 +00:00
|
|
|
toast(i18n.t("not_logged_in"), "danger");
|
2020-09-06 16:15:25 +00:00
|
|
|
this.context.router.history.push(`/login`);
|
|
|
|
}
|
|
|
|
|
2020-09-07 22:24:48 +00:00
|
|
|
// Only fetch the data if coming from another route
|
|
|
|
if (this.isoData.path == this.context.router.route.match.url) {
|
|
|
|
this.state.loading = false;
|
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
2020-09-07 22:24:48 +00:00
|
|
|
if (isBrowser()) {
|
|
|
|
this.subscription.unsubscribe();
|
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get documentTitle(): string {
|
2021-02-22 02:39:04 +00:00
|
|
|
return `${i18n.t("create_community")} - ${this.state.site_view.site.name}`;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<div class="container">
|
2020-09-11 18:09:21 +00:00
|
|
|
<HtmlTags
|
|
|
|
title={this.documentTitle}
|
|
|
|
path={this.context.router.route.match.url}
|
|
|
|
/>
|
2020-09-07 22:24:48 +00:00
|
|
|
{this.state.loading ? (
|
|
|
|
<h5>
|
2021-02-11 20:35:27 +00:00
|
|
|
<Spinner />
|
2020-09-07 22:24:48 +00:00
|
|
|
</h5>
|
|
|
|
) : (
|
|
|
|
<div class="row">
|
|
|
|
<div class="col-12 col-lg-6 offset-lg-3 mb-4">
|
2021-02-22 02:39:04 +00:00
|
|
|
<h5>{i18n.t("create_community")}</h5>
|
2020-09-07 22:24:48 +00:00
|
|
|
<CommunityForm
|
|
|
|
onCreate={this.handleCommunityCreate}
|
2020-12-24 01:58:27 +00:00
|
|
|
enableNsfw={this.state.site_view.site.enable_nsfw}
|
2020-09-07 22:24:48 +00:00
|
|
|
/>
|
|
|
|
</div>
|
2020-09-06 16:15:25 +00:00
|
|
|
</div>
|
2020-09-07 22:24:48 +00:00
|
|
|
)}
|
2020-09-06 16:15:25 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-12-24 01:58:27 +00:00
|
|
|
handleCommunityCreate(cv: CommunityView) {
|
|
|
|
this.props.history.push(`/c/${cv.community.name}`);
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2020-12-24 01:58:27 +00:00
|
|
|
parseMessage(msg: any) {
|
2020-09-06 16:15:25 +00:00
|
|
|
if (msg.error) {
|
|
|
|
// Toast errors are already handled by community-form
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|