1 import { Component } from 'inferno';
2 import { Subscription } from 'rxjs';
3 import { retryWhen, delay, take } from 'rxjs/operators';
4 import { PostForm } from './post-form';
5 import { toast, wsJsonToRes } from '../utils';
6 import { WebSocketService, UserService } from '../services';
10 WebSocketJsonResponse,
13 } from '../interfaces';
14 import { i18n } from '../i18next';
16 interface CreatePostState {
20 export class CreatePost extends Component<any, CreatePostState> {
21 private subscription: Subscription;
22 private emptyState: CreatePostState = {
26 creator_id: undefined,
28 creator_name: undefined,
29 number_of_users: undefined,
30 number_of_posts: undefined,
31 number_of_comments: undefined,
32 number_of_communities: undefined,
33 enable_downvotes: undefined,
34 open_registration: undefined,
35 enable_nsfw: undefined,
39 constructor(props: any, context: any) {
40 super(props, context);
41 this.handlePostCreate = this.handlePostCreate.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();
66 <div class="container">
68 <div class="col-12 col-lg-6 offset-lg-3 mb-4">
69 <h5>{i18n.t('create_post')}</h5>
71 onCreate={this.handlePostCreate}
73 enableDownvotes={this.state.site.enable_downvotes}
74 enableNsfw={this.state.site.enable_nsfw}
82 get params(): PostFormParams {
83 let urlParams = new URLSearchParams(this.props.location.search);
84 let params: PostFormParams = {
85 name: urlParams.get('title'),
86 community: urlParams.get('community') || this.prevCommunityName,
87 body: urlParams.get('body'),
88 url: urlParams.get('url'),
94 get prevCommunityName(): string {
95 if (this.props.match.params.name) {
96 return this.props.match.params.name;
97 } else if (this.props.location.state) {
98 let lastLocation = this.props.location.state.prevPath;
99 if (lastLocation.includes('/c/')) {
100 return lastLocation.split('/c/')[1];
106 handlePostCreate(id: number) {
107 this.props.history.push(`/post/${id}`);
110 parseMessage(msg: WebSocketJsonResponse) {
112 let res = wsJsonToRes(msg);
114 toast(i18n.t(msg.error), 'danger');
116 } else if (res.op == UserOperation.GetSite) {
117 let data = res.data as GetSiteResponse;
118 this.state.site = data.site;
119 this.setState(this.state);
120 document.title = `${i18n.t('create_post')} - ${data.site.name}`;