2023-06-21 22:28:24 +00:00
|
|
|
import { getRecipientIdFromProps, myAuth, setIsoData } from "@utils/app";
|
|
|
|
import { RouteDataResponse } from "@utils/types";
|
2021-02-22 02:39:04 +00:00
|
|
|
import { Component } from "inferno";
|
2020-09-06 16:15:25 +00:00
|
|
|
import {
|
2023-06-14 12:20:40 +00:00
|
|
|
CreatePrivateMessage as CreatePrivateMessageI,
|
2021-07-17 20:42:55 +00:00
|
|
|
GetPersonDetails,
|
2021-03-15 18:09:31 +00:00
|
|
|
GetPersonDetailsResponse,
|
2022-06-21 21:42:29 +00:00
|
|
|
GetSiteResponse,
|
2021-02-22 02:39:04 +00:00
|
|
|
} from "lemmy-js-client";
|
2021-07-17 20:42:55 +00:00
|
|
|
import { i18n } from "../../i18next";
|
|
|
|
import { InitialFetchRequest } from "../../interfaces";
|
2023-06-14 12:20:40 +00:00
|
|
|
import { FirstLoadService } from "../../services/FirstLoadService";
|
|
|
|
import { HttpService, RequestState } from "../../services/HttpService";
|
2023-06-21 22:28:24 +00:00
|
|
|
import { toast } from "../../toast";
|
2021-07-17 20:42:55 +00:00
|
|
|
import { HtmlTags } from "../common/html-tags";
|
|
|
|
import { Spinner } from "../common/icon";
|
|
|
|
import { PrivateMessageForm } from "./private-message-form";
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2023-06-16 02:08:14 +00:00
|
|
|
type CreatePrivateMessageData = RouteDataResponse<{
|
2023-05-30 00:40:00 +00:00
|
|
|
recipientDetailsResponse: GetPersonDetailsResponse;
|
2023-06-16 02:08:14 +00:00
|
|
|
}>;
|
2023-05-30 00:40:00 +00:00
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
interface CreatePrivateMessageState {
|
2022-06-21 21:42:29 +00:00
|
|
|
siteRes: GetSiteResponse;
|
2023-06-14 12:20:40 +00:00
|
|
|
recipientRes: RequestState<GetPersonDetailsResponse>;
|
|
|
|
recipientId: number;
|
|
|
|
isIsomorphic: boolean;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class CreatePrivateMessage extends Component<
|
2021-02-22 02:24:09 +00:00
|
|
|
any,
|
2020-09-06 16:15:25 +00:00
|
|
|
CreatePrivateMessageState
|
|
|
|
> {
|
2023-05-30 00:40:00 +00:00
|
|
|
private isoData = setIsoData<CreatePrivateMessageData>(this.context);
|
2023-01-04 16:56:24 +00:00
|
|
|
state: CreatePrivateMessageState = {
|
2022-06-21 21:42:29 +00:00
|
|
|
siteRes: this.isoData.site_res,
|
2023-06-14 12:20:40 +00:00
|
|
|
recipientRes: { state: "empty" },
|
|
|
|
recipientId: getRecipientIdFromProps(this.props),
|
|
|
|
isIsomorphic: false,
|
2020-09-06 16:15:25 +00:00
|
|
|
};
|
2022-09-22 15:03:35 +00:00
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
constructor(props: any, context: any) {
|
|
|
|
super(props, context);
|
2021-07-17 20:21:31 +00:00
|
|
|
this.handlePrivateMessageCreate =
|
|
|
|
this.handlePrivateMessageCreate.bind(this);
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2020-09-08 18:44:55 +00:00
|
|
|
// Only fetch the data if coming from another route
|
2023-06-14 12:20:40 +00:00
|
|
|
if (FirstLoadService.isFirstLoad) {
|
2022-09-22 15:03:35 +00:00
|
|
|
this.state = {
|
|
|
|
...this.state,
|
2023-06-16 02:08:14 +00:00
|
|
|
recipientRes: this.isoData.routeData.recipientDetailsResponse,
|
2023-06-14 12:20:40 +00:00
|
|
|
isIsomorphic: true,
|
2022-09-22 15:03:35 +00:00
|
|
|
};
|
2020-09-08 18:44:55 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2023-06-14 12:20:40 +00:00
|
|
|
async componentDidMount() {
|
|
|
|
if (!this.state.isIsomorphic) {
|
|
|
|
await this.fetchPersonDetails();
|
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2023-06-16 02:08:14 +00:00
|
|
|
static async fetchInitialData({
|
|
|
|
client,
|
|
|
|
path,
|
|
|
|
auth,
|
|
|
|
}: InitialFetchRequest): Promise<CreatePrivateMessageData> {
|
|
|
|
const person_id = Number(path.split("/").pop());
|
2023-05-30 00:40:00 +00:00
|
|
|
|
|
|
|
const form: GetPersonDetails = {
|
2021-03-15 18:09:31 +00:00
|
|
|
person_id,
|
2023-05-11 18:32:32 +00:00
|
|
|
sort: "New",
|
2023-01-04 16:56:24 +00:00
|
|
|
saved_only: false,
|
2023-06-16 02:08:14 +00:00
|
|
|
auth,
|
2023-01-04 16:56:24 +00:00
|
|
|
};
|
2023-05-30 00:40:00 +00:00
|
|
|
|
|
|
|
return {
|
2023-06-16 02:08:14 +00:00
|
|
|
recipientDetailsResponse: await client.getPersonDetails(form),
|
2023-05-30 00:40:00 +00:00
|
|
|
};
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2023-06-14 12:20:40 +00:00
|
|
|
async fetchPersonDetails() {
|
|
|
|
this.setState({
|
|
|
|
recipientRes: { state: "loading" },
|
|
|
|
});
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
recipientRes: await HttpService.client.getPersonDetails({
|
|
|
|
person_id: this.state.recipientId,
|
|
|
|
sort: "New",
|
|
|
|
saved_only: false,
|
|
|
|
auth: myAuth(),
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
get documentTitle(): string {
|
2023-06-14 12:20:40 +00:00
|
|
|
if (this.state.recipientRes.state == "success") {
|
|
|
|
const name_ = this.state.recipientRes.data.person_view.person.name;
|
|
|
|
return `${i18n.t("create_private_message")} - ${name_}`;
|
|
|
|
} else {
|
|
|
|
return "";
|
|
|
|
}
|
2020-09-08 18:44:55 +00:00
|
|
|
}
|
|
|
|
|
2023-06-14 12:20:40 +00:00
|
|
|
renderRecipientRes() {
|
|
|
|
switch (this.state.recipientRes.state) {
|
|
|
|
case "loading":
|
|
|
|
return (
|
|
|
|
<h5>
|
|
|
|
<Spinner large />
|
|
|
|
</h5>
|
|
|
|
);
|
|
|
|
case "success": {
|
|
|
|
const res = this.state.recipientRes.data;
|
|
|
|
return (
|
|
|
|
<div className="row">
|
|
|
|
<div className="col-12 col-lg-6 offset-lg-3 mb-4">
|
|
|
|
<h5>{i18n.t("create_private_message")}</h5>
|
|
|
|
<PrivateMessageForm
|
|
|
|
onCreate={this.handlePrivateMessageCreate}
|
|
|
|
recipient={res.person_view.person}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
2023-06-20 18:46:16 +00:00
|
|
|
<div className="create-private-message container-lg">
|
2020-09-11 18:09:21 +00:00
|
|
|
<HtmlTags
|
|
|
|
title={this.documentTitle}
|
|
|
|
path={this.context.router.route.match.url}
|
|
|
|
/>
|
2023-06-14 12:20:40 +00:00
|
|
|
{this.renderRecipientRes()}
|
2020-09-06 16:15:25 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-06-14 12:20:40 +00:00
|
|
|
async handlePrivateMessageCreate(form: CreatePrivateMessageI) {
|
|
|
|
const res = await HttpService.client.createPrivateMessage(form);
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2023-06-14 12:20:40 +00:00
|
|
|
if (res.state == "success") {
|
|
|
|
toast(i18n.t("message_sent"));
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2023-06-14 12:20:40 +00:00
|
|
|
// Navigate to the front
|
|
|
|
this.context.router.history.push("/");
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|