1 import { Component } from 'inferno';
2 import { Helmet } from 'inferno-helmet';
3 import { Subscription } from 'rxjs';
4 import { retryWhen, delay, take } from 'rxjs/operators';
5 import { PrivateMessageForm } from './private-message-form';
6 import { WebSocketService, UserService } from '../services';
12 PrivateMessageFormParams,
13 } from '../interfaces';
14 import { toast, wsJsonToRes } from '../utils';
15 import { i18n } from '../i18next';
17 interface CreatePrivateMessageState {
21 export class CreatePrivateMessage extends Component<
23 CreatePrivateMessageState
25 private subscription: Subscription;
26 private emptyState: CreatePrivateMessageState = {
29 constructor(props: any, context: any) {
30 super(props, context);
31 this.state = this.emptyState;
32 this.handlePrivateMessageCreate = this.handlePrivateMessageCreate.bind(
36 if (!UserService.Instance.user) {
37 toast(i18n.t('not_logged_in'), 'danger');
38 this.context.router.history.push(`/login`);
41 this.subscription = WebSocketService.Instance.subject
42 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
44 msg => this.parseMessage(msg),
45 err => console.error(err),
46 () => console.log('complete')
49 WebSocketService.Instance.getSite();
52 componentWillUnmount() {
53 this.subscription.unsubscribe();
56 get documentTitle(): string {
57 if (this.state.site) {
58 return `${i18n.t('create_private_message')} - ${this.state.site.name}`;
66 <div class="container">
67 <Helmet title={this.documentTitle} />
69 <div class="col-12 col-lg-6 offset-lg-3 mb-4">
70 <h5>{i18n.t('create_private_message')}</h5>
72 onCreate={this.handlePrivateMessageCreate}
81 get params(): PrivateMessageFormParams {
82 let urlParams = new URLSearchParams(this.props.location.search);
83 let params: PrivateMessageFormParams = {
84 recipient_id: Number(urlParams.get('recipient_id')),
90 handlePrivateMessageCreate() {
91 toast(i18n.t('message_sent'));
93 // Navigate to the front
94 this.props.history.push(`/`);
97 parseMessage(msg: WebSocketJsonResponse) {
99 let res = wsJsonToRes(msg);
101 toast(i18n.t(msg.error), 'danger');
103 } else if (res.op == UserOperation.GetSite) {
104 let data = res.data as GetSiteResponse;
105 this.state.site = data.site;
106 this.setState(this.state);