1 import { Component } from 'inferno';
2 import { Helmet } from 'inferno-helmet';
3 import { Subscription } from 'rxjs';
4 import { retryWhen, delay, take } from 'rxjs/operators';
5 import { CommunityForm } from './community-form';
12 } from '../interfaces';
13 import { toast, wsJsonToRes } from '../utils';
14 import { WebSocketService, UserService } from '../services';
15 import { i18n } from '../i18next';
17 interface CreateCommunityState {
21 export class CreateCommunity extends Component<any, CreateCommunityState> {
22 private subscription: Subscription;
23 private emptyState: CreateCommunityState = {
27 creator_id: undefined,
29 creator_name: undefined,
30 number_of_users: undefined,
31 number_of_posts: undefined,
32 number_of_comments: undefined,
33 number_of_communities: undefined,
34 enable_downvotes: undefined,
35 open_registration: undefined,
36 enable_nsfw: undefined,
39 constructor(props: any, context: any) {
40 super(props, context);
41 this.handleCommunityCreate = this.handleCommunityCreate.bind(this);
42 this.state = this.emptyState;
44 if (!UserService.Instance.user) {
45 toast(i18n.t('not_logged_in'), 'danger');
46 this.context.router.history.push(`/login`);
49 this.subscription = WebSocketService.Instance.subject
50 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
52 msg => this.parseMessage(msg),
53 err => console.error(err),
54 () => console.log('complete')
57 WebSocketService.Instance.getSite();
60 componentWillUnmount() {
61 this.subscription.unsubscribe();
64 get documentTitle(): string {
65 if (this.state.site.name) {
66 return `${i18n.t('create_community')} - ${this.state.site.name}`;
74 <div class="container">
75 <Helmet title={this.documentTitle} />
77 <div class="col-12 col-lg-6 offset-lg-3 mb-4">
78 <h5>{i18n.t('create_community')}</h5>
80 onCreate={this.handleCommunityCreate}
81 enableNsfw={this.state.site.enable_nsfw}
89 handleCommunityCreate(community: Community) {
90 this.props.history.push(`/c/${community.name}`);
93 parseMessage(msg: WebSocketJsonResponse) {
95 let res = wsJsonToRes(msg);
97 // Toast errors are already handled by community-form
99 } else if (res.op == UserOperation.GetSite) {
100 let data = res.data as GetSiteResponse;
101 this.state.site = data.site;
102 this.setState(this.state);