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.handlePostCreate = this.handlePostCreate.bind(this);
54 this.state = this.emptyState;
56 if (UserService.Instance.myUserInfo.isNone() && isBrowser()) {
57 toast(i18n.t("not_logged_in"), "danger");
58 this.context.router.history.push(`/login`);
61 this.parseMessage = this.parseMessage.bind(this);
62 this.subscription = wsSubscribe(this.parseMessage);
64 // Only fetch the data if coming from another route
65 if (this.isoData.path == this.context.router.route.match.url) {
66 this.state.listCommunitiesResponse = Some(
67 this.isoData.routeData[0] as ListCommunitiesResponse
69 this.state.loading = false;
76 this.params.nameOrId.match({
80 let form = new GetCommunity({
83 auth: auth(false).ok(),
85 WebSocketService.Instance.send(wsClient.getCommunity(form));
88 let form = new GetCommunity({
91 auth: auth(false).ok(),
93 WebSocketService.Instance.send(wsClient.getCommunity(form));
97 let listCommunitiesForm = new ListCommunities({
98 type_: Some(ListingType.All),
99 sort: Some(SortType.TopAll),
100 limit: Some(fetchLimit),
102 auth: auth(false).ok(),
104 WebSocketService.Instance.send(
105 wsClient.listCommunities(listCommunitiesForm)
111 componentWillUnmount() {
113 this.subscription.unsubscribe();
117 get documentTitle(): string {
118 return this.state.siteRes.site_view.match({
119 some: siteView => `${i18n.t("create_post")} - ${siteView.site.name}`,
126 <div class="container">
128 title={this.documentTitle}
129 path={this.context.router.route.match.url}
133 {this.state.loading ? (
138 this.state.listCommunitiesResponse.match({
141 <div class="col-12 col-lg-6 offset-lg-3 mb-4">
142 <h5>{i18n.t("create_post")}</h5>
145 communities={Some(res.communities)}
146 onCreate={this.handlePostCreate}
147 params={Some(this.params)}
148 enableDownvotes={enableDownvotes(this.state.siteRes)}
149 enableNsfw={enableNsfw(this.state.siteRes)}
161 get params(): PostFormParams {
162 let urlParams = new URLSearchParams(this.props.location.search);
163 let name = toOption(urlParams.get("community_name")).or(
164 this.prevCommunityName
166 let id = toOption(urlParams.get("community_id"))
168 .or(this.prevCommunityId);
169 let nameOrId: Option<Either<string, number>>;
171 nameOrId = Some(Left(name.unwrap()));
172 } else if (id.isSome()) {
173 nameOrId = Some(Right(id.unwrap()));
178 let params: PostFormParams = {
179 name: toOption(urlParams.get("title")),
181 body: toOption(urlParams.get("body")),
182 url: toOption(urlParams.get("url")),
188 get prevCommunityName(): Option<string> {
189 if (this.props.match.params.name) {
190 return toOption(this.props.match.params.name);
191 } else if (this.props.location.state) {
192 let lastLocation = this.props.location.state.prevPath;
193 if (lastLocation.includes("/c/")) {
194 return toOption(lastLocation.split("/c/")[1]);
200 get prevCommunityId(): Option<number> {
201 if (this.props.match.params.id) {
202 return toOption(this.props.match.params.id);
207 handlePostCreate(post_view: PostView) {
208 this.props.history.push(`/post/${post_view.post.id}`);
211 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
212 let listCommunitiesForm = new ListCommunities({
213 type_: Some(ListingType.All),
214 sort: Some(SortType.TopAll),
215 limit: Some(fetchLimit),
219 return [req.client.listCommunities(listCommunitiesForm)];
222 parseMessage(msg: any) {
223 let op = wsUserOp(msg);
226 toast(i18n.t(msg.error), "danger");
228 } else if (op == UserOperation.ListCommunities) {
229 let data = wsJsonToRes<ListCommunitiesResponse>(
231 ListCommunitiesResponse
233 this.state.listCommunitiesResponse = Some(data);
234 this.state.loading = false;
235 this.setState(this.state);
236 } else if (op == UserOperation.GetCommunity) {
237 let data = wsJsonToRes<GetCommunityResponse>(msg, GetCommunityResponse);
238 this.state.listCommunitiesResponse = Some({
239 communities: [data.community_view],
241 this.state.loading = false;
242 this.setState(this.state);