]> Untitled Git - lemmy-ui.git/blob - src/shared/components/community/create-community.tsx
component classes v2
[lemmy-ui.git] / src / shared / components / community / create-community.tsx
1 import { Component } from "inferno";
2 import {
3   CreateCommunity as CreateCommunityI,
4   GetSiteResponse,
5 } from "lemmy-js-client";
6 import { i18n } from "../../i18next";
7 import { HttpService } from "../../services/HttpService";
8 import { enableNsfw, setIsoData } from "../../utils";
9 import { HtmlTags } from "../common/html-tags";
10 import { CommunityForm } from "./community-form";
11
12 interface CreateCommunityState {
13   siteRes: GetSiteResponse;
14   loading: boolean;
15 }
16
17 export class CreateCommunity extends Component<any, CreateCommunityState> {
18   private isoData = setIsoData(this.context);
19   state: CreateCommunityState = {
20     siteRes: this.isoData.site_res,
21     loading: false,
22   };
23   constructor(props: any, context: any) {
24     super(props, context);
25     this.handleCommunityCreate = this.handleCommunityCreate.bind(this);
26   }
27
28   get documentTitle(): string {
29     return `${i18n.t("create_community")} - ${
30       this.state.siteRes.site_view.site.name
31     }`;
32   }
33
34   render() {
35     return (
36       <div className="create-community container-lg">
37         <HtmlTags
38           title={this.documentTitle}
39           path={this.context.router.route.match.url}
40         />
41         <div className="row">
42           <div className="col-12 col-lg-6 offset-lg-3 mb-4">
43             <h5>{i18n.t("create_community")}</h5>
44             <CommunityForm
45               onUpsertCommunity={this.handleCommunityCreate}
46               enableNsfw={enableNsfw(this.state.siteRes)}
47               allLanguages={this.state.siteRes.all_languages}
48               siteLanguages={this.state.siteRes.discussion_languages}
49               communityLanguages={this.state.siteRes.discussion_languages}
50               loading={this.state.loading}
51             />
52           </div>
53         </div>
54       </div>
55     );
56   }
57
58   async handleCommunityCreate(form: CreateCommunityI) {
59     this.setState({ loading: true });
60
61     const res = await HttpService.client.createCommunity(form);
62
63     if (res.state === "success") {
64       const name = res.data.community_view.community.name;
65       this.props.history.replace(`/c/${name}`);
66     } else {
67       this.setState({ loading: false });
68     }
69   }
70 }