1 import { Component } from "inferno";
7 ListCommunitiesResponse,
14 } from "lemmy-js-client";
15 import { Subscription } from "rxjs";
16 import { InitialFetchRequest, PostFormParams } from "shared/interfaces";
17 import { i18n } from "../../i18next";
18 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 {
35 listCommunitiesResponse?: ListCommunitiesResponse;
36 siteRes: GetSiteResponse;
40 export class CreatePost extends Component<any, CreatePostState> {
41 private isoData = setIsoData(this.context);
42 private subscription?: Subscription;
43 state: CreatePostState = {
44 siteRes: this.isoData.site_res,
48 constructor(props: any, context: any) {
49 super(props, context);
51 this.handlePostCreate = this.handlePostCreate.bind(this);
53 this.parseMessage = this.parseMessage.bind(this);
54 this.subscription = wsSubscribe(this.parseMessage);
56 if (!UserService.Instance.myUserInfo && isBrowser()) {
57 toast(i18n.t("not_logged_in"), "danger");
58 this.context.router.history.push(`/login`);
61 // Only fetch the data if coming from another route
62 if (this.isoData.path == this.context.router.route.match.url) {
65 listCommunitiesResponse: this.isoData
66 .routeData[0] as ListCommunitiesResponse,
75 let nameOrId = this.params.nameOrId;
76 let auth = myAuth(false);
78 if (typeof nameOrId === "string") {
79 let form: GetCommunity = {
83 WebSocketService.Instance.send(wsClient.getCommunity(form));
85 let form: GetCommunity = {
89 WebSocketService.Instance.send(wsClient.getCommunity(form));
92 let listCommunitiesForm: ListCommunities = {
93 type_: ListingType.All,
94 sort: SortType.TopAll,
98 WebSocketService.Instance.send(
99 wsClient.listCommunities(listCommunitiesForm)
104 componentWillUnmount() {
106 this.subscription?.unsubscribe();
110 get documentTitle(): string {
111 return `${i18n.t("create_post")} - ${
112 this.state.siteRes.site_view.site.name
117 let res = this.state.listCommunitiesResponse;
119 <div className="container-lg">
121 title={this.documentTitle}
122 path={this.context.router.route.match.url}
124 {this.state.loading ? (
130 <div className="row">
131 <div className="col-12 col-lg-6 offset-lg-3 mb-4">
132 <h5>{i18n.t("create_post")}</h5>
134 communities={res.communities}
135 onCreate={this.handlePostCreate}
137 enableDownvotes={enableDownvotes(this.state.siteRes)}
138 enableNsfw={enableNsfw(this.state.siteRes)}
139 allLanguages={this.state.siteRes.all_languages}
140 siteLanguages={this.state.siteRes.discussion_languages}
150 get params(): PostFormParams {
151 let urlParams = new URLSearchParams(this.props.location.search);
152 let name = urlParams.get("community_name") ?? this.prevCommunityName;
153 let communityIdParam = urlParams.get("community_id");
154 let id = communityIdParam ? Number(communityIdParam) : this.prevCommunityId;
155 let nameOrId: string | number | undefined;
162 let params: PostFormParams = {
163 name: urlParams.get("title") ?? undefined,
165 body: urlParams.get("body") ?? undefined,
166 url: urlParams.get("url") ?? undefined,
172 get prevCommunityName(): string | undefined {
173 if (this.props.match.params.name) {
174 return this.props.match.params.name;
175 } else if (this.props.location.state) {
176 let lastLocation = this.props.location.state.prevPath;
177 if (lastLocation.includes("/c/")) {
178 return lastLocation.split("/c/").at(1);
184 get prevCommunityId(): number | undefined {
185 // TODO is this actually a number? Whats the real return type
186 let id = this.props.match.params.id;
187 return id ?? undefined;
190 handlePostCreate(post_view: PostView) {
191 this.props.history.push(`/post/${post_view.post.id}`);
194 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
195 let listCommunitiesForm: ListCommunities = {
196 type_: ListingType.All,
197 sort: SortType.TopAll,
201 return [req.client.listCommunities(listCommunitiesForm)];
204 parseMessage(msg: any) {
205 let op = wsUserOp(msg);
208 toast(i18n.t(msg.error), "danger");
210 } else if (op == UserOperation.ListCommunities) {
211 let data = wsJsonToRes<ListCommunitiesResponse>(msg);
212 this.setState({ listCommunitiesResponse: data, loading: false });
213 } else if (op == UserOperation.GetCommunity) {
214 let data = wsJsonToRes<GetCommunityResponse>(msg);
216 listCommunitiesResponse: {
217 communities: [data.community_view],