lemmy-ui/src/shared/components/post/create-post.tsx

224 lines
6 KiB
TypeScript
Raw Normal View History

2021-02-22 02:39:04 +00:00
import { Component } from "inferno";
import {
GetCommunity,
GetCommunityResponse,
GetSiteResponse,
ListCommunities,
ListCommunitiesResponse,
ListingType,
PostView,
SortType,
UserOperation,
wsJsonToRes,
wsUserOp,
} from "lemmy-js-client";
2021-02-22 02:39:04 +00:00
import { Subscription } from "rxjs";
import { InitialFetchRequest, PostFormParams } from "shared/interfaces";
import { i18n } from "../../i18next";
import { UserService, WebSocketService } from "../../services";
import {
enableDownvotes,
enableNsfw,
fetchLimit,
2020-09-08 18:44:55 +00:00
isBrowser,
myAuth,
setIsoData,
toast,
wsClient,
wsSubscribe,
} from "../../utils";
import { HtmlTags } from "../common/html-tags";
import { Spinner } from "../common/icon";
import { PostForm } from "./post-form";
interface CreatePostState {
listCommunitiesResponse?: ListCommunitiesResponse;
siteRes: GetSiteResponse;
loading: boolean;
}
export class CreatePost extends Component<any, CreatePostState> {
private isoData = setIsoData(this.context);
private subscription?: Subscription;
state: CreatePostState = {
siteRes: this.isoData.site_res,
loading: true,
};
constructor(props: any, context: any) {
super(props, context);
this.handlePostCreate = this.handlePostCreate.bind(this);
this.parseMessage = this.parseMessage.bind(this);
this.subscription = wsSubscribe(this.parseMessage);
if (!UserService.Instance.myUserInfo && isBrowser()) {
2021-02-22 02:39:04 +00:00
toast(i18n.t("not_logged_in"), "danger");
this.context.router.history.push(`/login`);
}
// Only fetch the data if coming from another route
if (this.isoData.path == this.context.router.route.match.url) {
this.state = {
...this.state,
listCommunitiesResponse: this.isoData
.routeData[0] as ListCommunitiesResponse,
loading: false,
};
} else {
this.refetch();
}
}
refetch() {
let nameOrId = this.params.nameOrId;
let auth = myAuth(false);
if (nameOrId) {
if (typeof nameOrId === "string") {
let form: GetCommunity = {
name: nameOrId,
auth,
};
WebSocketService.Instance.send(wsClient.getCommunity(form));
} else {
let form: GetCommunity = {
id: nameOrId,
auth,
};
WebSocketService.Instance.send(wsClient.getCommunity(form));
}
} else {
let listCommunitiesForm: ListCommunities = {
type_: ListingType.All,
sort: SortType.TopAll,
limit: fetchLimit,
auth,
};
WebSocketService.Instance.send(
wsClient.listCommunities(listCommunitiesForm)
);
}
}
componentWillUnmount() {
2020-09-08 18:44:55 +00:00
if (isBrowser()) {
this.subscription?.unsubscribe();
2020-09-08 18:44:55 +00:00
}
}
get documentTitle(): string {
2022-11-09 19:53:07 +00:00
return `${i18n.t("create_post")} - ${
this.state.siteRes.site_view.site.name
}`;
}
render() {
let res = this.state.listCommunitiesResponse;
return (
<div className="container-lg">
2020-09-11 18:09:21 +00:00
<HtmlTags
title={this.documentTitle}
path={this.context.router.route.match.url}
/>
{this.state.loading ? (
<h5>
2021-07-17 20:21:31 +00:00
<Spinner large />
</h5>
) : (
res && (
<div className="row">
<div className="col-12 col-lg-6 offset-lg-3 mb-4">
<h5>{i18n.t("create_post")}</h5>
<PostForm
communities={res.communities}
onCreate={this.handlePostCreate}
params={this.params}
enableDownvotes={enableDownvotes(this.state.siteRes)}
enableNsfw={enableNsfw(this.state.siteRes)}
allLanguages={this.state.siteRes.all_languages}
siteLanguages={this.state.siteRes.discussion_languages}
/>
</div>
</div>
)
)}
</div>
);
}
get params(): PostFormParams {
let urlParams = new URLSearchParams(this.props.location.search);
let name = urlParams.get("community_name") ?? this.prevCommunityName;
let communityIdParam = urlParams.get("community_id");
let id = communityIdParam ? Number(communityIdParam) : this.prevCommunityId;
let nameOrId: string | number | undefined;
if (name) {
nameOrId = name;
} else if (id) {
nameOrId = id;
}
let params: PostFormParams = {
name: urlParams.get("title") ?? undefined,
nameOrId,
body: urlParams.get("body") ?? undefined,
url: urlParams.get("url") ?? undefined,
};
return params;
}
get prevCommunityName(): string | undefined {
if (this.props.match.params.name) {
return this.props.match.params.name;
} 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/")) {
return lastLocation.split("/c/").at(1);
}
}
return undefined;
}
get prevCommunityId(): number | undefined {
// TODO is this actually a number? Whats the real return type
let id = this.props.match.params.id;
return id ?? undefined;
}
2020-12-24 01:58:27 +00:00
handlePostCreate(post_view: PostView) {
this.props.history.push(`/post/${post_view.post.id}`);
}
static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
let listCommunitiesForm: ListCommunities = {
type_: ListingType.All,
sort: SortType.TopAll,
limit: fetchLimit,
auth: req.auth,
};
return [req.client.listCommunities(listCommunitiesForm)];
}
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);
if (msg.error) {
2021-02-22 02:39:04 +00:00
toast(i18n.t(msg.error), "danger");
return;
2020-12-24 01:58:27 +00:00
} else if (op == UserOperation.ListCommunities) {
let data = wsJsonToRes<ListCommunitiesResponse>(msg);
this.setState({ listCommunitiesResponse: data, loading: false });
} else if (op == UserOperation.GetCommunity) {
let data = wsJsonToRes<GetCommunityResponse>(msg);
this.setState({
listCommunitiesResponse: {
communities: [data.community_view],
},
loading: false,
});
}
}
}