]> Untitled Git - lemmy.git/blob - ui/src/components/create-community.tsx
Merge branch 'html_titles'
[lemmy.git] / ui / src / components / create-community.tsx
1 import { Component } from 'inferno';
2 import { Subscription } from 'rxjs';
3 import { retryWhen, delay, take } from 'rxjs/operators';
4 import { CommunityForm } from './community-form';
5 import {
6   Community,
7   UserOperation,
8   WebSocketJsonResponse,
9   GetSiteResponse,
10 } from '../interfaces';
11 import { toast, wsJsonToRes } from '../utils';
12 import { WebSocketService } from '../services';
13 import { i18n } from '../i18next';
14
15 interface CreateCommunityState {
16   enableNsfw: boolean;
17 }
18
19 export class CreateCommunity extends Component<any, CreateCommunityState> {
20   private subscription: Subscription;
21   private emptyState: CreateCommunityState = {
22     enableNsfw: null,
23   };
24   constructor(props: any, context: any) {
25     super(props, context);
26     this.handleCommunityCreate = this.handleCommunityCreate.bind(this);
27     this.state = this.emptyState;
28
29     this.subscription = WebSocketService.Instance.subject
30       .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
31       .subscribe(
32         msg => this.parseMessage(msg),
33         err => console.error(err),
34         () => console.log('complete')
35       );
36
37     WebSocketService.Instance.getSite();
38   }
39
40   componentWillUnmount() {
41     this.subscription.unsubscribe();
42   }
43
44   render() {
45     return (
46       <div class="container">
47         <div class="row">
48           <div class="col-12 col-lg-6 offset-lg-3 mb-4">
49             <h5>{i18n.t('create_community')}</h5>
50             <CommunityForm
51               onCreate={this.handleCommunityCreate}
52               enableNsfw={this.state.enableNsfw}
53             />
54           </div>
55         </div>
56       </div>
57     );
58   }
59
60   handleCommunityCreate(community: Community) {
61     this.props.history.push(`/c/${community.name}`);
62   }
63
64   parseMessage(msg: WebSocketJsonResponse) {
65     console.log(msg);
66     let res = wsJsonToRes(msg);
67     if (msg.error) {
68       toast(i18n.t(msg.error), 'danger');
69       return;
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}`;
75     }
76   }
77 }