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