1 import { Component } from "inferno";
2 import { RouteComponentProps } from "inferno-router/dist/Route";
11 } from "lemmy-js-client";
12 import { Subscription } from "rxjs";
13 import { InitialFetchRequest, PostFormParams } from "shared/interfaces";
14 import { i18n } from "../../i18next";
15 import { WebSocketService } from "../../services";
31 import { HtmlTags } from "../common/html-tags";
32 import { Spinner } from "../common/icon";
33 import { PostForm } from "./post-form";
35 export interface CreatePostProps {
39 function getCreatePostQueryParams() {
40 return getQueryParams<CreatePostProps>({
41 communityId: getIdFromString,
45 interface CreatePostState {
46 siteRes: GetSiteResponse;
48 selectedCommunityChoice?: Choice;
51 export class CreatePost extends Component<
52 RouteComponentProps<Record<string, never>>,
55 private isoData = setIsoData(this.context);
56 private subscription?: Subscription;
57 state: CreatePostState = {
58 siteRes: this.isoData.site_res,
62 constructor(props: RouteComponentProps<Record<string, never>>, context: any) {
63 super(props, context);
65 this.handlePostCreate = this.handlePostCreate.bind(this);
66 this.handleSelectedCommunityChange =
67 this.handleSelectedCommunityChange.bind(this);
69 this.parseMessage = this.parseMessage.bind(this);
70 this.subscription = wsSubscribe(this.parseMessage);
72 // Only fetch the data if coming from another route
73 if (this.isoData.path === this.context.router.route.match.url) {
74 const communityRes = this.isoData.routeData[0] as
75 | GetCommunityResponse
79 const communityChoice: Choice = {
80 label: communityRes.community_view.community.name,
81 value: communityRes.community_view.community.id.toString(),
86 selectedCommunityChoice: communityChoice,
95 this.fetchCommunity();
100 const { communityId } = getCreatePostQueryParams();
101 const auth = myAuth(false);
104 const form: GetCommunity = {
109 WebSocketService.Instance.send(wsClient.getCommunity(form));
113 componentDidMount(): void {
114 const { communityId } = getCreatePostQueryParams();
116 if (communityId?.toString() !== this.state.selectedCommunityChoice?.value) {
117 this.fetchCommunity();
118 } else if (!communityId) {
120 selectedCommunityChoice: undefined,
126 componentWillUnmount() {
128 this.subscription?.unsubscribe();
132 get documentTitle(): string {
133 return `${i18n.t("create_post")} - ${
134 this.state.siteRes.site_view.site.name
139 const { selectedCommunityChoice } = this.state;
141 const locationState = this.props.history.location.state as
146 <div className="container-lg">
148 title={this.documentTitle}
149 path={this.context.router.route.match.url}
151 {this.state.loading ? (
156 <div className="row">
157 <div className="col-12 col-lg-6 offset-lg-3 mb-4">
158 <h5>{i18n.t("create_post")}</h5>
160 onCreate={this.handlePostCreate}
161 params={locationState}
162 enableDownvotes={enableDownvotes(this.state.siteRes)}
163 enableNsfw={enableNsfw(this.state.siteRes)}
164 allLanguages={this.state.siteRes.all_languages}
165 siteLanguages={this.state.siteRes.discussion_languages}
166 selectedCommunityChoice={selectedCommunityChoice}
167 onSelectCommunity={this.handleSelectedCommunityChange}
176 updateUrl({ communityId }: Partial<CreatePostProps>) {
177 const { communityId: urlCommunityId } = getCreatePostQueryParams();
179 const queryParams: QueryParams<CreatePostProps> = {
180 communityId: (communityId ?? urlCommunityId)?.toString(),
183 const locationState = this.props.history.location.state as
187 this.props.history.replace(
188 `/create_post${getQueryString(queryParams)}`,
192 this.fetchCommunity();
195 handleSelectedCommunityChange(choice: Choice) {
197 communityId: getIdFromString(choice?.value),
201 handlePostCreate(post_view: PostView) {
202 this.props.history.replace(`/post/${post_view.post.id}`);
205 static fetchInitialData({
207 query: { communityId },
209 }: InitialFetchRequest<QueryParams<CreatePostProps>>): Promise<any>[] {
210 const promises: Promise<any>[] = [];
213 const form: GetCommunity = {
215 id: getIdFromString(communityId),
218 promises.push(client.getCommunity(form));
220 promises.push(Promise.resolve());
226 parseMessage(msg: any) {
227 const op = wsUserOp(msg);
230 toast(i18n.t(msg.error), "danger");
234 if (op === UserOperation.GetCommunity) {
237 community: { name, id },
239 } = wsJsonToRes<GetCommunityResponse>(msg);
242 selectedCommunityChoice: { label: name, value: id.toString() },