2020-01-22 21:35:29 +00:00
|
|
|
import { Component } from 'inferno';
|
2020-07-08 05:02:14 +00:00
|
|
|
import { Subscription } from 'rxjs';
|
|
|
|
import { retryWhen, delay, take } from 'rxjs/operators';
|
2020-01-22 21:35:29 +00:00
|
|
|
import { PrivateMessageForm } from './private-message-form';
|
2020-07-10 00:03:47 +00:00
|
|
|
import { WebSocketService, UserService } from '../services';
|
2020-07-08 05:02:14 +00:00
|
|
|
import {
|
|
|
|
UserOperation,
|
|
|
|
WebSocketJsonResponse,
|
|
|
|
GetSiteResponse,
|
|
|
|
PrivateMessageFormParams,
|
|
|
|
} from '../interfaces';
|
|
|
|
import { toast, wsJsonToRes } from '../utils';
|
2020-01-22 21:35:29 +00:00
|
|
|
import { i18n } from '../i18next';
|
|
|
|
|
|
|
|
export class CreatePrivateMessage extends Component<any, any> {
|
2020-07-08 05:02:14 +00:00
|
|
|
private subscription: Subscription;
|
2020-01-22 21:35:29 +00:00
|
|
|
constructor(props: any, context: any) {
|
|
|
|
super(props, context);
|
|
|
|
this.handlePrivateMessageCreate = this.handlePrivateMessageCreate.bind(
|
|
|
|
this
|
|
|
|
);
|
2020-07-08 05:02:14 +00:00
|
|
|
|
2020-07-10 00:03:47 +00:00
|
|
|
if (!UserService.Instance.user) {
|
|
|
|
toast(i18n.t('not_logged_in'), 'danger');
|
|
|
|
this.context.router.history.push(`/login`);
|
|
|
|
}
|
|
|
|
|
2020-07-08 05:02:14 +00:00
|
|
|
this.subscription = WebSocketService.Instance.subject
|
|
|
|
.pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
|
|
|
|
.subscribe(
|
|
|
|
msg => this.parseMessage(msg),
|
|
|
|
err => console.error(err),
|
|
|
|
() => console.log('complete')
|
|
|
|
);
|
|
|
|
|
|
|
|
WebSocketService.Instance.getSite();
|
2020-01-22 21:35:29 +00:00
|
|
|
}
|
|
|
|
|
2020-07-08 05:02:14 +00:00
|
|
|
componentWillUnmount() {
|
|
|
|
this.subscription.unsubscribe();
|
2020-01-22 21:35:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<div class="container">
|
|
|
|
<div class="row">
|
|
|
|
<div class="col-12 col-lg-6 offset-lg-3 mb-4">
|
|
|
|
<h5>{i18n.t('create_private_message')}</h5>
|
|
|
|
<PrivateMessageForm
|
|
|
|
onCreate={this.handlePrivateMessageCreate}
|
|
|
|
params={this.params}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
get params(): PrivateMessageFormParams {
|
|
|
|
let urlParams = new URLSearchParams(this.props.location.search);
|
|
|
|
let params: PrivateMessageFormParams = {
|
|
|
|
recipient_id: Number(urlParams.get('recipient_id')),
|
|
|
|
};
|
|
|
|
|
|
|
|
return params;
|
|
|
|
}
|
|
|
|
|
|
|
|
handlePrivateMessageCreate() {
|
2020-01-23 03:29:11 +00:00
|
|
|
toast(i18n.t('message_sent'));
|
2020-01-22 21:35:29 +00:00
|
|
|
|
|
|
|
// Navigate to the front
|
|
|
|
this.props.history.push(`/`);
|
|
|
|
}
|
2020-07-08 05:02:14 +00:00
|
|
|
|
|
|
|
parseMessage(msg: WebSocketJsonResponse) {
|
|
|
|
console.log(msg);
|
|
|
|
let res = wsJsonToRes(msg);
|
|
|
|
if (msg.error) {
|
|
|
|
toast(i18n.t(msg.error), 'danger');
|
|
|
|
return;
|
|
|
|
} else if (res.op == UserOperation.GetSite) {
|
|
|
|
let data = res.data as GetSiteResponse;
|
|
|
|
document.title = `${i18n.t('create_private_message')} - ${
|
|
|
|
data.site.name
|
|
|
|
}`;
|
|
|
|
}
|
|
|
|
}
|
2020-01-22 21:35:29 +00:00
|
|
|
}
|