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