1 import { Component } from 'inferno';
2 import { Subscription } from 'rxjs';
3 import { PostForm } from './post-form';
4 import { HtmlTags } from './html-tags';
13 import { UserService, WebSocketService } from '../services';
16 ListCommunitiesResponse,
22 } from 'lemmy-js-client';
23 import { i18n } from '../i18next';
24 import { InitialFetchRequest, PostFormParams } from 'shared/interfaces';
26 interface CreatePostState {
28 communities: CommunityView[];
32 export class CreatePost extends Component<any, CreatePostState> {
33 private isoData = setIsoData(this.context);
34 private subscription: Subscription;
35 private emptyState: CreatePostState = {
36 site_view: this.isoData.site_res.site_view,
41 constructor(props: any, context: any) {
42 super(props, context);
43 this.handlePostCreate = this.handlePostCreate.bind(this);
44 this.state = this.emptyState;
46 if (!UserService.Instance.user && isBrowser()) {
47 toast(i18n.t('not_logged_in'), 'danger');
48 this.context.router.history.push(`/login`);
51 this.parseMessage = this.parseMessage.bind(this);
52 this.subscription = wsSubscribe(this.parseMessage);
54 // Only fetch the data if coming from another route
55 if (this.isoData.path == this.context.router.route.match.url) {
56 this.state.communities = this.isoData.routeData[0].communities;
57 this.state.loading = false;
64 let listCommunitiesForm: ListCommunities = {
65 sort: SortType.TopAll,
67 auth: UserService.Instance.authField(false),
69 WebSocketService.Instance.client.listCommunities(listCommunitiesForm);
72 componentWillUnmount() {
74 this.subscription.unsubscribe();
78 get documentTitle(): string {
79 return `${i18n.t('create_post')} - ${this.state.site_view.site.name}`;
84 <div class="container">
86 title={this.documentTitle}
87 path={this.context.router.route.match.url}
89 {this.state.loading ? (
91 <svg class="icon icon-spinner spin">
92 <use xlinkHref="#icon-spinner"></use>
97 <div class="col-12 col-lg-6 offset-lg-3 mb-4">
98 <h5>{i18n.t('create_post')}</h5>
100 communities={this.state.communities}
101 onCreate={this.handlePostCreate}
103 enableDownvotes={this.state.site_view.site.enable_downvotes}
104 enableNsfw={this.state.site_view.site.enable_nsfw}
113 get params(): PostFormParams {
114 let urlParams = new URLSearchParams(this.props.location.search);
115 let params: PostFormParams = {
116 name: urlParams.get('title'),
117 community_name: urlParams.get('community_name') || this.prevCommunityName,
118 community_id: urlParams.get('community_id')
119 ? Number(urlParams.get('community_id')) || this.prevCommunityId
121 body: urlParams.get('body'),
122 url: urlParams.get('url'),
128 get prevCommunityName(): string {
129 if (this.props.match.params.name) {
130 return this.props.match.params.name;
131 } else if (this.props.location.state) {
132 let lastLocation = this.props.location.state.prevPath;
133 if (lastLocation.includes('/c/')) {
134 return lastLocation.split('/c/')[1];
140 get prevCommunityId(): number {
141 if (this.props.match.params.id) {
142 return this.props.match.params.id;
143 } else if (this.props.location.state) {
144 let lastLocation = this.props.location.state.prevPath;
145 if (lastLocation.includes('/community/')) {
146 return Number(lastLocation.split('/community/')[1]);
152 handlePostCreate(post_view: PostView) {
153 this.props.history.push(`/post/${post_view.post.id}`);
156 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
157 let listCommunitiesForm: ListCommunities = {
158 sort: SortType.TopAll,
162 return [req.client.listCommunities(listCommunitiesForm)];
165 parseMessage(msg: any) {
166 let op = wsUserOp(msg);
168 toast(i18n.t(msg.error), 'danger');
170 } else if (op == UserOperation.ListCommunities) {
171 let data = wsJsonToRes<ListCommunitiesResponse>(msg).data;
172 this.state.communities = data.communities;
173 this.state.loading = false;
174 this.setState(this.state);