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 { PostForm } from './post-form';
6 import { toast, wsJsonToRes } from '../utils';
7 import { WebSocketService, UserService } from '../services';
11 WebSocketJsonResponse,
14 } from 'lemmy-js-client';
15 import { i18n } from '../i18next';
17 interface CreatePostState {
21 export class CreatePost extends Component<any, CreatePostState> {
22 private subscription: Subscription;
23 private emptyState: CreatePostState = {
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,
40 constructor(props: any, context: any) {
41 super(props, context);
42 this.handlePostCreate = this.handlePostCreate.bind(this);
43 this.state = this.emptyState;
45 if (!UserService.Instance.user) {
46 toast(i18n.t('not_logged_in'), 'danger');
47 this.context.router.history.push(`/login`);
50 this.subscription = WebSocketService.Instance.subject
51 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
53 msg => this.parseMessage(msg),
54 err => console.error(err),
55 () => console.log('complete')
58 WebSocketService.Instance.getSite();
61 componentWillUnmount() {
62 this.subscription.unsubscribe();
65 get documentTitle(): string {
66 if (this.state.site.name) {
67 return `${i18n.t('create_post')} - ${this.state.site.name}`;
75 <div class="container">
76 <Helmet title={this.documentTitle} />
78 <div class="col-12 col-lg-6 offset-lg-3 mb-4">
79 <h5>{i18n.t('create_post')}</h5>
81 onCreate={this.handlePostCreate}
83 enableDownvotes={this.state.site.enable_downvotes}
84 enableNsfw={this.state.site.enable_nsfw}
92 get params(): PostFormParams {
93 let urlParams = new URLSearchParams(this.props.location.search);
94 let params: PostFormParams = {
95 name: urlParams.get('title'),
96 community: urlParams.get('community') || this.prevCommunityName,
97 body: urlParams.get('body'),
98 url: urlParams.get('url'),
104 get prevCommunityName(): string {
105 if (this.props.match.params.name) {
106 return this.props.match.params.name;
107 } else if (this.props.location.state) {
108 let lastLocation = this.props.location.state.prevPath;
109 if (lastLocation.includes('/c/')) {
110 return lastLocation.split('/c/')[1];
116 handlePostCreate(id: number) {
117 this.props.history.push(`/post/${id}`);
120 parseMessage(msg: WebSocketJsonResponse) {
122 let res = wsJsonToRes(msg);
124 toast(i18n.t(msg.error), 'danger');
126 } else if (res.op == UserOperation.GetSite) {
127 let data = res.data as GetSiteResponse;
128 this.state.site = data.site;
129 this.setState(this.state);