]> Untitled Git - lemmy-ui.git/blob - src/shared/components/community/create-community.tsx
Adding Community Language fixes. #783 (#868)
[lemmy-ui.git] / src / shared / components / community / create-community.tsx
1 import { None, Some } from "@sniptt/monads";
2 import { Component } from "inferno";
3 import { CommunityView, GetSiteResponse } from "lemmy-js-client";
4 import { Subscription } from "rxjs";
5 import { i18n } from "../../i18next";
6 import { UserService } from "../../services";
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   private emptyState: 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     this.state = this.emptyState;
34
35     this.parseMessage = this.parseMessage.bind(this);
36     this.subscription = wsSubscribe(this.parseMessage);
37
38     if (UserService.Instance.myUserInfo.isNone() && isBrowser()) {
39       toast(i18n.t("not_logged_in"), "danger");
40       this.context.router.history.push(`/login`);
41     }
42   }
43
44   componentWillUnmount() {
45     if (isBrowser()) {
46       this.subscription.unsubscribe();
47     }
48   }
49
50   get documentTitle(): string {
51     return `${i18n.t("create_community")} - ${
52       this.state.siteRes.site_view.site.name
53     }`;
54   }
55
56   render() {
57     return (
58       <div className="container-lg">
59         <HtmlTags
60           title={this.documentTitle}
61           path={this.context.router.route.match.url}
62           description={None}
63           image={None}
64         />
65         {this.state.loading ? (
66           <h5>
67             <Spinner large />
68           </h5>
69         ) : (
70           <div className="row">
71             <div className="col-12 col-lg-6 offset-lg-3 mb-4">
72               <h5>{i18n.t("create_community")}</h5>
73               <CommunityForm
74                 community_view={None}
75                 onCreate={this.handleCommunityCreate}
76                 enableNsfw={enableNsfw(this.state.siteRes)}
77                 allLanguages={this.state.siteRes.all_languages}
78                 siteLanguages={this.state.siteRes.discussion_languages}
79                 communityLanguages={Some(
80                   this.state.siteRes.discussion_languages
81                 )}
82               />
83             </div>
84           </div>
85         )}
86       </div>
87     );
88   }
89
90   handleCommunityCreate(cv: CommunityView) {
91     this.props.history.push(`/c/${cv.community.name}`);
92   }
93
94   parseMessage(msg: any) {
95     if (msg.error) {
96       toast(i18n.t(msg.error), "danger");
97     }
98   }
99 }