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";
30 import { HtmlTags } from "../common/html-tags";
31 import { Spinner } from "../common/icon";
32 import { PostForm } from "./post-form";
34 export interface CreatePostProps {
38 function getCreatePostQueryParams() {
39 return getQueryParams<CreatePostProps>({
40 communityId: getIdFromString,
44 interface CreatePostState {
45 siteRes: GetSiteResponse;
47 selectedCommunityChoice?: Choice;
50 export class CreatePost extends Component<
51 RouteComponentProps<Record<string, never>>,
54 private isoData = setIsoData(this.context);
55 private subscription?: Subscription;
56 state: CreatePostState = {
57 siteRes: this.isoData.site_res,
61 constructor(props: RouteComponentProps<Record<string, never>>, context: any) {
62 super(props, context);
64 this.handlePostCreate = this.handlePostCreate.bind(this);
65 this.handleSelectedCommunityChange =
66 this.handleSelectedCommunityChange.bind(this);
68 this.parseMessage = this.parseMessage.bind(this);
69 this.subscription = wsSubscribe(this.parseMessage);
71 // Only fetch the data if coming from another route
72 if (this.isoData.path === this.context.router.route.match.url) {
73 const communityRes = this.isoData.routeData[0] as
74 | GetCommunityResponse
78 const communityChoice: Choice = {
79 label: communityRes.community_view.community.title,
80 value: communityRes.community_view.community.id.toString(),
85 selectedCommunityChoice: communityChoice,
94 this.fetchCommunity();
99 const { communityId } = getCreatePostQueryParams();
100 const auth = myAuth(false);
103 const form: GetCommunity = {
108 WebSocketService.Instance.send(wsClient.getCommunity(form));
112 componentDidMount(): void {
113 const { communityId } = getCreatePostQueryParams();
115 if (communityId?.toString() !== this.state.selectedCommunityChoice?.value) {
116 this.fetchCommunity();
117 } else if (!communityId) {
119 selectedCommunityChoice: undefined,
125 componentWillUnmount() {
127 this.subscription?.unsubscribe();
131 get documentTitle(): string {
132 return `${i18n.t("create_post")} - ${
133 this.state.siteRes.site_view.site.name
138 const { selectedCommunityChoice } = this.state;
140 const locationState = this.props.history.location.state as
145 <div className="container-lg">
147 title={this.documentTitle}
148 path={this.context.router.route.match.url}
150 {this.state.loading ? (
155 <div className="row">
156 <div className="col-12 col-lg-6 offset-lg-3 mb-4">
157 <h5>{i18n.t("create_post")}</h5>
159 onCreate={this.handlePostCreate}
160 params={locationState}
161 enableDownvotes={enableDownvotes(this.state.siteRes)}
162 enableNsfw={enableNsfw(this.state.siteRes)}
163 allLanguages={this.state.siteRes.all_languages}
164 siteLanguages={this.state.siteRes.discussion_languages}
165 selectedCommunityChoice={selectedCommunityChoice}
166 onSelectCommunity={this.handleSelectedCommunityChange}
175 updateUrl({ communityId }: Partial<CreatePostProps>) {
176 const { communityId: urlCommunityId } = getCreatePostQueryParams();
178 const locationState = this.props.history.location.state as
182 const url = new URL(location.href);
184 const newId = (communityId ?? urlCommunityId)?.toString();
186 if (newId !== undefined) {
187 url.searchParams.set("communityId", newId);
189 url.searchParams.delete("communityId");
192 history.replaceState(locationState, "", url);
194 this.fetchCommunity();
197 handleSelectedCommunityChange(choice: Choice) {
199 communityId: getIdFromString(choice?.value),
203 handlePostCreate(post_view: PostView) {
204 this.props.history.replace(`/post/${post_view.post.id}`);
207 static fetchInitialData({
209 query: { communityId },
211 }: InitialFetchRequest<QueryParams<CreatePostProps>>): Promise<any>[] {
212 const promises: Promise<any>[] = [];
215 const form: GetCommunity = {
217 id: getIdFromString(communityId),
220 promises.push(client.getCommunity(form));
222 promises.push(Promise.resolve());
228 parseMessage(msg: any) {
229 const op = wsUserOp(msg);
232 toast(i18n.t(msg.error), "danger");
236 if (op === UserOperation.GetCommunity) {
239 community: { title, id },
241 } = wsJsonToRes<GetCommunityResponse>(msg);
244 selectedCommunityChoice: { label: title, value: id.toString() },