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 `${i18n.t("create_post")} - ${
123 this.state.siteRes.site_view.site.name
129 <div className="container-lg">
131 title={this.documentTitle}
132 path={this.context.router.route.match.url}
136 {this.state.loading ? (
141 this.state.listCommunitiesResponse.match({
143 <div className="row">
144 <div className="col-12 col-lg-6 offset-lg-3 mb-4">
145 <h5>{i18n.t("create_post")}</h5>
148 communities={Some(res.communities)}
149 onCreate={this.handlePostCreate}
150 params={Some(this.params)}
151 enableDownvotes={enableDownvotes(this.state.siteRes)}
152 enableNsfw={enableNsfw(this.state.siteRes)}
153 allLanguages={this.state.siteRes.all_languages}
165 get params(): PostFormParams {
166 let urlParams = new URLSearchParams(this.props.location.search);
167 let name = toOption(urlParams.get("community_name")).or(
168 this.prevCommunityName
170 let id = toOption(urlParams.get("community_id"))
172 .or(this.prevCommunityId);
173 let nameOrId: Option<Either<string, number>>;
175 nameOrId = Some(Left(name.unwrap()));
176 } else if (id.isSome()) {
177 nameOrId = Some(Right(id.unwrap()));
182 let params: PostFormParams = {
183 name: toOption(urlParams.get("title")),
185 body: toOption(urlParams.get("body")),
186 url: toOption(urlParams.get("url")),
192 get prevCommunityName(): Option<string> {
193 if (this.props.match.params.name) {
194 return toOption(this.props.match.params.name);
195 } else if (this.props.location.state) {
196 let lastLocation = this.props.location.state.prevPath;
197 if (lastLocation.includes("/c/")) {
198 return toOption(lastLocation.split("/c/")[1]);
204 get prevCommunityId(): Option<number> {
205 if (this.props.match.params.id) {
206 return toOption(this.props.match.params.id);
211 handlePostCreate(post_view: PostView) {
212 this.props.history.push(`/post/${post_view.post.id}`);
215 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
216 let listCommunitiesForm = new ListCommunities({
217 type_: Some(ListingType.All),
218 sort: Some(SortType.TopAll),
219 limit: Some(fetchLimit),
223 return [req.client.listCommunities(listCommunitiesForm)];
226 parseMessage(msg: any) {
227 let op = wsUserOp(msg);
230 toast(i18n.t(msg.error), "danger");
232 } else if (op == UserOperation.ListCommunities) {
233 let data = wsJsonToRes<ListCommunitiesResponse>(
235 ListCommunitiesResponse
237 this.setState({ listCommunitiesResponse: Some(data), loading: false });
238 } else if (op == UserOperation.GetCommunity) {
239 let data = wsJsonToRes<GetCommunityResponse>(msg, GetCommunityResponse);
241 listCommunitiesResponse: Some({
242 communities: [data.community_view],