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 { UserService, 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 if (!UserService.Instance.myUserInfo && isBrowser()) {
73 toast(i18n.t("not_logged_in"), "danger");
74 this.context.router.history.push(`/login`);
77 // Only fetch the data if coming from another route
78 if (this.isoData.path === this.context.router.route.match.url) {
79 const communityRes = this.isoData.routeData[0] as
80 | GetCommunityResponse
84 const communityChoice: Choice = {
85 label: communityRes.community_view.community.name,
86 value: communityRes.community_view.community.id.toString(),
91 selectedCommunityChoice: communityChoice,
100 this.fetchCommunity();
105 const { communityId } = getCreatePostQueryParams();
106 const auth = myAuth(false);
109 const form: GetCommunity = {
114 WebSocketService.Instance.send(wsClient.getCommunity(form));
118 componentDidMount(): void {
119 const { communityId } = getCreatePostQueryParams();
121 if (communityId?.toString() !== this.state.selectedCommunityChoice?.value) {
122 this.fetchCommunity();
123 } else if (!communityId) {
125 selectedCommunityChoice: undefined,
131 componentWillUnmount() {
133 this.subscription?.unsubscribe();
137 get documentTitle(): string {
138 return `${i18n.t("create_post")} - ${
139 this.state.siteRes.site_view.site.name
144 const { selectedCommunityChoice } = this.state;
146 const locationState = this.props.history.location.state as
151 <div className="container-lg">
153 title={this.documentTitle}
154 path={this.context.router.route.match.url}
156 {this.state.loading ? (
161 <div className="row">
162 <div className="col-12 col-lg-6 offset-lg-3 mb-4">
163 <h5>{i18n.t("create_post")}</h5>
165 onCreate={this.handlePostCreate}
166 params={locationState}
167 enableDownvotes={enableDownvotes(this.state.siteRes)}
168 enableNsfw={enableNsfw(this.state.siteRes)}
169 allLanguages={this.state.siteRes.all_languages}
170 siteLanguages={this.state.siteRes.discussion_languages}
171 selectedCommunityChoice={selectedCommunityChoice}
172 onSelectCommunity={this.handleSelectedCommunityChange}
181 updateUrl({ communityId }: Partial<CreatePostProps>) {
182 const { communityId: urlCommunityId } = getCreatePostQueryParams();
184 const queryParams: QueryParams<CreatePostProps> = {
185 communityId: (communityId ?? urlCommunityId)?.toString(),
188 const locationState = this.props.history.location.state as
192 this.props.history.push(
193 `/create_post${getQueryString(queryParams)}`,
197 this.fetchCommunity();
200 handleSelectedCommunityChange(choice: Choice) {
202 communityId: getIdFromString(choice?.value),
206 handlePostCreate(post_view: PostView) {
207 this.props.history.replace(`/post/${post_view.post.id}`);
210 static fetchInitialData({
212 query: { communityId },
214 }: InitialFetchRequest<QueryParams<CreatePostProps>>): Promise<any>[] {
215 const promises: Promise<any>[] = [];
218 const form: GetCommunity = {
220 id: getIdFromString(communityId),
223 promises.push(client.getCommunity(form));
225 promises.push(Promise.resolve());
231 parseMessage(msg: any) {
232 const op = wsUserOp(msg);
235 toast(i18n.t(msg.error), "danger");
239 if (op === UserOperation.GetCommunity) {
242 community: { name, id },
244 } = wsJsonToRes<GetCommunityResponse>(msg);
247 selectedCommunityChoice: { label: name, value: id.toString() },