1 import { Component } from 'inferno';
2 import { Subscription } from 'rxjs';
3 import { CommunityForm } from './community-form';
4 import { HtmlTags } from './html-tags';
10 ListCategoriesResponse,
12 } from 'lemmy-js-client';
20 import { WebSocketService, UserService } from '../services';
21 import { i18n } from '../i18next';
22 import { InitialFetchRequest } from 'shared/interfaces';
24 interface CreateCommunityState {
26 categories: Category[];
30 export class CreateCommunity extends Component<any, CreateCommunityState> {
31 private isoData = setIsoData(this.context);
32 private subscription: Subscription;
33 private emptyState: CreateCommunityState = {
34 site: this.isoData.site.site,
38 constructor(props: any, context: any) {
39 super(props, context);
40 this.handleCommunityCreate = this.handleCommunityCreate.bind(this);
41 this.state = this.emptyState;
43 this.parseMessage = this.parseMessage.bind(this);
44 this.subscription = wsSubscribe(this.parseMessage);
46 if (!UserService.Instance.user && isBrowser()) {
47 toast(i18n.t('not_logged_in'), 'danger');
48 this.context.router.history.push(`/login`);
51 // Only fetch the data if coming from another route
52 if (this.isoData.path == this.context.router.route.match.url) {
53 this.state.categories = this.isoData.routeData[0].categories;
54 this.state.loading = false;
56 WebSocketService.Instance.listCategories();
60 componentWillUnmount() {
62 this.subscription.unsubscribe();
66 get documentTitle(): string {
67 return `${i18n.t('create_community')} - ${this.state.site.name}`;
72 <div class="container">
74 title={this.documentTitle}
75 path={this.context.router.route.match.url}
77 {this.state.loading ? (
79 <svg class="icon icon-spinner spin">
80 <use xlinkHref="#icon-spinner"></use>
85 <div class="col-12 col-lg-6 offset-lg-3 mb-4">
86 <h5>{i18n.t('create_community')}</h5>
88 categories={this.state.categories}
89 onCreate={this.handleCommunityCreate}
90 enableNsfw={this.state.site.enable_nsfw}
99 handleCommunityCreate(community: Community) {
100 this.props.history.push(`/c/${community.name}`);
103 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
104 return [req.client.listCategories()];
107 parseMessage(msg: WebSocketJsonResponse) {
109 let res = wsJsonToRes(msg);
111 // Toast errors are already handled by community-form
113 } else if (res.op == UserOperation.ListCategories) {
114 let data = res.data as ListCategoriesResponse;
115 this.state.categories = data.categories;
116 this.state.loading = false;
117 this.setState(this.state);