1 import { Either, Left, None, Option, Right, Some } from "@sniptt/monads";
2 import { Component } from "inferno";
8 ListCommunitiesResponse,
16 } from "lemmy-js-client";
17 import { Subscription } from "rxjs";
18 import { InitialFetchRequest, PostFormParams } from "shared/interfaces";
19 import { i18n } from "../../i18next";
20 import { UserService, WebSocketService } from "../../services";
32 import { HtmlTags } from "../common/html-tags";
33 import { Spinner } from "../common/icon";
34 import { PostForm } from "./post-form";
36 interface CreatePostState {
37 listCommunitiesResponse: Option<ListCommunitiesResponse>;
38 siteRes: GetSiteResponse;
42 export class CreatePost extends Component<any, CreatePostState> {
43 private isoData = setIsoData(this.context, ListCommunitiesResponse);
44 private subscription: Subscription;
45 private emptyState: CreatePostState = {
46 siteRes: this.isoData.site_res,
47 listCommunitiesResponse: None,
51 constructor(props: any, context: any) {
52 super(props, context);
53 this.state = this.emptyState;
55 this.handlePostCreate = this.handlePostCreate.bind(this);
57 this.parseMessage = this.parseMessage.bind(this);
58 this.subscription = wsSubscribe(this.parseMessage);
60 if (UserService.Instance.myUserInfo.isNone() && isBrowser()) {
61 toast(i18n.t("not_logged_in"), "danger");
62 this.context.router.history.push(`/login`);
65 // Only fetch the data if coming from another route
66 if (this.isoData.path == this.context.router.route.match.url) {
69 listCommunitiesResponse: Some(
70 this.isoData.routeData[0] as ListCommunitiesResponse
80 this.params.nameOrId.match({
84 let form = new GetCommunity({
87 auth: auth(false).ok(),
89 WebSocketService.Instance.send(wsClient.getCommunity(form));
92 let form = new GetCommunity({
95 auth: auth(false).ok(),
97 WebSocketService.Instance.send(wsClient.getCommunity(form));
101 let listCommunitiesForm = new ListCommunities({
102 type_: Some(ListingType.All),
103 sort: Some(SortType.TopAll),
104 limit: Some(fetchLimit),
106 auth: auth(false).ok(),
108 WebSocketService.Instance.send(
109 wsClient.listCommunities(listCommunitiesForm)
115 componentWillUnmount() {
117 this.subscription.unsubscribe();
121 get documentTitle(): string {
122 return this.state.siteRes.site_view.match({
123 some: siteView => `${i18n.t("create_post")} - ${siteView.site.name}`,
130 <div className="container-lg">
132 title={this.documentTitle}
133 path={this.context.router.route.match.url}
137 {this.state.loading ? (
142 this.state.listCommunitiesResponse.match({
144 <div className="row">
145 <div className="col-12 col-lg-6 offset-lg-3 mb-4">
146 <h5>{i18n.t("create_post")}</h5>
149 communities={Some(res.communities)}
150 onCreate={this.handlePostCreate}
151 params={Some(this.params)}
152 enableDownvotes={enableDownvotes(this.state.siteRes)}
153 enableNsfw={enableNsfw(this.state.siteRes)}
154 allLanguages={this.state.siteRes.all_languages}
166 get params(): PostFormParams {
167 let urlParams = new URLSearchParams(this.props.location.search);
168 let name = toOption(urlParams.get("community_name")).or(
169 this.prevCommunityName
171 let id = toOption(urlParams.get("community_id"))
173 .or(this.prevCommunityId);
174 let nameOrId: Option<Either<string, number>>;
176 nameOrId = Some(Left(name.unwrap()));
177 } else if (id.isSome()) {
178 nameOrId = Some(Right(id.unwrap()));
183 let params: PostFormParams = {
184 name: toOption(urlParams.get("title")),
186 body: toOption(urlParams.get("body")),
187 url: toOption(urlParams.get("url")),
193 get prevCommunityName(): Option<string> {
194 if (this.props.match.params.name) {
195 return toOption(this.props.match.params.name);
196 } else if (this.props.location.state) {
197 let lastLocation = this.props.location.state.prevPath;
198 if (lastLocation.includes("/c/")) {
199 return toOption(lastLocation.split("/c/")[1]);
205 get prevCommunityId(): Option<number> {
206 if (this.props.match.params.id) {
207 return toOption(this.props.match.params.id);
212 handlePostCreate(post_view: PostView) {
213 this.props.history.push(`/post/${post_view.post.id}`);
216 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
217 let listCommunitiesForm = new ListCommunities({
218 type_: Some(ListingType.All),
219 sort: Some(SortType.TopAll),
220 limit: Some(fetchLimit),
224 return [req.client.listCommunities(listCommunitiesForm)];
227 parseMessage(msg: any) {
228 let op = wsUserOp(msg);
231 toast(i18n.t(msg.error), "danger");
233 } else if (op == UserOperation.ListCommunities) {
234 let data = wsJsonToRes<ListCommunitiesResponse>(
236 ListCommunitiesResponse
238 this.setState({ listCommunitiesResponse: Some(data), loading: false });
239 } else if (op == UserOperation.GetCommunity) {
240 let data = wsJsonToRes<GetCommunityResponse>(msg, GetCommunityResponse);
242 listCommunitiesResponse: Some({
243 communities: [data.community_view],