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 { UserService, WebSocketService } from '../services';
9 GetUserDetailsResponse,
13 } from 'lemmy-js-client';
16 getRecipientIdFromProps,
25 import { i18n } from '../i18next';
26 import { InitialFetchRequest } from 'shared/interfaces';
28 interface CreatePrivateMessageProps {}
30 interface CreatePrivateMessageState {
32 recipient: UserViewSafe;
37 export class CreatePrivateMessage extends Component<
38 CreatePrivateMessageProps,
39 CreatePrivateMessageState
41 private isoData = setIsoData(this.context);
42 private subscription: Subscription;
43 private emptyState: CreatePrivateMessageState = {
44 site_view: this.isoData.site_res.site_view,
46 recipient_id: getRecipientIdFromProps(this.props),
49 constructor(props: any, context: any) {
50 super(props, context);
51 this.state = this.emptyState;
52 this.handlePrivateMessageCreate = this.handlePrivateMessageCreate.bind(
56 this.parseMessage = this.parseMessage.bind(this);
57 this.subscription = wsSubscribe(this.parseMessage);
59 if (!UserService.Instance.user) {
60 toast(i18n.t('not_logged_in'), 'danger');
61 this.context.router.history.push(`/login`);
64 // Only fetch the data if coming from another route
65 if (this.isoData.path == this.context.router.route.match.url) {
66 this.state.recipient = this.isoData.routeData[0].user;
67 this.state.loading = false;
69 this.fetchUserDetails();
74 let form: GetUserDetails = {
75 user_id: this.state.recipient_id,
78 auth: authField(false),
80 WebSocketService.Instance.send(wsClient.getUserDetails(form));
83 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
84 let user_id = Number(req.path.split('/').pop());
85 let form: GetUserDetails = {
91 return [req.client.getUserDetails(form)];
94 get documentTitle(): string {
95 return `${i18n.t('create_private_message')} - ${
96 this.state.site_view.site.name
100 componentWillUnmount() {
102 this.subscription.unsubscribe();
108 <div class="container">
110 title={this.documentTitle}
111 path={this.context.router.route.match.url}
113 {this.state.loading ? (
115 <svg class="icon icon-spinner spin">
116 <use xlinkHref="#icon-spinner"></use>
121 <div class="col-12 col-lg-6 offset-lg-3 mb-4">
122 <h5>{i18n.t('create_private_message')}</h5>
124 onCreate={this.handlePrivateMessageCreate}
125 recipient={this.state.recipient.user}
134 handlePrivateMessageCreate() {
135 toast(i18n.t('message_sent'));
137 // Navigate to the front
138 this.context.router.history.push(`/`);
141 parseMessage(msg: any) {
142 let op = wsUserOp(msg);
144 toast(i18n.t(msg.error), 'danger');
145 this.state.loading = false;
146 this.setState(this.state);
148 } else if (op == UserOperation.GetUserDetails) {
149 let data = wsJsonToRes<GetUserDetailsResponse>(msg).data;
150 this.state.recipient = data.user_view;
151 this.state.loading = false;
152 this.setState(this.state);