2021-02-22 02:39:04 +00:00
|
|
|
import { Component } from "inferno";
|
2023-04-15 14:47:10 +00:00
|
|
|
import { RouteComponentProps } from "inferno-router/dist/Route";
|
2021-07-17 20:42:55 +00:00
|
|
|
import {
|
2023-06-14 12:20:40 +00:00
|
|
|
CreatePost as CreatePostI,
|
2021-07-17 20:42:55 +00:00
|
|
|
GetCommunity,
|
|
|
|
GetCommunityResponse,
|
2022-06-21 21:42:29 +00:00
|
|
|
GetSiteResponse,
|
2023-06-14 12:20:40 +00:00
|
|
|
ListCommunitiesResponse,
|
2021-07-17 20:42:55 +00:00
|
|
|
} from "lemmy-js-client";
|
|
|
|
import { i18n } from "../../i18next";
|
2023-06-13 10:33:27 +00:00
|
|
|
import { InitialFetchRequest, PostFormParams } from "../../interfaces";
|
2023-06-14 12:20:40 +00:00
|
|
|
import { FirstLoadService } from "../../services/FirstLoadService";
|
|
|
|
import {
|
|
|
|
HttpService,
|
|
|
|
RequestState,
|
|
|
|
WrappedLemmyHttp,
|
|
|
|
} from "../../services/HttpService";
|
2020-09-07 22:24:48 +00:00
|
|
|
import {
|
2023-04-15 14:47:10 +00:00
|
|
|
Choice,
|
2023-06-16 02:08:14 +00:00
|
|
|
RouteDataResponse,
|
2022-06-21 21:42:29 +00:00
|
|
|
enableDownvotes,
|
|
|
|
enableNsfw,
|
2023-04-15 14:47:10 +00:00
|
|
|
getIdFromString,
|
2023-01-04 16:56:24 +00:00
|
|
|
myAuth,
|
2020-09-07 22:24:48 +00:00
|
|
|
setIsoData,
|
2021-07-17 20:42:55 +00:00
|
|
|
} from "../../utils";
|
2023-06-20 01:48:38 +00:00
|
|
|
import getQueryParams from "../../utils/helpers/get-query-params";
|
2023-06-16 21:25:53 +00:00
|
|
|
import type { QueryParams } from "../../utils/types/query-params";
|
2021-07-17 20:42:55 +00:00
|
|
|
import { HtmlTags } from "../common/html-tags";
|
|
|
|
import { Spinner } from "../common/icon";
|
|
|
|
import { PostForm } from "./post-form";
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
export interface CreatePostProps {
|
|
|
|
communityId?: number;
|
|
|
|
}
|
|
|
|
|
2023-06-16 02:08:14 +00:00
|
|
|
type CreatePostData = RouteDataResponse<{
|
2023-06-16 22:12:14 +00:00
|
|
|
communityResponse: GetCommunityResponse;
|
2023-06-16 02:08:14 +00:00
|
|
|
initialCommunitiesRes: ListCommunitiesResponse;
|
|
|
|
}>;
|
2023-05-30 00:40:00 +00:00
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
function getCreatePostQueryParams() {
|
|
|
|
return getQueryParams<CreatePostProps>({
|
|
|
|
communityId: getIdFromString,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-06-14 12:20:40 +00:00
|
|
|
function fetchCommunitiesForOptions(client: WrappedLemmyHttp) {
|
|
|
|
return client.listCommunities({ limit: 30, sort: "TopMonth", type_: "All" });
|
|
|
|
}
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
interface CreatePostState {
|
2022-06-21 21:42:29 +00:00
|
|
|
siteRes: GetSiteResponse;
|
2020-09-07 22:24:48 +00:00
|
|
|
loading: boolean;
|
2023-04-15 14:47:10 +00:00
|
|
|
selectedCommunityChoice?: Choice;
|
2023-06-14 12:20:40 +00:00
|
|
|
initialCommunitiesRes: RequestState<ListCommunitiesResponse>;
|
|
|
|
isIsomorphic: boolean;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
export class CreatePost extends Component<
|
|
|
|
RouteComponentProps<Record<string, never>>,
|
|
|
|
CreatePostState
|
|
|
|
> {
|
2023-05-30 00:40:00 +00:00
|
|
|
private isoData = setIsoData<CreatePostData>(this.context);
|
2023-01-04 16:56:24 +00:00
|
|
|
state: CreatePostState = {
|
2022-06-21 21:42:29 +00:00
|
|
|
siteRes: this.isoData.site_res,
|
2020-09-07 22:24:48 +00:00
|
|
|
loading: true,
|
2023-06-14 12:20:40 +00:00
|
|
|
initialCommunitiesRes: { state: "empty" },
|
|
|
|
isIsomorphic: false,
|
2020-09-06 16:15:25 +00:00
|
|
|
};
|
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
constructor(props: RouteComponentProps<Record<string, never>>, context: any) {
|
2020-09-06 16:15:25 +00:00
|
|
|
super(props, context);
|
|
|
|
|
2022-09-22 15:03:35 +00:00
|
|
|
this.handlePostCreate = this.handlePostCreate.bind(this);
|
2023-04-15 14:47:10 +00:00
|
|
|
this.handleSelectedCommunityChange =
|
|
|
|
this.handleSelectedCommunityChange.bind(this);
|
2022-09-22 15:03:35 +00:00
|
|
|
|
2020-09-07 22:24:48 +00:00
|
|
|
// Only fetch the data if coming from another route
|
2023-06-14 12:20:40 +00:00
|
|
|
if (FirstLoadService.isFirstLoad) {
|
2023-06-16 02:08:14 +00:00
|
|
|
const { communityResponse: communityRes, initialCommunitiesRes } =
|
|
|
|
this.isoData.routeData;
|
2023-04-15 14:47:10 +00:00
|
|
|
|
2023-06-16 02:39:04 +00:00
|
|
|
this.state = {
|
|
|
|
...this.state,
|
|
|
|
loading: false,
|
|
|
|
initialCommunitiesRes,
|
|
|
|
isIsomorphic: true,
|
|
|
|
};
|
2023-04-15 14:47:10 +00:00
|
|
|
|
2023-06-14 12:20:40 +00:00
|
|
|
if (communityRes?.state === "success") {
|
2023-04-15 14:47:10 +00:00
|
|
|
const communityChoice: Choice = {
|
2023-06-14 12:20:40 +00:00
|
|
|
label: communityRes.data.community_view.community.title,
|
|
|
|
value: communityRes.data.community_view.community.id.toString(),
|
2023-04-15 14:47:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
...this.state,
|
|
|
|
selectedCommunityChoice: communityChoice,
|
|
|
|
};
|
|
|
|
}
|
2020-09-07 22:24:48 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2023-06-14 12:20:40 +00:00
|
|
|
async fetchCommunity() {
|
2023-04-15 14:47:10 +00:00
|
|
|
const { communityId } = getCreatePostQueryParams();
|
2023-06-14 12:20:40 +00:00
|
|
|
const auth = myAuth();
|
2023-04-15 14:47:10 +00:00
|
|
|
|
|
|
|
if (communityId) {
|
2023-06-14 12:20:40 +00:00
|
|
|
const res = await HttpService.client.getCommunity({
|
2023-04-15 14:47:10 +00:00
|
|
|
id: communityId,
|
2023-01-04 16:56:24 +00:00
|
|
|
auth,
|
2023-06-14 12:20:40 +00:00
|
|
|
});
|
|
|
|
if (res.state === "success") {
|
|
|
|
this.setState({
|
|
|
|
selectedCommunityChoice: {
|
|
|
|
label: res.data.community_view.community.name,
|
|
|
|
value: res.data.community_view.community.id.toString(),
|
|
|
|
},
|
|
|
|
loading: false,
|
|
|
|
});
|
|
|
|
}
|
2023-04-15 14:47:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-14 12:20:40 +00:00
|
|
|
async componentDidMount() {
|
|
|
|
// TODO test this
|
|
|
|
if (!this.state.isIsomorphic) {
|
|
|
|
const { communityId } = getCreatePostQueryParams();
|
|
|
|
|
|
|
|
const initialCommunitiesRes = await fetchCommunitiesForOptions(
|
|
|
|
HttpService.client
|
|
|
|
);
|
2023-04-15 14:47:10 +00:00
|
|
|
|
|
|
|
this.setState({
|
2023-06-14 12:20:40 +00:00
|
|
|
initialCommunitiesRes,
|
2023-04-15 14:47:10 +00:00
|
|
|
});
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2023-06-14 12:20:40 +00:00
|
|
|
if (
|
|
|
|
communityId?.toString() !== this.state.selectedCommunityChoice?.value
|
|
|
|
) {
|
|
|
|
await this.fetchCommunity();
|
|
|
|
} else if (!communityId) {
|
|
|
|
this.setState({
|
|
|
|
selectedCommunityChoice: undefined,
|
|
|
|
loading: false,
|
|
|
|
});
|
|
|
|
}
|
2020-09-08 18:44:55 +00:00
|
|
|
}
|
2020-09-06 16:15:25 +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
|
|
|
|
}`;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2023-04-15 14:47:10 +00:00
|
|
|
const { selectedCommunityChoice } = this.state;
|
|
|
|
|
|
|
|
const locationState = this.props.history.location.state as
|
|
|
|
| PostFormParams
|
|
|
|
| undefined;
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
return (
|
2022-10-03 18:16:36 +00:00
|
|
|
<div className="container-lg">
|
2020-09-11 18:09:21 +00:00
|
|
|
<HtmlTags
|
|
|
|
title={this.documentTitle}
|
|
|
|
path={this.context.router.route.match.url}
|
|
|
|
/>
|
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>
|
|
|
|
) : (
|
2023-04-15 14:47:10 +00:00
|
|
|
<div className="row">
|
2023-06-16 17:12:07 +00:00
|
|
|
<div
|
|
|
|
id="createPostForm"
|
|
|
|
className="col-12 col-lg-6 offset-lg-3 mb-4"
|
|
|
|
>
|
2023-04-15 14:47:10 +00:00
|
|
|
<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}
|
2023-06-14 12:20:40 +00:00
|
|
|
initialCommunities={
|
|
|
|
this.state.initialCommunitiesRes.state === "success"
|
|
|
|
? this.state.initialCommunitiesRes.data.communities
|
|
|
|
: []
|
|
|
|
}
|
2023-04-15 14:47:10 +00:00
|
|
|
/>
|
2023-01-04 16:56:24 +00:00
|
|
|
</div>
|
2023-04-15 14:47:10 +00:00
|
|
|
</div>
|
2020-09-07 22:24:48 +00:00
|
|
|
)}
|
2020-09-06 16:15:25 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-06-14 12:20:40 +00:00
|
|
|
async updateUrl({ communityId }: Partial<CreatePostProps>) {
|
2023-04-15 14:47:10 +00:00
|
|
|
const { communityId: urlCommunityId } = getCreatePostQueryParams();
|
2022-06-21 21:42:29 +00:00
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
const locationState = this.props.history.location.state as
|
|
|
|
| PostFormParams
|
|
|
|
| undefined;
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2023-05-29 13:01:52 +00:00
|
|
|
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);
|
2023-04-15 14:47:10 +00:00
|
|
|
|
2023-06-14 12:20:40 +00:00
|
|
|
await this.fetchCommunity();
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
handleSelectedCommunityChange(choice: Choice) {
|
|
|
|
this.updateUrl({
|
|
|
|
communityId: getIdFromString(choice?.value),
|
|
|
|
});
|
2020-11-17 01:34:41 +00:00
|
|
|
}
|
|
|
|
|
2023-06-14 12:20:40 +00:00
|
|
|
async handlePostCreate(form: CreatePostI) {
|
|
|
|
const res = await HttpService.client.createPost(form);
|
|
|
|
|
|
|
|
if (res.state === "success") {
|
|
|
|
const postId = res.data.post_view.post.id;
|
|
|
|
this.props.history.replace(`/post/${postId}`);
|
2023-06-16 15:02:28 +00:00
|
|
|
} else {
|
|
|
|
this.setState({
|
|
|
|
loading: false,
|
|
|
|
});
|
2023-06-14 12:20:40 +00:00
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2023-06-16 02:08:14 +00:00
|
|
|
static async fetchInitialData({
|
2023-04-15 14:47:10 +00:00
|
|
|
client,
|
|
|
|
query: { communityId },
|
|
|
|
auth,
|
2023-05-30 00:40:00 +00:00
|
|
|
}: InitialFetchRequest<
|
|
|
|
QueryParams<CreatePostProps>
|
2023-06-16 02:08:14 +00:00
|
|
|
>): Promise<CreatePostData> {
|
|
|
|
const data: CreatePostData = {
|
|
|
|
initialCommunitiesRes: await fetchCommunitiesForOptions(client),
|
2023-06-16 22:12:14 +00:00
|
|
|
communityResponse: { state: "empty" },
|
2023-06-16 02:08:14 +00:00
|
|
|
};
|
2023-04-15 14:47:10 +00:00
|
|
|
|
|
|
|
if (communityId) {
|
|
|
|
const form: GetCommunity = {
|
|
|
|
auth,
|
|
|
|
id: getIdFromString(communityId),
|
|
|
|
};
|
|
|
|
|
2023-06-16 02:08:14 +00:00
|
|
|
data.communityResponse = await client.getCommunity(form);
|
2023-04-15 14:47:10 +00:00
|
|
|
}
|
|
|
|
|
2023-05-30 00:40:00 +00:00
|
|
|
return data;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
}
|