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 GetUserDetailsResponse,
14 } from "lemmy-js-client";
17 getRecipientIdFromProps,
26 import { i18n } from "../i18next";
27 import { InitialFetchRequest } from "shared/interfaces";
29 interface CreatePrivateMessageState {
31 recipient: UserViewSafe;
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.user) {
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.fetchUserDetails();
73 let form: GetUserDetails = {
74 user_id: this.state.recipient_id,
77 auth: authField(false),
79 WebSocketService.Instance.send(wsClient.getUserDetails(form));
82 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
83 let user_id = Number(req.path.split("/").pop());
84 let form: GetUserDetails = {
90 return [req.client.getUserDetails(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.user}
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);
141 toast(i18n.t(msg.error), "danger");
142 this.state.loading = false;
143 this.setState(this.state);
145 } else if (op == UserOperation.GetUserDetails) {
146 let data = wsJsonToRes<GetUserDetailsResponse>(msg).data;
147 this.state.recipient = data.user_view;
148 this.state.loading = false;
149 this.setState(this.state);