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