]> Untitled Git - lemmy-ui.git/blob - src/shared/components/community/create-community.tsx
Merge branch 'main' into rate-limiting-tab
[lemmy-ui.git] / src / shared / components / community / create-community.tsx
1 import { Component } from "inferno";
2 import { CommunityView, GetSiteResponse } from "lemmy-js-client";
3 import { Subscription } from "rxjs";
4 import { i18n } from "../../i18next";
5 import {
6   enableNsfw,
7   isBrowser,
8   setIsoData,
9   toast,
10   wsSubscribe,
11 } from "../../utils";
12 import { HtmlTags } from "../common/html-tags";
13 import { Spinner } from "../common/icon";
14 import { CommunityForm } from "./community-form";
15
16 interface CreateCommunityState {
17   siteRes: GetSiteResponse;
18   loading: boolean;
19 }
20
21 export class CreateCommunity extends Component<any, CreateCommunityState> {
22   private isoData = setIsoData(this.context);
23   private subscription?: Subscription;
24   state: CreateCommunityState = {
25     siteRes: this.isoData.site_res,
26     loading: false,
27   };
28   constructor(props: any, context: any) {
29     super(props, context);
30     this.handleCommunityCreate = this.handleCommunityCreate.bind(this);
31
32     this.parseMessage = this.parseMessage.bind(this);
33     this.subscription = wsSubscribe(this.parseMessage);
34   }
35
36   componentWillUnmount() {
37     if (isBrowser()) {
38       this.subscription?.unsubscribe();
39     }
40   }
41
42   get documentTitle(): string {
43     return `${i18n.t("create_community")} - ${
44       this.state.siteRes.site_view.site.name
45     }`;
46   }
47
48   render() {
49     return (
50       <div className="container-lg">
51         <HtmlTags
52           title={this.documentTitle}
53           path={this.context.router.route.match.url}
54         />
55         {this.state.loading ? (
56           <h5>
57             <Spinner large />
58           </h5>
59         ) : (
60           <div className="row">
61             <div className="col-12 col-lg-6 offset-lg-3 mb-4">
62               <h5>{i18n.t("create_community")}</h5>
63               <CommunityForm
64                 onCreate={this.handleCommunityCreate}
65                 enableNsfw={enableNsfw(this.state.siteRes)}
66                 allLanguages={this.state.siteRes.all_languages}
67                 siteLanguages={this.state.siteRes.discussion_languages}
68                 communityLanguages={this.state.siteRes.discussion_languages}
69               />
70             </div>
71           </div>
72         )}
73       </div>
74     );
75   }
76
77   handleCommunityCreate(cv: CommunityView) {
78     this.props.history.push(`/c/${cv.community.name}`);
79   }
80
81   parseMessage(msg: any) {
82     if (msg.error) {
83       toast(i18n.t(msg.error), "danger");
84     }
85   }
86 }