import { Component } from "inferno"; import { GetPersonDetails, GetPersonDetailsResponse, GetSiteResponse, UserOperation, wsJsonToRes, wsUserOp, } from "lemmy-js-client"; import { Subscription } from "rxjs"; import { i18n } from "../../i18next"; import { InitialFetchRequest } from "../../interfaces"; import { WebSocketService } from "../../services"; import { WithPromiseKeys, getRecipientIdFromProps, isBrowser, myAuth, setIsoData, toast, wsClient, wsSubscribe, } from "../../utils"; import { HtmlTags } from "../common/html-tags"; import { Spinner } from "../common/icon"; import { PrivateMessageForm } from "./private-message-form"; interface CreatePrivateMessageData { recipientDetailsResponse: GetPersonDetailsResponse; } interface CreatePrivateMessageState { siteRes: GetSiteResponse; recipientDetailsRes?: GetPersonDetailsResponse; recipient_id: number; loading: boolean; } export class CreatePrivateMessage extends Component< any, CreatePrivateMessageState > { private isoData = setIsoData(this.context); private subscription?: Subscription; state: CreatePrivateMessageState = { siteRes: this.isoData.site_res, recipient_id: getRecipientIdFromProps(this.props), loading: true, }; constructor(props: any, context: any) { super(props, context); this.handlePrivateMessageCreate = this.handlePrivateMessageCreate.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) { this.state = { ...this.state, recipientDetailsRes: this.isoData.routeData.recipientDetailsResponse, loading: false, }; } else { this.fetchPersonDetails(); } } fetchPersonDetails() { let form: GetPersonDetails = { person_id: this.state.recipient_id, sort: "New", saved_only: false, auth: myAuth(false), }; WebSocketService.Instance.send(wsClient.getPersonDetails(form)); } static fetchInitialData( req: InitialFetchRequest ): WithPromiseKeys { const person_id = Number(req.path.split("/").pop()); const form: GetPersonDetails = { person_id, sort: "New", saved_only: false, auth: req.auth, }; return { recipientDetailsResponse: req.client.getPersonDetails(form), }; } get documentTitle(): string { let name_ = this.state.recipientDetailsRes?.person_view.person.name; return name_ ? `${i18n.t("create_private_message")} - ${name_}` : ""; } componentWillUnmount() { if (isBrowser()) { this.subscription?.unsubscribe(); } } render() { let res = this.state.recipientDetailsRes; return (
{this.state.loading ? (
) : ( res && (
{i18n.t("create_private_message")}
) )}
); } handlePrivateMessageCreate() { toast(i18n.t("message_sent")); // Navigate to the front this.context.router.history.push("/"); } parseMessage(msg: any) { let op = wsUserOp(msg); console.log(msg); if (msg.error) { toast(i18n.t(msg.error), "danger"); this.setState({ loading: false }); return; } else if (op == UserOperation.GetPersonDetails) { let data = wsJsonToRes(msg); this.setState({ recipientDetailsRes: data, loading: false }); } } }