1 import { Component } from "inferno";
2 import { Subscription } from "rxjs";
3 import { PrivateMessageForm } from "./private-message-form";
4 import { HtmlTags } from "./html-tags";
5 import { Spinner } from "./icon";
6 import { UserService, WebSocketService } from "../services";
10 GetPersonDetailsResponse,
14 } from "lemmy-js-client";
17 getRecipientIdFromProps,
26 import { i18n } from "../i18next";
27 import { InitialFetchRequest } from "shared/interfaces";
29 interface CreatePrivateMessageState {
31 recipient: PersonViewSafe;
36 export class CreatePrivateMessage extends Component<
38 CreatePrivateMessageState
40 private isoData = setIsoData(this.context);
41 private subscription: Subscription;
42 private emptyState: CreatePrivateMessageState = {
43 site_view: this.isoData.site_res.site_view,
45 recipient_id: getRecipientIdFromProps(this.props),
48 constructor(props: any, context: any) {
49 super(props, context);
50 this.state = this.emptyState;
51 this.handlePrivateMessageCreate = this.handlePrivateMessageCreate.bind(
55 this.parseMessage = this.parseMessage.bind(this);
56 this.subscription = wsSubscribe(this.parseMessage);
58 if (!UserService.Instance.localUserView) {
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) {
65 this.state.recipient = this.isoData.routeData[0].user;
66 this.state.loading = false;
68 this.fetchPersonDetails();
72 fetchPersonDetails() {
73 let form: GetPersonDetails = {
74 person_id: this.state.recipient_id,
77 auth: authField(false),
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 return `${i18n.t("create_private_message")} - ${
95 this.state.site_view.site.name
99 componentWillUnmount() {
101 this.subscription.unsubscribe();
107 <div class="container">
109 title={this.documentTitle}
110 path={this.context.router.route.match.url}
112 {this.state.loading ? (
118 <div class="col-12 col-lg-6 offset-lg-3 mb-4">
119 <h5>{i18n.t("create_private_message")}</h5>
121 onCreate={this.handlePrivateMessageCreate}
122 recipient={this.state.recipient.person}
131 handlePrivateMessageCreate() {
132 toast(i18n.t("message_sent"));
134 // Navigate to the front
135 this.context.router.history.push(`/`);
138 parseMessage(msg: any) {
139 let op = wsUserOp(msg);
142 toast(i18n.t(msg.error), "danger");
143 this.state.loading = false;
144 this.setState(this.state);
146 } else if (op == UserOperation.GetPersonDetails) {
147 let data = wsJsonToRes<GetPersonDetailsResponse>(msg).data;
148 this.state.recipient = data.person_view;
149 this.state.loading = false;
150 this.setState(this.state);