1 import { Component } from 'inferno';
2 import { Subscription } from 'rxjs';
3 import { PostForm } from './post-form';
4 import { HtmlTags } from './html-tags';
16 import { UserService, WebSocketService } from '../services';
19 ListCommunitiesResponse,
25 } from 'lemmy-js-client';
26 import { i18n } from '../i18next';
27 import { InitialFetchRequest, PostFormParams } from 'shared/interfaces';
29 interface CreatePostState {
31 communities: CommunityView[];
35 export class CreatePost extends Component<any, CreatePostState> {
36 private isoData = setIsoData(this.context);
37 private subscription: Subscription;
38 private emptyState: CreatePostState = {
39 site_view: this.isoData.site_res.site_view,
44 constructor(props: any, context: any) {
45 super(props, context);
46 this.handlePostCreate = this.handlePostCreate.bind(this);
47 this.state = this.emptyState;
49 if (!UserService.Instance.user && isBrowser()) {
50 toast(i18n.t('not_logged_in'), 'danger');
51 this.context.router.history.push(`/login`);
54 this.parseMessage = this.parseMessage.bind(this);
55 this.subscription = wsSubscribe(this.parseMessage);
57 // Only fetch the data if coming from another route
58 if (this.isoData.path == this.context.router.route.match.url) {
59 this.state.communities = this.isoData.routeData[0].communities;
60 this.state.loading = false;
67 let listCommunitiesForm: ListCommunities = {
68 sort: SortType.TopAll,
70 auth: authField(false),
72 WebSocketService.Instance.send(
73 wsClient.listCommunities(listCommunitiesForm)
77 componentWillUnmount() {
79 this.subscription.unsubscribe();
83 get documentTitle(): string {
84 return `${i18n.t('create_post')} - ${this.state.site_view.site.name}`;
89 <div class="container">
91 title={this.documentTitle}
92 path={this.context.router.route.match.url}
94 {this.state.loading ? (
96 <svg class="icon icon-spinner spin">
97 <use xlinkHref="#icon-spinner"></use>
102 <div class="col-12 col-lg-6 offset-lg-3 mb-4">
103 <h5>{i18n.t('create_post')}</h5>
105 communities={this.state.communities}
106 onCreate={this.handlePostCreate}
108 enableDownvotes={this.state.site_view.site.enable_downvotes}
109 enableNsfw={this.state.site_view.site.enable_nsfw}
118 get params(): PostFormParams {
119 let urlParams = new URLSearchParams(this.props.location.search);
120 let params: PostFormParams = {
121 name: urlParams.get('title'),
122 community_name: urlParams.get('community_name') || this.prevCommunityName,
123 community_id: urlParams.get('community_id')
124 ? Number(urlParams.get('community_id')) || this.prevCommunityId
126 body: urlParams.get('body'),
127 url: urlParams.get('url'),
133 get prevCommunityName(): string {
134 if (this.props.match.params.name) {
135 return this.props.match.params.name;
136 } else if (this.props.location.state) {
137 let lastLocation = this.props.location.state.prevPath;
138 if (lastLocation.includes('/c/')) {
139 return lastLocation.split('/c/')[1];
145 get prevCommunityId(): number {
146 if (this.props.match.params.id) {
147 return this.props.match.params.id;
148 } else if (this.props.location.state) {
149 let lastLocation = this.props.location.state.prevPath;
150 if (lastLocation.includes('/community/')) {
151 return Number(lastLocation.split('/community/')[1]);
157 handlePostCreate(post_view: PostView) {
158 this.props.history.push(`/post/${post_view.post.id}`);
161 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
162 let listCommunitiesForm: ListCommunities = {
163 sort: SortType.TopAll,
166 setOptionalAuth(listCommunitiesForm, req.auth);
167 return [req.client.listCommunities(listCommunitiesForm)];
170 parseMessage(msg: any) {
171 let op = wsUserOp(msg);
173 toast(i18n.t(msg.error), 'danger');
175 } else if (op == UserOperation.ListCommunities) {
176 let data = wsJsonToRes<ListCommunitiesResponse>(msg).data;
177 this.state.communities = data.communities;
178 this.state.loading = false;
179 this.setState(this.state);