1 import { Component, linkEvent } from 'inferno';
2 import { Prompt } from 'inferno-router';
3 import { Subscription } from 'rxjs';
4 import { retryWhen, delay, take } from 'rxjs/operators';
6 PrivateMessageForm as PrivateMessageFormI,
7 EditPrivateMessageForm,
8 PrivateMessageFormParams,
10 PrivateMessageResponse,
16 WebSocketJsonResponse,
17 } from '../interfaces';
18 import { WebSocketService } from '../services';
20 capitalizeFirstLetter,
25 import { UserListing } from './user-listing';
26 import { MarkdownTextArea } from './markdown-textarea';
27 import { i18n } from '../i18next';
28 import { T } from 'inferno-i18next';
30 interface PrivateMessageFormProps {
31 privateMessage?: PrivateMessage; // If a pm is given, that means this is an edit
32 params?: PrivateMessageFormParams;
34 onCreate?(message: PrivateMessage): any;
35 onEdit?(message: PrivateMessage): any;
38 interface PrivateMessageFormState {
39 privateMessageForm: PrivateMessageFormI;
43 showDisclaimer: boolean;
46 export class PrivateMessageForm extends Component<
47 PrivateMessageFormProps,
48 PrivateMessageFormState
50 private subscription: Subscription;
51 private emptyState: PrivateMessageFormState = {
59 showDisclaimer: false,
62 constructor(props: any, context: any) {
63 super(props, context);
65 this.state = this.emptyState;
67 this.handleContentChange = this.handleContentChange.bind(this);
69 if (this.props.privateMessage) {
70 this.state.privateMessageForm = {
71 content: this.props.privateMessage.content,
72 recipient_id: this.props.privateMessage.recipient_id,
76 if (this.props.params) {
77 this.state.privateMessageForm.recipient_id = this.props.params.recipient_id;
78 let form: GetUserDetailsForm = {
79 user_id: this.state.privateMessageForm.recipient_id,
80 sort: SortType[SortType.New],
83 WebSocketService.Instance.getUserDetails(form);
86 this.subscription = WebSocketService.Instance.subject
87 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
89 msg => this.parseMessage(msg),
90 err => console.error(err),
91 () => console.log('complete')
99 componentDidUpdate() {
100 if (!this.state.loading && this.state.privateMessageForm.content) {
101 window.onbeforeunload = () => true;
103 window.onbeforeunload = undefined;
107 componentWillUnmount() {
108 this.subscription.unsubscribe();
109 window.onbeforeunload = null;
116 when={!this.state.loading && this.state.privateMessageForm.content}
117 message={i18n.t('block_leaving')}
119 <form onSubmit={linkEvent(this, this.handlePrivateMessageSubmit)}>
120 {!this.props.privateMessage && (
121 <div class="form-group row">
122 <label class="col-sm-2 col-form-label">
123 {capitalizeFirstLetter(i18n.t('to'))}
126 {this.state.recipient && (
127 <div class="col-sm-10 form-control-plaintext">
130 name: this.state.recipient.name,
131 avatar: this.state.recipient.avatar,
132 id: this.state.recipient.id,
133 local: this.state.recipient.local,
134 actor_id: this.state.recipient.actor_id,
141 <div class="form-group row">
142 <label class="col-sm-2 col-form-label">
145 onClick={linkEvent(this, this.handleShowDisclaimer)}
146 class="ml-2 pointer text-danger"
147 data-tippy-content={i18n.t('disclaimer')}
149 <svg class={`icon icon-inline`}>
150 <use xlinkHref="#icon-alert-triangle"></use>
154 <div class="col-sm-10">
156 initialContent={this.state.privateMessageForm.content}
157 onContentChange={this.handleContentChange}
162 {this.state.showDisclaimer && (
163 <div class="form-group row">
164 <div class="offset-sm-2 col-sm-10">
165 <div class="alert alert-danger" role="alert">
166 <T i18nKey="private_message_disclaimer">
172 href="https://element.io/get-started"
181 <div class="form-group row">
182 <div class="offset-sm-2 col-sm-10">
185 class="btn btn-secondary mr-2"
186 disabled={this.state.loading}
188 {this.state.loading ? (
189 <svg class="icon icon-spinner spin">
190 <use xlinkHref="#icon-spinner"></use>
192 ) : this.props.privateMessage ? (
193 capitalizeFirstLetter(i18n.t('save'))
195 capitalizeFirstLetter(i18n.t('send_message'))
198 {this.props.privateMessage && (
201 class="btn btn-secondary"
202 onClick={linkEvent(this, this.handleCancel)}
207 <ul class="d-inline-block float-right list-inline mb-1 text-muted font-weight-bold">
208 <li class="list-inline-item"></li>
217 handlePrivateMessageSubmit(i: PrivateMessageForm, event: any) {
218 event.preventDefault();
219 if (i.props.privateMessage) {
220 let editForm: EditPrivateMessageForm = {
221 edit_id: i.props.privateMessage.id,
222 content: i.state.privateMessageForm.content,
224 WebSocketService.Instance.editPrivateMessage(editForm);
226 WebSocketService.Instance.createPrivateMessage(
227 i.state.privateMessageForm
230 i.state.loading = true;
234 handleRecipientChange(i: PrivateMessageForm, event: any) {
235 i.state.recipient = event.target.value;
239 handleContentChange(val: string) {
240 this.state.privateMessageForm.content = val;
241 this.setState(this.state);
244 handleCancel(i: PrivateMessageForm) {
248 handlePreviewToggle(i: PrivateMessageForm, event: any) {
249 event.preventDefault();
250 i.state.previewMode = !i.state.previewMode;
254 handleShowDisclaimer(i: PrivateMessageForm) {
255 i.state.showDisclaimer = !i.state.showDisclaimer;
259 parseMessage(msg: WebSocketJsonResponse) {
260 let res = wsJsonToRes(msg);
262 toast(i18n.t(msg.error), 'danger');
263 this.state.loading = false;
264 this.setState(this.state);
266 } else if (res.op == UserOperation.EditPrivateMessage) {
267 let data = res.data as PrivateMessageResponse;
268 this.state.loading = false;
269 this.props.onEdit(data.message);
270 } else if (res.op == UserOperation.GetUserDetails) {
271 let data = res.data as UserDetailsResponse;
272 this.state.recipient = data.user;
273 this.state.privateMessageForm.recipient_id = data.user.id;
274 this.setState(this.state);
275 } else if (res.op == UserOperation.CreatePrivateMessage) {
276 let data = res.data as PrivateMessageResponse;
277 this.state.loading = false;
278 this.props.onCreate(data.message);
279 this.setState(this.state);