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 } 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 this.subscription = WebSocketService.Instance.subject
30 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
32 msg => this.parseMessage(msg),
33 err => console.error(err),
34 () => console.log('complete')
37 WebSocketService.Instance.getSite();
40 componentWillUnmount() {
41 this.subscription.unsubscribe();
46 <div class="container">
48 <div class="col-12 col-lg-6 offset-lg-3 mb-4">
49 <h5>{i18n.t('create_community')}</h5>
51 onCreate={this.handleCommunityCreate}
52 enableNsfw={this.state.enableNsfw}
60 handleCommunityCreate(community: Community) {
61 this.props.history.push(`/c/${community.name}`);
64 parseMessage(msg: WebSocketJsonResponse) {
66 let res = wsJsonToRes(msg);
68 toast(i18n.t(msg.error), 'danger');
70 } else if (res.op == UserOperation.GetSite) {
71 let data = res.data as GetSiteResponse;
72 this.state.enableNsfw = data.site.enable_nsfw;
73 this.setState(this.state);
74 document.title = `${i18n.t('create_community')} - ${data.site.name}`;