1 import { Component } from "inferno";
7 ListCommunitiesResponse,
13 } from "lemmy-js-client";
14 import { Subscription } from "rxjs";
15 import { InitialFetchRequest, PostFormParams } from "shared/interfaces";
16 import { i18n } from "../../i18next";
17 import { UserService, WebSocketService } from "../../services";
30 import { HtmlTags } from "../common/html-tags";
31 import { Spinner } from "../common/icon";
32 import { PostForm } from "./post-form";
34 interface CreatePostState {
36 communities: CommunityView[];
40 export class CreatePost extends Component<any, CreatePostState> {
41 private isoData = setIsoData(this.context);
42 private subscription: Subscription;
43 private emptyState: CreatePostState = {
44 site_view: this.isoData.site_res.site_view,
49 constructor(props: any, context: any) {
50 super(props, context);
51 this.handlePostCreate = this.handlePostCreate.bind(this);
52 this.state = this.emptyState;
54 if (!UserService.Instance.localUserView && isBrowser()) {
55 toast(i18n.t("not_logged_in"), "danger");
56 this.context.router.history.push(`/login`);
59 this.parseMessage = this.parseMessage.bind(this);
60 this.subscription = wsSubscribe(this.parseMessage);
62 // Only fetch the data if coming from another route
63 if (this.isoData.path == this.context.router.route.match.url) {
64 this.state.communities = this.isoData.routeData[0].communities;
65 this.state.loading = false;
72 if (this.params.community_id) {
73 let form: GetCommunity = {
74 id: this.params.community_id,
76 WebSocketService.Instance.send(wsClient.getCommunity(form));
77 } else if (this.params.community_name) {
78 let form: GetCommunity = {
79 name: this.params.community_name,
81 WebSocketService.Instance.send(wsClient.getCommunity(form));
83 let listCommunitiesForm: ListCommunities = {
84 type_: ListingType.All,
85 sort: SortType.TopAll,
87 auth: authField(false),
89 WebSocketService.Instance.send(
90 wsClient.listCommunities(listCommunitiesForm)
95 componentWillUnmount() {
97 this.subscription.unsubscribe();
101 get documentTitle(): string {
102 return `${i18n.t("create_post")} - ${this.state.site_view.site.name}`;
107 <div class="container">
109 title={this.documentTitle}
110 path={this.context.router.route.match.url}
112 {this.state.loading ? (
118 <div class="col-12 col-lg-6 offset-lg-3 mb-4">
119 <h5>{i18n.t("create_post")}</h5>
121 communities={this.state.communities}
122 onCreate={this.handlePostCreate}
124 enableDownvotes={this.state.site_view.site.enable_downvotes}
125 enableNsfw={this.state.site_view.site.enable_nsfw}
134 get params(): PostFormParams {
135 let urlParams = new URLSearchParams(this.props.location.search);
136 let params: PostFormParams = {
137 name: urlParams.get("title"),
138 community_name: urlParams.get("community_name") || this.prevCommunityName,
139 community_id: urlParams.get("community_id")
140 ? Number(urlParams.get("community_id")) || this.prevCommunityId
142 body: urlParams.get("body"),
143 url: urlParams.get("url"),
149 get prevCommunityName(): string {
150 if (this.props.match.params.name) {
151 return this.props.match.params.name;
152 } else if (this.props.location.state) {
153 let lastLocation = this.props.location.state.prevPath;
154 if (lastLocation.includes("/c/")) {
155 return lastLocation.split("/c/")[1];
161 get prevCommunityId(): number {
162 if (this.props.match.params.id) {
163 return this.props.match.params.id;
168 handlePostCreate(post_view: PostView) {
169 this.props.history.push(`/post/${post_view.post.id}`);
172 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
173 let listCommunitiesForm: ListCommunities = {
174 type_: ListingType.All,
175 sort: SortType.TopAll,
178 setOptionalAuth(listCommunitiesForm, req.auth);
179 return [req.client.listCommunities(listCommunitiesForm)];
182 parseMessage(msg: any) {
183 let op = wsUserOp(msg);
186 toast(i18n.t(msg.error), "danger");
188 } else if (op == UserOperation.ListCommunities) {
189 let data = wsJsonToRes<ListCommunitiesResponse>(msg).data;
190 this.state.communities = data.communities;
191 this.state.loading = false;
192 this.setState(this.state);
193 } else if (op == UserOperation.GetCommunity) {
194 let data = wsJsonToRes<GetCommunityResponse>(msg).data;
195 this.state.communities = [data.community_view];
196 this.state.loading = false;
197 this.setState(this.state);