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