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';
17 WebSocketJsonResponse,
18 ListCommunitiesResponse,
23 } from 'lemmy-js-client';
24 import { i18n } from '../i18next';
25 import { InitialFetchRequest } from 'shared/interfaces';
27 interface CreatePostState {
29 communities: Community[];
33 export class CreatePost extends Component<any, CreatePostState> {
34 private isoData = setIsoData(this.context);
35 private subscription: Subscription;
36 private emptyState: CreatePostState = {
37 site: this.isoData.site.site,
42 constructor(props: any, context: any) {
43 super(props, context);
44 this.handlePostCreate = this.handlePostCreate.bind(this);
45 this.state = this.emptyState;
47 if (!UserService.Instance.user && isBrowser()) {
48 toast(i18n.t('not_logged_in'), 'danger');
49 this.context.router.history.push(`/login`);
52 this.parseMessage = this.parseMessage.bind(this);
53 this.subscription = wsSubscribe(this.parseMessage);
55 // Only fetch the data if coming from another route
56 if (this.isoData.path == this.context.router.route.match.url) {
57 this.state.communities = this.isoData.routeData[0].communities;
58 this.state.loading = false;
65 let listCommunitiesForm: ListCommunitiesForm = {
66 sort: SortType.TopAll,
69 WebSocketService.Instance.listCommunities(listCommunitiesForm);
72 componentWillUnmount() {
74 this.subscription.unsubscribe();
78 get documentTitle(): string {
79 return `${i18n.t('create_post')} - ${this.state.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.enable_downvotes}
104 enableNsfw={this.state.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(id: number) {
153 this.props.history.push(`/post/${id}`);
156 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
157 let listCommunitiesForm: ListCommunitiesForm = {
158 sort: SortType.TopAll,
161 setAuth(listCommunitiesForm, req.auth);
162 return [req.client.listCommunities(listCommunitiesForm)];
165 parseMessage(msg: WebSocketJsonResponse) {
167 let res = wsJsonToRes(msg);
169 toast(i18n.t(msg.error), 'danger');
171 } else if (res.op == UserOperation.ListCommunities) {
172 let data = res.data as ListCommunitiesResponse;
173 this.state.communities = data.communities;
174 this.state.loading = false;
175 this.setState(this.state);