1 import { None, Option, Some } from "@sniptt/monads";
2 import { Component } from "inferno";
5 GetPersonDetailsResponse,
11 } from "lemmy-js-client";
12 import { Subscription } from "rxjs";
13 import { i18n } from "../../i18next";
14 import { InitialFetchRequest } from "../../interfaces";
15 import { UserService, WebSocketService } from "../../services";
18 getRecipientIdFromProps,
25 import { HtmlTags } from "../common/html-tags";
26 import { Spinner } from "../common/icon";
27 import { PrivateMessageForm } from "./private-message-form";
29 interface CreatePrivateMessageState {
30 siteRes: GetSiteResponse;
31 recipientDetailsRes: Option<GetPersonDetailsResponse>;
36 export class CreatePrivateMessage extends Component<
38 CreatePrivateMessageState
40 private isoData = setIsoData(this.context, GetPersonDetailsResponse);
41 private subscription: Subscription;
42 private emptyState: CreatePrivateMessageState = {
43 siteRes: this.isoData.site_res,
44 recipientDetailsRes: None,
45 recipient_id: getRecipientIdFromProps(this.props),
49 constructor(props: any, context: any) {
50 super(props, context);
51 this.state = this.emptyState;
52 this.handlePrivateMessageCreate =
53 this.handlePrivateMessageCreate.bind(this);
55 this.parseMessage = this.parseMessage.bind(this);
56 this.subscription = wsSubscribe(this.parseMessage);
58 if (UserService.Instance.myUserInfo.isNone() && isBrowser()) {
59 toast(i18n.t("not_logged_in"), "danger");
60 this.context.router.history.push(`/login`);
63 // Only fetch the data if coming from another route
64 if (this.isoData.path == this.context.router.route.match.url) {
67 recipientDetailsRes: Some(
68 this.isoData.routeData[0] as GetPersonDetailsResponse
73 this.fetchPersonDetails();
77 fetchPersonDetails() {
78 let form = new GetPersonDetails({
79 person_id: Some(this.state.recipient_id),
80 sort: Some(SortType.New),
81 saved_only: Some(false),
86 auth: auth(false).ok(),
88 WebSocketService.Instance.send(wsClient.getPersonDetails(form));
91 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
92 let person_id = Some(Number(req.path.split("/").pop()));
93 let form = new GetPersonDetails({
95 sort: Some(SortType.New),
96 saved_only: Some(false),
103 return [req.client.getPersonDetails(form)];
106 get documentTitle(): string {
107 return this.state.recipientDetailsRes.match({
109 `${i18n.t("create_private_message")} - ${res.person_view.person.name}`,
114 componentWillUnmount() {
116 this.subscription.unsubscribe();
122 <div className="container-lg">
124 title={this.documentTitle}
125 path={this.context.router.route.match.url}
129 {this.state.loading ? (
134 this.state.recipientDetailsRes.match({
136 <div className="row">
137 <div className="col-12 col-lg-6 offset-lg-3 mb-4">
138 <h5>{i18n.t("create_private_message")}</h5>
140 privateMessageView={None}
141 onCreate={this.handlePrivateMessageCreate}
142 recipient={res.person_view.person}
154 handlePrivateMessageCreate() {
155 toast(i18n.t("message_sent"));
157 // Navigate to the front
158 this.context.router.history.push("/");
161 parseMessage(msg: any) {
162 let op = wsUserOp(msg);
165 toast(i18n.t(msg.error), "danger");
166 this.setState({ loading: false });
168 } else if (op == UserOperation.GetPersonDetails) {
169 let data = wsJsonToRes<GetPersonDetailsResponse>(
171 GetPersonDetailsResponse
173 this.setState({ recipientDetailsRes: Some(data), loading: false });