]> Untitled Git - lemmy.git/blob - ui/src/components/create-post.tsx
Merge branch 'master' into master
[lemmy.git] / ui / src / components / create-post.tsx
1 import { Component } from 'inferno';
2 import { PostForm } from './post-form';
3 import { WebSocketService } from '../services';
4 import { PostFormParams } from '../interfaces';
5 import { i18n } from '../i18next';
6 import { T } from 'inferno-i18next';
7
8 export class CreatePost extends Component<any, any> {
9   constructor(props: any, context: any) {
10     super(props, context);
11     this.handlePostCreate = this.handlePostCreate.bind(this);
12   }
13
14   componentDidMount() {
15     document.title = `${i18n.t('create_post')} - ${
16       WebSocketService.Instance.site.name
17     }`;
18   }
19
20   render() {
21     return (
22       <div class="container">
23         <div class="row">
24           <div class="col-12 col-lg-6 offset-lg-3 mb-4">
25             <h5>
26               <T i18nKey="create_post">#</T>
27             </h5>
28             <PostForm onCreate={this.handlePostCreate} params={this.params} />
29           </div>
30         </div>
31       </div>
32     );
33   }
34
35   get params(): PostFormParams {
36     let urlParams = new URLSearchParams(this.props.location.search);
37     let params: PostFormParams = {
38       name: urlParams.get('name'),
39       community: urlParams.get('community') || this.prevCommunityName,
40       body: urlParams.get('body'),
41       url: urlParams.get('url'),
42     };
43
44     return params;
45   }
46
47   get prevCommunityName(): string {
48     if (this.props.match.params.name) {
49       return this.props.match.params.name;
50     } else if (this.props.location.state) {
51       let lastLocation = this.props.location.state.prevPath;
52       if (lastLocation.includes('/c/')) {
53         return lastLocation.split('/c/')[1];
54       }
55     }
56     return undefined;
57   }
58
59   handlePostCreate(id: number) {
60     this.props.history.push(`/post/${id}`);
61   }
62 }