2022-06-21 21:42:29 +00:00
|
|
|
import { Either, Left, None, Option, Right, Some } from "@sniptt/monads";
|
2021-02-22 02:39:04 +00:00
|
|
|
import { Component } from "inferno";
|
2021-07-17 20:42:55 +00:00
|
|
|
import {
|
|
|
|
GetCommunity,
|
|
|
|
GetCommunityResponse,
|
2022-06-21 21:42:29 +00:00
|
|
|
GetSiteResponse,
|
2021-07-17 20:42:55 +00:00
|
|
|
ListCommunities,
|
|
|
|
ListCommunitiesResponse,
|
|
|
|
ListingType,
|
|
|
|
PostView,
|
|
|
|
SortType,
|
2022-06-21 21:42:29 +00:00
|
|
|
toOption,
|
2021-07-17 20:42:55 +00:00
|
|
|
UserOperation,
|
2022-06-21 21:42:29 +00:00
|
|
|
wsJsonToRes,
|
|
|
|
wsUserOp,
|
2021-07-17 20:42:55 +00:00
|
|
|
} from "lemmy-js-client";
|
2021-02-22 02:39:04 +00:00
|
|
|
import { Subscription } from "rxjs";
|
2021-07-17 20:42:55 +00:00
|
|
|
import { InitialFetchRequest, PostFormParams } from "shared/interfaces";
|
|
|
|
import { i18n } from "../../i18next";
|
|
|
|
import { UserService, WebSocketService } from "../../services";
|
2020-09-07 22:24:48 +00:00
|
|
|
import {
|
2022-06-21 21:42:29 +00:00
|
|
|
auth,
|
|
|
|
enableDownvotes,
|
|
|
|
enableNsfw,
|
2021-04-23 21:48:31 +00:00
|
|
|
fetchLimit,
|
2020-09-08 18:44:55 +00:00
|
|
|
isBrowser,
|
2020-09-07 22:24:48 +00:00
|
|
|
setIsoData,
|
|
|
|
toast,
|
2020-12-24 22:05:57 +00:00
|
|
|
wsClient,
|
2020-09-07 22:24:48 +00:00
|
|
|
wsSubscribe,
|
2021-07-17 20:42:55 +00:00
|
|
|
} from "../../utils";
|
|
|
|
import { HtmlTags } from "../common/html-tags";
|
|
|
|
import { Spinner } from "../common/icon";
|
|
|
|
import { PostForm } from "./post-form";
|
2020-09-06 16:15:25 +00:00
|
|
|
|
|
|
|
interface CreatePostState {
|
2022-06-21 21:42:29 +00:00
|
|
|
listCommunitiesResponse: Option<ListCommunitiesResponse>;
|
|
|
|
siteRes: GetSiteResponse;
|
2020-09-07 22:24:48 +00:00
|
|
|
loading: boolean;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class CreatePost extends Component<any, CreatePostState> {
|
2022-06-21 21:42:29 +00:00
|
|
|
private isoData = setIsoData(this.context, ListCommunitiesResponse);
|
2020-09-06 16:15:25 +00:00
|
|
|
private subscription: Subscription;
|
|
|
|
private emptyState: CreatePostState = {
|
2022-06-21 21:42:29 +00:00
|
|
|
siteRes: this.isoData.site_res,
|
|
|
|
listCommunitiesResponse: None,
|
2020-09-07 22:24:48 +00:00
|
|
|
loading: true,
|
2020-09-06 16:15:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props: any, context: any) {
|
|
|
|
super(props, context);
|
|
|
|
this.state = this.emptyState;
|
|
|
|
|
2022-09-22 15:03:35 +00:00
|
|
|
this.handlePostCreate = this.handlePostCreate.bind(this);
|
|
|
|
|
|
|
|
this.parseMessage = this.parseMessage.bind(this);
|
|
|
|
this.subscription = wsSubscribe(this.parseMessage);
|
|
|
|
|
2022-06-21 21:42:29 +00:00
|
|
|
if (UserService.Instance.myUserInfo.isNone() && isBrowser()) {
|
2021-02-22 02:39:04 +00:00
|
|
|
toast(i18n.t("not_logged_in"), "danger");
|
2020-09-06 16:15:25 +00:00
|
|
|
this.context.router.history.push(`/login`);
|
|
|
|
}
|
|
|
|
|
2020-09-07 22:24:48 +00:00
|
|
|
// Only fetch the data if coming from another route
|
|
|
|
if (this.isoData.path == this.context.router.route.match.url) {
|
2022-09-22 15:03:35 +00:00
|
|
|
this.state = {
|
|
|
|
...this.state,
|
|
|
|
listCommunitiesResponse: Some(
|
|
|
|
this.isoData.routeData[0] as ListCommunitiesResponse
|
|
|
|
),
|
|
|
|
loading: false,
|
|
|
|
};
|
2020-09-07 22:24:48 +00:00
|
|
|
} else {
|
|
|
|
this.refetch();
|
|
|
|
}
|
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2020-09-07 22:24:48 +00:00
|
|
|
refetch() {
|
2022-06-21 21:42:29 +00:00
|
|
|
this.params.nameOrId.match({
|
|
|
|
some: opt =>
|
|
|
|
opt.match({
|
|
|
|
left: name => {
|
|
|
|
let form = new GetCommunity({
|
|
|
|
name: Some(name),
|
|
|
|
id: None,
|
|
|
|
auth: auth(false).ok(),
|
|
|
|
});
|
|
|
|
WebSocketService.Instance.send(wsClient.getCommunity(form));
|
|
|
|
},
|
|
|
|
right: id => {
|
|
|
|
let form = new GetCommunity({
|
|
|
|
id: Some(id),
|
|
|
|
name: None,
|
|
|
|
auth: auth(false).ok(),
|
|
|
|
});
|
|
|
|
WebSocketService.Instance.send(wsClient.getCommunity(form));
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
none: () => {
|
|
|
|
let listCommunitiesForm = new ListCommunities({
|
|
|
|
type_: Some(ListingType.All),
|
|
|
|
sort: Some(SortType.TopAll),
|
|
|
|
limit: Some(fetchLimit),
|
|
|
|
page: None,
|
|
|
|
auth: auth(false).ok(),
|
|
|
|
});
|
|
|
|
WebSocketService.Instance.send(
|
|
|
|
wsClient.listCommunities(listCommunitiesForm)
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
2020-09-08 18:44:55 +00:00
|
|
|
if (isBrowser()) {
|
|
|
|
this.subscription.unsubscribe();
|
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get documentTitle(): string {
|
2022-06-21 21:42:29 +00:00
|
|
|
return this.state.siteRes.site_view.match({
|
|
|
|
some: siteView => `${i18n.t("create_post")} - ${siteView.site.name}`,
|
|
|
|
none: "",
|
|
|
|
});
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="container">
|
2020-09-11 18:09:21 +00:00
|
|
|
<HtmlTags
|
|
|
|
title={this.documentTitle}
|
|
|
|
path={this.context.router.route.match.url}
|
2022-06-21 21:42:29 +00:00
|
|
|
description={None}
|
|
|
|
image={None}
|
2020-09-11 18:09:21 +00:00
|
|
|
/>
|
2020-09-07 22:24:48 +00:00
|
|
|
{this.state.loading ? (
|
|
|
|
<h5>
|
2021-07-17 20:21:31 +00:00
|
|
|
<Spinner large />
|
2020-09-07 22:24:48 +00:00
|
|
|
</h5>
|
|
|
|
) : (
|
2022-06-21 21:42:29 +00:00
|
|
|
this.state.listCommunitiesResponse.match({
|
|
|
|
some: res => (
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="row">
|
|
|
|
<div className="col-12 col-lg-6 offset-lg-3 mb-4">
|
2022-06-21 21:42:29 +00:00
|
|
|
<h5>{i18n.t("create_post")}</h5>
|
|
|
|
<PostForm
|
|
|
|
post_view={None}
|
|
|
|
communities={Some(res.communities)}
|
|
|
|
onCreate={this.handlePostCreate}
|
|
|
|
params={Some(this.params)}
|
|
|
|
enableDownvotes={enableDownvotes(this.state.siteRes)}
|
|
|
|
enableNsfw={enableNsfw(this.state.siteRes)}
|
2022-09-22 15:14:58 +00:00
|
|
|
allLanguages={this.state.siteRes.all_languages}
|
2022-06-21 21:42:29 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
),
|
|
|
|
none: <></>,
|
|
|
|
})
|
2020-09-07 22:24:48 +00:00
|
|
|
)}
|
2020-09-06 16:15:25 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
get params(): PostFormParams {
|
|
|
|
let urlParams = new URLSearchParams(this.props.location.search);
|
2022-06-21 21:42:29 +00:00
|
|
|
let name = toOption(urlParams.get("community_name")).or(
|
|
|
|
this.prevCommunityName
|
|
|
|
);
|
|
|
|
let id = toOption(urlParams.get("community_id"))
|
|
|
|
.map(Number)
|
|
|
|
.or(this.prevCommunityId);
|
|
|
|
let nameOrId: Option<Either<string, number>>;
|
|
|
|
if (name.isSome()) {
|
|
|
|
nameOrId = Some(Left(name.unwrap()));
|
|
|
|
} else if (id.isSome()) {
|
|
|
|
nameOrId = Some(Right(id.unwrap()));
|
|
|
|
} else {
|
|
|
|
nameOrId = None;
|
|
|
|
}
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
let params: PostFormParams = {
|
2022-06-21 21:42:29 +00:00
|
|
|
name: toOption(urlParams.get("title")),
|
|
|
|
nameOrId,
|
|
|
|
body: toOption(urlParams.get("body")),
|
|
|
|
url: toOption(urlParams.get("url")),
|
2020-09-06 16:15:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return params;
|
|
|
|
}
|
|
|
|
|
2022-06-21 21:42:29 +00:00
|
|
|
get prevCommunityName(): Option<string> {
|
2020-09-06 16:15:25 +00:00
|
|
|
if (this.props.match.params.name) {
|
2022-06-21 21:42:29 +00:00
|
|
|
return toOption(this.props.match.params.name);
|
2020-09-06 16:15:25 +00:00
|
|
|
} else if (this.props.location.state) {
|
|
|
|
let lastLocation = this.props.location.state.prevPath;
|
2021-02-22 02:39:04 +00:00
|
|
|
if (lastLocation.includes("/c/")) {
|
2022-06-21 21:42:29 +00:00
|
|
|
return toOption(lastLocation.split("/c/")[1]);
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-21 21:42:29 +00:00
|
|
|
return None;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2022-06-21 21:42:29 +00:00
|
|
|
get prevCommunityId(): Option<number> {
|
2020-11-17 01:34:41 +00:00
|
|
|
if (this.props.match.params.id) {
|
2022-06-21 21:42:29 +00:00
|
|
|
return toOption(this.props.match.params.id);
|
2020-11-17 01:34:41 +00:00
|
|
|
}
|
2022-06-21 21:42:29 +00:00
|
|
|
return None;
|
2020-11-17 01:34:41 +00:00
|
|
|
}
|
|
|
|
|
2020-12-24 01:58:27 +00:00
|
|
|
handlePostCreate(post_view: PostView) {
|
|
|
|
this.props.history.push(`/post/${post_view.post.id}`);
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2020-11-12 21:56:46 +00:00
|
|
|
static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
|
2022-06-21 21:42:29 +00:00
|
|
|
let listCommunitiesForm = new ListCommunities({
|
|
|
|
type_: Some(ListingType.All),
|
|
|
|
sort: Some(SortType.TopAll),
|
|
|
|
limit: Some(fetchLimit),
|
|
|
|
page: None,
|
|
|
|
auth: req.auth,
|
|
|
|
});
|
2020-11-12 21:56:46 +00:00
|
|
|
return [req.client.listCommunities(listCommunitiesForm)];
|
2020-09-07 22:24:48 +00:00
|
|
|
}
|
|
|
|
|
2020-12-24 01:58:27 +00:00
|
|
|
parseMessage(msg: any) {
|
|
|
|
let op = wsUserOp(msg);
|
2021-04-07 15:54:38 +00:00
|
|
|
console.log(msg);
|
2020-09-06 16:15:25 +00:00
|
|
|
if (msg.error) {
|
2021-02-22 02:39:04 +00:00
|
|
|
toast(i18n.t(msg.error), "danger");
|
2020-09-06 16:15:25 +00:00
|
|
|
return;
|
2020-12-24 01:58:27 +00:00
|
|
|
} else if (op == UserOperation.ListCommunities) {
|
2022-06-21 21:42:29 +00:00
|
|
|
let data = wsJsonToRes<ListCommunitiesResponse>(
|
|
|
|
msg,
|
|
|
|
ListCommunitiesResponse
|
|
|
|
);
|
2022-09-22 15:03:35 +00:00
|
|
|
this.setState({ listCommunitiesResponse: Some(data), loading: false });
|
2021-04-23 21:48:31 +00:00
|
|
|
} else if (op == UserOperation.GetCommunity) {
|
2022-06-21 21:42:29 +00:00
|
|
|
let data = wsJsonToRes<GetCommunityResponse>(msg, GetCommunityResponse);
|
2022-09-22 15:03:35 +00:00
|
|
|
this.setState({
|
|
|
|
listCommunitiesResponse: Some({
|
|
|
|
communities: [data.community_view],
|
|
|
|
}),
|
|
|
|
loading: false,
|
2022-06-21 21:42:29 +00:00
|
|
|
});
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|