1 import { Component } from 'inferno';
2 import { Subscription } from 'rxjs';
3 import { retryWhen, delay, take } from 'rxjs/operators';
4 import { CommunityForm } from './community-form';
10 } from '../interfaces';
11 import { toast, wsJsonToRes } from '../utils';
12 import { WebSocketService, UserService } from '../services';
13 import { i18n } from '../i18next';
15 interface CreateCommunityState {
19 export class CreateCommunity extends Component<any, CreateCommunityState> {
20 private subscription: Subscription;
21 private emptyState: CreateCommunityState = {
24 constructor(props: any, context: any) {
25 super(props, context);
26 this.handleCommunityCreate = this.handleCommunityCreate.bind(this);
27 this.state = this.emptyState;
29 if (!UserService.Instance.user) {
30 toast(i18n.t('not_logged_in'), 'danger');
31 this.context.router.history.push(`/login`);
34 this.subscription = WebSocketService.Instance.subject
35 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
37 msg => this.parseMessage(msg),
38 err => console.error(err),
39 () => console.log('complete')
42 WebSocketService.Instance.getSite();
45 componentWillUnmount() {
46 this.subscription.unsubscribe();
51 <div class="container">
53 <div class="col-12 col-lg-6 offset-lg-3 mb-4">
54 <h5>{i18n.t('create_community')}</h5>
56 onCreate={this.handleCommunityCreate}
57 enableNsfw={this.state.enableNsfw}
65 handleCommunityCreate(community: Community) {
66 this.props.history.push(`/c/${community.name}`);
69 parseMessage(msg: WebSocketJsonResponse) {
71 let res = wsJsonToRes(msg);
73 toast(i18n.t(msg.error), 'danger');
75 } else if (res.op == UserOperation.GetSite) {
76 let data = res.data as GetSiteResponse;
77 this.state.enableNsfw = data.site.enable_nsfw;
78 this.setState(this.state);
79 document.title = `${i18n.t('create_community')} - ${data.site.name}`;