]> Untitled Git - lemmy-ui.git/blob - src/shared/components/community/create-community.tsx
Use http client (#1081)
[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 }
15
16 export class CreateCommunity extends Component<any, CreateCommunityState> {
17   private isoData = setIsoData(this.context);
18   state: CreateCommunityState = {
19     siteRes: this.isoData.site_res,
20   };
21   constructor(props: any, context: any) {
22     super(props, context);
23     this.handleCommunityCreate = this.handleCommunityCreate.bind(this);
24   }
25
26   get documentTitle(): string {
27     return `${i18n.t("create_community")} - ${
28       this.state.siteRes.site_view.site.name
29     }`;
30   }
31
32   render() {
33     return (
34       <div className="container-lg">
35         <HtmlTags
36           title={this.documentTitle}
37           path={this.context.router.route.match.url}
38         />
39         <div className="row">
40           <div className="col-12 col-lg-6 offset-lg-3 mb-4">
41             <h5>{i18n.t("create_community")}</h5>
42             <CommunityForm
43               onUpsertCommunity={this.handleCommunityCreate}
44               enableNsfw={enableNsfw(this.state.siteRes)}
45               allLanguages={this.state.siteRes.all_languages}
46               siteLanguages={this.state.siteRes.discussion_languages}
47               communityLanguages={this.state.siteRes.discussion_languages}
48             />
49           </div>
50         </div>
51       </div>
52     );
53   }
54
55   async handleCommunityCreate(form: CreateCommunityI) {
56     const res = await HttpService.client.createCommunity(form);
57     if (res.state === "success") {
58       const name = res.data.community_view.community.name;
59       this.props.history.replace(`/c/${name}`);
60     }
61   }
62 }