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

185 lines
5 KiB
TypeScript
Raw Normal View History

2021-02-22 02:39:04 +00:00
import { Component } from "inferno";
import { Subscription } from "rxjs";
import { PostForm } from "./post-form";
import { HtmlTags } from "./html-tags";
import { Spinner } from "./icon";
import {
authField,
2020-09-08 18:44:55 +00:00
isBrowser,
setIsoData,
setOptionalAuth,
toast,
wsClient,
wsJsonToRes,
wsSubscribe,
2020-12-24 01:58:27 +00:00
wsUserOp,
2021-02-22 02:39:04 +00:00
} from "../utils";
import { UserService, WebSocketService } from "../services";
import {
UserOperation,
ListCommunitiesResponse,
2020-12-24 01:58:27 +00:00
CommunityView,
SiteView,
ListCommunities,
SortType,
ListingType,
2020-12-24 01:58:27 +00:00
PostView,
2021-02-22 02:39:04 +00:00
} from "lemmy-js-client";
import { i18n } from "../i18next";
import { InitialFetchRequest, PostFormParams } from "shared/interfaces";
interface CreatePostState {
2020-12-24 01:58:27 +00:00
site_view: SiteView;
communities: CommunityView[];
loading: boolean;
}
export class CreatePost extends Component<any, CreatePostState> {
private isoData = setIsoData(this.context);
private subscription: Subscription;
private emptyState: CreatePostState = {
2020-12-24 01:58:27 +00:00
site_view: this.isoData.site_res.site_view,
communities: [],
loading: true,
};
constructor(props: any, context: any) {
super(props, context);
this.handlePostCreate = this.handlePostCreate.bind(this);
this.state = this.emptyState;
2020-09-09 20:33:40 +00:00
if (!UserService.Instance.user && isBrowser()) {
2021-02-22 02:39:04 +00:00
toast(i18n.t("not_logged_in"), "danger");
this.context.router.history.push(`/login`);
}
this.parseMessage = this.parseMessage.bind(this);
this.subscription = wsSubscribe(this.parseMessage);
// Only fetch the data if coming from another route
if (this.isoData.path == this.context.router.route.match.url) {
this.state.communities = this.isoData.routeData[0].communities;
this.state.loading = false;
} else {
this.refetch();
}
}
refetch() {
2020-12-24 01:58:27 +00:00
let listCommunitiesForm: ListCommunities = {
type_: ListingType.All,
sort: SortType.TopAll,
limit: 9999,
auth: authField(false),
};
WebSocketService.Instance.send(
wsClient.listCommunities(listCommunitiesForm)
);
}
componentWillUnmount() {
2020-09-08 18:44:55 +00:00
if (isBrowser()) {
this.subscription.unsubscribe();
}
}
get documentTitle(): string {
2021-02-22 02:39:04 +00:00
return `${i18n.t("create_post")} - ${this.state.site_view.site.name}`;
}
render() {
return (
<div class="container">
2020-09-11 18:09:21 +00:00
<HtmlTags
title={this.documentTitle}
path={this.context.router.route.match.url}
/>
{this.state.loading ? (
<h5>
2021-02-11 20:35:27 +00:00
<Spinner />
</h5>
) : (
<div class="row">
<div class="col-12 col-lg-6 offset-lg-3 mb-4">
2021-02-22 02:39:04 +00:00
<h5>{i18n.t("create_post")}</h5>
<PostForm
communities={this.state.communities}
onCreate={this.handlePostCreate}
params={this.params}
2020-12-24 01:58:27 +00:00
enableDownvotes={this.state.site_view.site.enable_downvotes}
enableNsfw={this.state.site_view.site.enable_nsfw}
/>
</div>
</div>
)}
</div>
);
}
get params(): PostFormParams {
let urlParams = new URLSearchParams(this.props.location.search);
let params: PostFormParams = {
2021-02-22 02:39:04 +00:00
name: urlParams.get("title"),
community_name: urlParams.get("community_name") || this.prevCommunityName,
community_id: urlParams.get("community_id")
? Number(urlParams.get("community_id")) || this.prevCommunityId
: null,
2021-02-22 02:39:04 +00:00
body: urlParams.get("body"),
url: urlParams.get("url"),
};
return params;
}
get prevCommunityName(): string {
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/")[1];
}
}
return null;
}
get prevCommunityId(): number {
if (this.props.match.params.id) {
return this.props.match.params.id;
} else if (this.props.location.state) {
let lastLocation = this.props.location.state.prevPath;
2021-02-22 02:39:04 +00:00
if (lastLocation.includes("/community/")) {
return Number(lastLocation.split("/community/")[1]);
}
}
return null;
}
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>[] {
2020-12-24 01:58:27 +00:00
let listCommunitiesForm: ListCommunities = {
type_: ListingType.All,
sort: SortType.TopAll,
limit: 9999,
};
setOptionalAuth(listCommunitiesForm, req.auth);
return [req.client.listCommunities(listCommunitiesForm)];
}
2020-12-24 01:58:27 +00:00
parseMessage(msg: any) {
let op = wsUserOp(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).data;
this.state.communities = data.communities;
this.state.loading = false;
this.setState(this.state);
}
}
}