1 import { Component } from "inferno";
4 GetPersonDetailsResponse,
9 } from "lemmy-js-client";
10 import { Subscription } from "rxjs";
11 import { i18n } from "../../i18next";
12 import { InitialFetchRequest } from "../../interfaces";
13 import { UserService, WebSocketService } from "../../services";
15 getRecipientIdFromProps,
23 import { HtmlTags } from "../common/html-tags";
24 import { Spinner } from "../common/icon";
25 import { PrivateMessageForm } from "./private-message-form";
27 interface CreatePrivateMessageState {
28 siteRes: GetSiteResponse;
29 recipientDetailsRes?: GetPersonDetailsResponse;
34 export class CreatePrivateMessage extends Component<
36 CreatePrivateMessageState
38 private isoData = setIsoData(this.context);
39 private subscription?: Subscription;
40 state: CreatePrivateMessageState = {
41 siteRes: this.isoData.site_res,
42 recipient_id: getRecipientIdFromProps(this.props),
46 constructor(props: any, context: any) {
47 super(props, context);
48 this.handlePrivateMessageCreate =
49 this.handlePrivateMessageCreate.bind(this);
51 this.parseMessage = this.parseMessage.bind(this);
52 this.subscription = wsSubscribe(this.parseMessage);
54 if (!UserService.Instance.myUserInfo && isBrowser()) {
55 toast(i18n.t("not_logged_in"), "danger");
56 this.context.router.history.push(`/login`);
59 // Only fetch the data if coming from another route
60 if (this.isoData.path == this.context.router.route.match.url) {
63 recipientDetailsRes: this.isoData
64 .routeData[0] as GetPersonDetailsResponse,
68 this.fetchPersonDetails();
72 fetchPersonDetails() {
73 let form: GetPersonDetails = {
74 person_id: this.state.recipient_id,
79 WebSocketService.Instance.send(wsClient.getPersonDetails(form));
82 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
83 let person_id = Number(req.path.split("/").pop());
84 let form: GetPersonDetails = {
90 return [req.client.getPersonDetails(form)];
93 get documentTitle(): string {
94 let name_ = this.state.recipientDetailsRes?.person_view.person.name;
95 return name_ ? `${i18n.t("create_private_message")} - ${name_}` : "";
98 componentWillUnmount() {
100 this.subscription?.unsubscribe();
105 let res = this.state.recipientDetailsRes;
107 <div className="container-lg">
109 title={this.documentTitle}
110 path={this.context.router.route.match.url}
112 {this.state.loading ? (
118 <div className="row">
119 <div className="col-12 col-lg-6 offset-lg-3 mb-4">
120 <h5>{i18n.t("create_private_message")}</h5>
122 onCreate={this.handlePrivateMessageCreate}
123 recipient={res.person_view.person}
133 handlePrivateMessageCreate() {
134 toast(i18n.t("message_sent"));
136 // Navigate to the front
137 this.context.router.history.push("/");
140 parseMessage(msg: any) {
141 let op = wsUserOp(msg);
144 toast(i18n.t(msg.error), "danger");
145 this.setState({ loading: false });
147 } else if (op == UserOperation.GetPersonDetails) {
148 let data = wsJsonToRes<GetPersonDetailsResponse>(msg);
149 this.setState({ recipientDetailsRes: data, loading: false });