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";
32 import { HtmlTags } from "../common/html-tags";
33 import { Spinner } from "../common/icon";
34 import { PostForm } from "./post-form";
36 export interface CreatePostProps {
40 interface CreatePostData {
41 communityResponse?: GetCommunityResponse;
44 function getCreatePostQueryParams() {
45 return getQueryParams<CreatePostProps>({
46 communityId: getIdFromString,
50 interface CreatePostState {
51 siteRes: GetSiteResponse;
53 selectedCommunityChoice?: Choice;
56 export class CreatePost extends Component<
57 RouteComponentProps<Record<string, never>>,
60 private isoData = setIsoData<CreatePostData>(this.context);
61 private subscription?: Subscription;
62 state: CreatePostState = {
63 siteRes: this.isoData.site_res,
67 constructor(props: RouteComponentProps<Record<string, never>>, context: any) {
68 super(props, context);
70 this.handlePostCreate = this.handlePostCreate.bind(this);
71 this.handleSelectedCommunityChange =
72 this.handleSelectedCommunityChange.bind(this);
74 this.parseMessage = this.parseMessage.bind(this);
75 this.subscription = wsSubscribe(this.parseMessage);
77 // Only fetch the data if coming from another route
78 if (this.isoData.path === this.context.router.route.match.url) {
79 const { communityResponse } = this.isoData.routeData;
81 if (communityResponse) {
82 const communityChoice: Choice = {
83 label: communityResponse.community_view.community.name,
84 value: communityResponse.community_view.community.id.toString(),
89 selectedCommunityChoice: communityChoice,
98 this.fetchCommunity();
103 const { communityId } = getCreatePostQueryParams();
104 const auth = myAuth(false);
107 const form: GetCommunity = {
112 WebSocketService.Instance.send(wsClient.getCommunity(form));
116 componentDidMount(): void {
117 const { communityId } = getCreatePostQueryParams();
119 if (communityId?.toString() !== this.state.selectedCommunityChoice?.value) {
120 this.fetchCommunity();
121 } else if (!communityId) {
123 selectedCommunityChoice: undefined,
129 componentWillUnmount() {
131 this.subscription?.unsubscribe();
135 get documentTitle(): string {
136 return `${i18n.t("create_post")} - ${
137 this.state.siteRes.site_view.site.name
142 const { selectedCommunityChoice } = this.state;
144 const locationState = this.props.history.location.state as
149 <div className="container-lg">
151 title={this.documentTitle}
152 path={this.context.router.route.match.url}
154 {this.state.loading ? (
159 <div className="row">
160 <div className="col-12 col-lg-6 offset-lg-3 mb-4">
161 <h5>{i18n.t("create_post")}</h5>
163 onCreate={this.handlePostCreate}
164 params={locationState}
165 enableDownvotes={enableDownvotes(this.state.siteRes)}
166 enableNsfw={enableNsfw(this.state.siteRes)}
167 allLanguages={this.state.siteRes.all_languages}
168 siteLanguages={this.state.siteRes.discussion_languages}
169 selectedCommunityChoice={selectedCommunityChoice}
170 onSelectCommunity={this.handleSelectedCommunityChange}
179 updateUrl({ communityId }: Partial<CreatePostProps>) {
180 const { communityId: urlCommunityId } = getCreatePostQueryParams();
182 const queryParams: QueryParams<CreatePostProps> = {
183 communityId: (communityId ?? urlCommunityId)?.toString(),
186 const locationState = this.props.history.location.state as
190 this.props.history.replace(
191 `/create_post${getQueryString(queryParams)}`,
195 this.fetchCommunity();
198 handleSelectedCommunityChange(choice: Choice) {
200 communityId: getIdFromString(choice?.value),
204 handlePostCreate(post_view: PostView) {
205 this.props.history.replace(`/post/${post_view.post.id}`);
208 static fetchInitialData({
210 query: { communityId },
212 }: InitialFetchRequest<
213 QueryParams<CreatePostProps>
214 >): WithPromiseKeys<CreatePostData> {
215 const data: WithPromiseKeys<CreatePostData> = {};
218 const form: GetCommunity = {
220 id: getIdFromString(communityId),
223 data.communityResponse = client.getCommunity(form);
229 parseMessage(msg: any) {
230 const op = wsUserOp(msg);
233 toast(i18n.t(msg.error), "danger");
237 if (op === UserOperation.GetCommunity) {
240 community: { name, id },
242 } = wsJsonToRes<GetCommunityResponse>(msg);
245 selectedCommunityChoice: { label: name, value: id.toString() },