2020-09-06 16:15:25 +00:00
|
|
|
import { Component } from 'inferno';
|
|
|
|
import { Subscription } from 'rxjs';
|
|
|
|
import { PrivateMessageForm } from './private-message-form';
|
2020-09-11 18:09:21 +00:00
|
|
|
import { HtmlTags } from './html-tags';
|
2020-09-08 18:44:55 +00:00
|
|
|
import { UserService, WebSocketService } from '../services';
|
2020-09-06 16:15:25 +00:00
|
|
|
import {
|
|
|
|
Site,
|
2020-09-08 18:44:55 +00:00
|
|
|
WebSocketJsonResponse,
|
|
|
|
UserOperation,
|
|
|
|
UserDetailsResponse,
|
|
|
|
UserView,
|
|
|
|
SortType,
|
|
|
|
GetUserDetailsForm,
|
2020-09-06 16:15:25 +00:00
|
|
|
} from 'lemmy-js-client';
|
2020-09-08 18:44:55 +00:00
|
|
|
import {
|
|
|
|
getRecipientIdFromProps,
|
|
|
|
isBrowser,
|
|
|
|
setAuth,
|
|
|
|
setIsoData,
|
|
|
|
toast,
|
|
|
|
wsJsonToRes,
|
|
|
|
wsSubscribe,
|
|
|
|
} from '../utils';
|
2020-09-06 16:15:25 +00:00
|
|
|
import { i18n } from '../i18next';
|
2020-11-12 21:56:46 +00:00
|
|
|
import { InitialFetchRequest } from 'shared/interfaces';
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2020-09-08 18:44:55 +00:00
|
|
|
interface CreatePrivateMessageProps {}
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
interface CreatePrivateMessageState {
|
|
|
|
site: Site;
|
2020-09-08 18:44:55 +00:00
|
|
|
recipient: UserView;
|
|
|
|
recipient_id: number;
|
|
|
|
loading: boolean;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class CreatePrivateMessage extends Component<
|
2020-09-08 18:44:55 +00:00
|
|
|
CreatePrivateMessageProps,
|
2020-09-06 16:15:25 +00:00
|
|
|
CreatePrivateMessageState
|
|
|
|
> {
|
2020-09-08 18:44:55 +00:00
|
|
|
private isoData = setIsoData(this.context);
|
2020-09-06 16:15:25 +00:00
|
|
|
private subscription: Subscription;
|
|
|
|
private emptyState: CreatePrivateMessageState = {
|
2020-09-08 18:44:55 +00:00
|
|
|
site: this.isoData.site.site,
|
|
|
|
recipient: undefined,
|
|
|
|
recipient_id: getRecipientIdFromProps(this.props),
|
|
|
|
loading: true,
|
2020-09-06 16:15:25 +00:00
|
|
|
};
|
|
|
|
constructor(props: any, context: any) {
|
|
|
|
super(props, context);
|
|
|
|
this.state = this.emptyState;
|
|
|
|
this.handlePrivateMessageCreate = this.handlePrivateMessageCreate.bind(
|
|
|
|
this
|
|
|
|
);
|
|
|
|
|
2020-09-08 18:44:55 +00:00
|
|
|
this.parseMessage = this.parseMessage.bind(this);
|
|
|
|
this.subscription = wsSubscribe(this.parseMessage);
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
if (!UserService.Instance.user) {
|
|
|
|
toast(i18n.t('not_logged_in'), 'danger');
|
|
|
|
this.context.router.history.push(`/login`);
|
|
|
|
}
|
|
|
|
|
2020-09-08 18:44:55 +00:00
|
|
|
// Only fetch the data if coming from another route
|
|
|
|
if (this.isoData.path == this.context.router.route.match.url) {
|
|
|
|
this.state.recipient = this.isoData.routeData[0].user;
|
|
|
|
this.state.loading = false;
|
|
|
|
} else {
|
|
|
|
this.fetchUserDetails();
|
|
|
|
}
|
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2020-09-08 18:44:55 +00:00
|
|
|
fetchUserDetails() {
|
|
|
|
let form: GetUserDetailsForm = {
|
|
|
|
user_id: this.state.recipient_id,
|
|
|
|
sort: SortType.New,
|
|
|
|
saved_only: false,
|
|
|
|
};
|
|
|
|
WebSocketService.Instance.getUserDetails(form);
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2020-11-12 21:56:46 +00:00
|
|
|
static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
|
|
|
|
let user_id = Number(req.path.split('/').pop());
|
2020-09-08 18:44:55 +00:00
|
|
|
let form: GetUserDetailsForm = {
|
|
|
|
user_id,
|
|
|
|
sort: SortType.New,
|
|
|
|
saved_only: false,
|
|
|
|
};
|
2020-11-12 21:56:46 +00:00
|
|
|
setAuth(form, req.auth);
|
|
|
|
return [req.client.getUserDetails(form)];
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get documentTitle(): string {
|
2020-09-08 18:44:55 +00:00
|
|
|
return `${i18n.t('create_private_message')} - ${this.state.site.name}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
if (isBrowser()) {
|
|
|
|
this.subscription.unsubscribe();
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<div class="container">
|
2020-09-11 18:09:21 +00:00
|
|
|
<HtmlTags
|
|
|
|
title={this.documentTitle}
|
|
|
|
path={this.context.router.route.match.url}
|
|
|
|
/>
|
2020-09-08 18:44:55 +00:00
|
|
|
{this.state.loading ? (
|
|
|
|
<h5>
|
|
|
|
<svg class="icon icon-spinner spin">
|
|
|
|
<use xlinkHref="#icon-spinner"></use>
|
|
|
|
</svg>
|
|
|
|
</h5>
|
|
|
|
) : (
|
|
|
|
<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}
|
|
|
|
recipient={this.state.recipient}
|
|
|
|
/>
|
|
|
|
</div>
|
2020-09-06 16:15:25 +00:00
|
|
|
</div>
|
2020-09-08 18:44:55 +00:00
|
|
|
)}
|
2020-09-06 16:15:25 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
handlePrivateMessageCreate() {
|
|
|
|
toast(i18n.t('message_sent'));
|
|
|
|
|
|
|
|
// Navigate to the front
|
2020-09-08 18:44:55 +00:00
|
|
|
this.context.router.history.push(`/`);
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
parseMessage(msg: WebSocketJsonResponse) {
|
|
|
|
let res = wsJsonToRes(msg);
|
|
|
|
if (msg.error) {
|
|
|
|
toast(i18n.t(msg.error), 'danger');
|
2020-09-08 18:44:55 +00:00
|
|
|
this.state.loading = false;
|
|
|
|
this.setState(this.state);
|
2020-09-06 16:15:25 +00:00
|
|
|
return;
|
2020-09-08 18:44:55 +00:00
|
|
|
} else if (res.op == UserOperation.GetUserDetails) {
|
|
|
|
let data = res.data as UserDetailsResponse;
|
|
|
|
this.state.recipient = data.user;
|
|
|
|
this.state.loading = false;
|
2020-09-06 16:15:25 +00:00
|
|
|
this.setState(this.state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|