lemmy-ui/src/shared/components/community/create-community.tsx

96 lines
2.5 KiB
TypeScript
Raw Normal View History

import { None } from "@sniptt/monads";
2021-02-22 02:39:04 +00:00
import { Component } from "inferno";
import { CommunityView, GetSiteResponse } from "lemmy-js-client";
2021-02-22 02:39:04 +00:00
import { Subscription } from "rxjs";
import { i18n } from "../../i18next";
import { UserService } from "../../services";
import {
enableNsfw,
isBrowser,
setIsoData,
toast,
wsSubscribe,
} from "../../utils";
import { HtmlTags } from "../common/html-tags";
import { Spinner } from "../common/icon";
2021-02-22 02:39:04 +00:00
import { CommunityForm } from "./community-form";
interface CreateCommunityState {
siteRes: GetSiteResponse;
loading: boolean;
}
export class CreateCommunity extends Component<any, CreateCommunityState> {
private isoData = setIsoData(this.context);
private subscription: Subscription;
private emptyState: CreateCommunityState = {
siteRes: this.isoData.site_res,
2021-03-04 03:42:11 +00:00
loading: false,
};
constructor(props: any, context: any) {
super(props, context);
this.handleCommunityCreate = this.handleCommunityCreate.bind(this);
this.state = this.emptyState;
this.parseMessage = this.parseMessage.bind(this);
this.subscription = wsSubscribe(this.parseMessage);
if (UserService.Instance.myUserInfo.isNone() && isBrowser()) {
2021-02-22 02:39:04 +00:00
toast(i18n.t("not_logged_in"), "danger");
this.context.router.history.push(`/login`);
}
}
componentWillUnmount() {
if (isBrowser()) {
this.subscription.unsubscribe();
}
}
get documentTitle(): string {
return this.state.siteRes.site_view.match({
some: siteView => `${i18n.t("create_community")} - ${siteView.site.name}`,
none: "",
});
}
render() {
return (
<div class="container">
2020-09-11 18:09:21 +00:00
<HtmlTags
title={this.documentTitle}
path={this.context.router.route.match.url}
description={None}
image={None}
2020-09-11 18:09:21 +00:00
/>
{this.state.loading ? (
<h5>
2021-07-17 20:21:31 +00:00
<Spinner large />
</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>
<CommunityForm
community_view={None}
onCreate={this.handleCommunityCreate}
enableNsfw={enableNsfw(this.state.siteRes)}
/>
</div>
</div>
)}
</div>
);
}
2020-12-24 01:58:27 +00:00
handleCommunityCreate(cv: CommunityView) {
this.props.history.push(`/c/${cv.community.name}`);
}
2020-12-24 01:58:27 +00:00
parseMessage(msg: any) {
if (msg.error) {
toast(i18n.t(msg.error), "danger");
}
}
}