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

253 lines
6.2 KiB
TypeScript
Raw Normal View History

2021-02-22 02:39:04 +00:00
import { Component } from "inferno";
import { RouteComponentProps } from "inferno-router/dist/Route";
import {
GetCommunity,
GetCommunityResponse,
GetSiteResponse,
PostView,
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 { WebSocketService } from "../../services";
import {
Choice,
2023-05-11 17:06:32 +00:00
QueryParams,
2023-05-30 00:40:00 +00:00
WithPromiseKeys,
enableDownvotes,
enableNsfw,
getIdFromString,
getQueryParams,
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";
export interface CreatePostProps {
communityId?: number;
}
2023-05-30 00:40:00 +00:00
interface CreatePostData {
communityResponse?: GetCommunityResponse;
}
function getCreatePostQueryParams() {
return getQueryParams<CreatePostProps>({
communityId: getIdFromString,
});
}
interface CreatePostState {
siteRes: GetSiteResponse;
loading: boolean;
selectedCommunityChoice?: Choice;
}
export class CreatePost extends Component<
RouteComponentProps<Record<string, never>>,
CreatePostState
> {
2023-05-30 00:40:00 +00:00
private isoData = setIsoData<CreatePostData>(this.context);
private subscription?: Subscription;
state: CreatePostState = {
siteRes: this.isoData.site_res,
loading: true,
};
constructor(props: RouteComponentProps<Record<string, never>>, context: any) {
super(props, context);
this.handlePostCreate = this.handlePostCreate.bind(this);
this.handleSelectedCommunityChange =
this.handleSelectedCommunityChange.bind(this);
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) {
2023-05-30 00:40:00 +00:00
const { communityResponse } = this.isoData.routeData;
2023-05-30 00:40:00 +00:00
if (communityResponse) {
const communityChoice: Choice = {
label: communityResponse.community_view.community.title,
2023-05-30 00:40:00 +00:00
value: communityResponse.community_view.community.id.toString(),
};
this.state = {
...this.state,
selectedCommunityChoice: communityChoice,
};
}
this.state = {
...this.state,
loading: false,
};
} else {
this.fetchCommunity();
}
}
fetchCommunity() {
const { communityId } = getCreatePostQueryParams();
const auth = myAuth(false);
if (communityId) {
const form: GetCommunity = {
id: communityId,
auth,
};
WebSocketService.Instance.send(wsClient.getCommunity(form));
}
}
componentDidMount(): void {
const { communityId } = getCreatePostQueryParams();
if (communityId?.toString() !== this.state.selectedCommunityChoice?.value) {
this.fetchCommunity();
} else if (!communityId) {
this.setState({
selectedCommunityChoice: undefined,
loading: false,
});
}
}
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() {
const { selectedCommunityChoice } = this.state;
const locationState = this.props.history.location.state as
| PostFormParams
| undefined;
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>
) : (
<div className="row">
<div className="col-12 col-lg-6 offset-lg-3 mb-4">
<h5>{i18n.t("create_post")}</h5>
<PostForm
onCreate={this.handlePostCreate}
params={locationState}
enableDownvotes={enableDownvotes(this.state.siteRes)}
enableNsfw={enableNsfw(this.state.siteRes)}
allLanguages={this.state.siteRes.all_languages}
siteLanguages={this.state.siteRes.discussion_languages}
selectedCommunityChoice={selectedCommunityChoice}
onSelectCommunity={this.handleSelectedCommunityChange}
/>
</div>
</div>
)}
</div>
);
}
updateUrl({ communityId }: Partial<CreatePostProps>) {
const { communityId: urlCommunityId } = getCreatePostQueryParams();
const locationState = this.props.history.location.state as
| PostFormParams
| undefined;
const url = new URL(location.href);
const newId = (communityId ?? urlCommunityId)?.toString();
if (newId !== undefined) {
url.searchParams.set("communityId", newId);
} else {
url.searchParams.delete("communityId");
}
history.replaceState(locationState, "", url);
this.fetchCommunity();
}
handleSelectedCommunityChange(choice: Choice) {
this.updateUrl({
communityId: getIdFromString(choice?.value),
});
}
2020-12-24 01:58:27 +00:00
handlePostCreate(post_view: PostView) {
this.props.history.replace(`/post/${post_view.post.id}`);
}
static fetchInitialData({
client,
query: { communityId },
auth,
2023-05-30 00:40:00 +00:00
}: InitialFetchRequest<
QueryParams<CreatePostProps>
>): WithPromiseKeys<CreatePostData> {
const data: WithPromiseKeys<CreatePostData> = {};
if (communityId) {
const form: GetCommunity = {
auth,
id: getIdFromString(communityId),
};
2023-05-30 00:40:00 +00:00
data.communityResponse = client.getCommunity(form);
}
2023-05-30 00:40:00 +00:00
return data;
}
2020-12-24 01:58:27 +00:00
parseMessage(msg: any) {
const 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;
}
if (op === UserOperation.GetCommunity) {
const {
community_view: {
community: { title, id },
},
} = wsJsonToRes<GetCommunityResponse>(msg);
this.setState({
selectedCommunityChoice: { label: title, value: id.toString() },
loading: false,
});
}
}
}