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

250 lines
6.2 KiB
TypeScript
Raw Normal View History

2021-02-22 02:39:04 +00:00
import { Component } from "inferno";
import { Redirect } from "inferno-router";
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 { UserService, WebSocketService } from "../../services";
import {
Choice,
2023-05-11 17:06:32 +00:00
QueryParams,
enableDownvotes,
enableNsfw,
getIdFromString,
getQueryParams,
getQueryString,
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;
}
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
> {
private isoData = setIsoData(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) {
const communityRes = this.isoData.routeData[0] as
| GetCommunityResponse
| undefined;
if (communityRes) {
const communityChoice: Choice = {
label: communityRes.community_view.community.name,
value: communityRes.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">
{!UserService.Instance.myUserInfo && <Redirect to="/login" />}
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 queryParams: QueryParams<CreatePostProps> = {
communityId: (communityId ?? urlCommunityId)?.toString(),
};
const locationState = this.props.history.location.state as
| PostFormParams
| undefined;
this.props.history.replace(
`/create_post${getQueryString(queryParams)}`,
locationState
);
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,
}: InitialFetchRequest<QueryParams<CreatePostProps>>): Promise<any>[] {
const promises: Promise<any>[] = [];
if (communityId) {
const form: GetCommunity = {
auth,
id: getIdFromString(communityId),
};
promises.push(client.getCommunity(form));
} else {
promises.push(Promise.resolve());
}
return promises;
}
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: { name, id },
},
} = wsJsonToRes<GetCommunityResponse>(msg);
this.setState({
selectedCommunityChoice: { label: name, value: id.toString() },
loading: false,
});
}
}
}