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 {
|
|
|
|
GetCommunity,
|
|
|
|
GetCommunityResponse,
|
2022-06-21 21:42:29 +00:00
|
|
|
GetSiteResponse,
|
2021-07-17 20:42:55 +00:00
|
|
|
PostView,
|
|
|
|
UserOperation,
|
2022-06-21 21:42:29 +00:00
|
|
|
wsJsonToRes,
|
|
|
|
wsUserOp,
|
2021-07-17 20:42:55 +00:00
|
|
|
} from "lemmy-js-client";
|
2021-02-22 02:39:04 +00:00
|
|
|
import { Subscription } from "rxjs";
|
2021-07-17 20:42:55 +00:00
|
|
|
import { InitialFetchRequest, PostFormParams } from "shared/interfaces";
|
|
|
|
import { i18n } from "../../i18next";
|
2023-05-14 19:28:38 +00:00
|
|
|
import { WebSocketService } from "../../services";
|
2020-09-07 22:24:48 +00:00
|
|
|
import {
|
2023-04-15 14:47:10 +00:00
|
|
|
Choice,
|
2023-05-11 17:06:32 +00:00
|
|
|
QueryParams,
|
2022-06-21 21:42:29 +00:00
|
|
|
enableDownvotes,
|
|
|
|
enableNsfw,
|
2023-04-15 14:47:10 +00:00
|
|
|
getIdFromString,
|
|
|
|
getQueryParams,
|
|
|
|
getQueryString,
|
2020-09-08 18:44:55 +00:00
|
|
|
isBrowser,
|
2023-01-04 16:56:24 +00:00
|
|
|
myAuth,
|
2020-09-07 22:24:48 +00:00
|
|
|
setIsoData,
|
|
|
|
toast,
|
2020-12-24 22:05:57 +00:00
|
|
|
wsClient,
|
2020-09-07 22:24:48 +00:00
|
|
|
wsSubscribe,
|
2021-07-17 20:42:55 +00:00
|
|
|
} from "../../utils";
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getCreatePostQueryParams() {
|
|
|
|
return getQueryParams<CreatePostProps>({
|
|
|
|
communityId: getIdFromString,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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;
|
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-01-04 16:56:24 +00:00
|
|
|
private isoData = setIsoData(this.context);
|
|
|
|
private subscription?: Subscription;
|
|
|
|
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,
|
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
|
|
|
|
|
|
|
this.parseMessage = this.parseMessage.bind(this);
|
|
|
|
this.subscription = wsSubscribe(this.parseMessage);
|
|
|
|
|
2020-09-07 22:24:48 +00:00
|
|
|
// Only fetch the data if coming from another route
|
2023-04-15 14:47:10 +00:00
|
|
|
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,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-09-22 15:03:35 +00:00
|
|
|
this.state = {
|
|
|
|
...this.state,
|
|
|
|
loading: false,
|
|
|
|
};
|
2020-09-07 22:24:48 +00:00
|
|
|
} else {
|
2023-04-15 14:47:10 +00:00
|
|
|
this.fetchCommunity();
|
2020-09-07 22:24:48 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
fetchCommunity() {
|
|
|
|
const { communityId } = getCreatePostQueryParams();
|
|
|
|
const auth = myAuth(false);
|
|
|
|
|
|
|
|
if (communityId) {
|
|
|
|
const form: GetCommunity = {
|
|
|
|
id: communityId,
|
2023-01-04 16:56:24 +00:00
|
|
|
auth,
|
|
|
|
};
|
2023-04-15 14:47:10 +00:00
|
|
|
|
|
|
|
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,
|
|
|
|
});
|
2023-01-04 16:56:24 +00:00
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
2020-09-08 18:44:55 +00:00
|
|
|
if (isBrowser()) {
|
2023-01-04 16:56:24 +00:00
|
|
|
this.subscription?.unsubscribe();
|
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">
|
|
|
|
<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}
|
|
|
|
/>
|
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-04-15 14:47:10 +00:00
|
|
|
updateUrl({ communityId }: Partial<CreatePostProps>) {
|
|
|
|
const { communityId: urlCommunityId } = getCreatePostQueryParams();
|
2022-06-21 21:42:29 +00:00
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
const queryParams: QueryParams<CreatePostProps> = {
|
|
|
|
communityId: (communityId ?? urlCommunityId)?.toString(),
|
2020-09-06 16:15:25 +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-03 16:09:47 +00:00
|
|
|
this.props.history.replace(
|
2023-04-15 14:47:10 +00:00
|
|
|
`/create_post${getQueryString(queryParams)}`,
|
|
|
|
locationState
|
|
|
|
);
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-12-24 01:58:27 +00:00
|
|
|
handlePostCreate(post_view: PostView) {
|
2023-04-15 14:47:10 +00:00
|
|
|
this.props.history.replace(`/post/${post_view.post.id}`);
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
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-09-07 22:24:48 +00:00
|
|
|
}
|
|
|
|
|
2020-12-24 01:58:27 +00:00
|
|
|
parseMessage(msg: any) {
|
2023-04-15 14:47:10 +00:00
|
|
|
const op = wsUserOp(msg);
|
2021-04-07 15:54:38 +00:00
|
|
|
console.log(msg);
|
2020-09-06 16:15:25 +00:00
|
|
|
if (msg.error) {
|
2021-02-22 02:39:04 +00:00
|
|
|
toast(i18n.t(msg.error), "danger");
|
2020-09-06 16:15:25 +00:00
|
|
|
return;
|
2023-04-15 14:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (op === UserOperation.GetCommunity) {
|
|
|
|
const {
|
|
|
|
community_view: {
|
|
|
|
community: { name, id },
|
2023-01-04 16:56:24 +00:00
|
|
|
},
|
2023-04-15 14:47:10 +00:00
|
|
|
} = wsJsonToRes<GetCommunityResponse>(msg);
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
selectedCommunityChoice: { label: name, value: id.toString() },
|
2022-09-22 15:03:35 +00:00
|
|
|
loading: false,
|
2022-06-21 21:42:29 +00:00
|
|
|
});
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|