lemmy-ui/src/shared/components/create-private-message.tsx

155 lines
3.9 KiB
TypeScript
Raw Normal View History

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';
2021-02-11 20:35:27 +00:00
import { Spinner } from './icon';
2020-09-08 18:44:55 +00:00
import { UserService, WebSocketService } from '../services';
import {
2020-12-24 01:58:27 +00:00
SiteView,
2020-09-08 18:44:55 +00:00
UserOperation,
2020-12-24 01:58:27 +00:00
GetUserDetailsResponse,
UserViewSafe,
2020-09-08 18:44:55 +00:00
SortType,
2020-12-24 01:58:27 +00:00
GetUserDetails,
} from 'lemmy-js-client';
2020-09-08 18:44:55 +00:00
import {
authField,
2020-09-08 18:44:55 +00:00
getRecipientIdFromProps,
isBrowser,
setIsoData,
toast,
wsClient,
2020-09-08 18:44:55 +00:00
wsJsonToRes,
wsSubscribe,
2020-12-24 01:58:27 +00:00
wsUserOp,
2020-09-08 18:44:55 +00:00
} from '../utils';
import { i18n } from '../i18next';
import { InitialFetchRequest } from 'shared/interfaces';
2020-09-08 18:44:55 +00:00
interface CreatePrivateMessageProps {}
interface CreatePrivateMessageState {
2020-12-24 01:58:27 +00:00
site_view: SiteView;
recipient: UserViewSafe;
2020-09-08 18:44:55 +00:00
recipient_id: number;
loading: boolean;
}
export class CreatePrivateMessage extends Component<
2020-09-08 18:44:55 +00:00
CreatePrivateMessageProps,
CreatePrivateMessageState
> {
2020-09-08 18:44:55 +00:00
private isoData = setIsoData(this.context);
private subscription: Subscription;
private emptyState: CreatePrivateMessageState = {
2020-12-24 01:58:27 +00:00
site_view: this.isoData.site_res.site_view,
2020-09-08 18:44:55 +00:00
recipient: undefined,
recipient_id: getRecipientIdFromProps(this.props),
loading: true,
};
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);
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-08 18:44:55 +00:00
fetchUserDetails() {
2020-12-24 01:58:27 +00:00
let form: GetUserDetails = {
2020-09-08 18:44:55 +00:00
user_id: this.state.recipient_id,
sort: SortType.New,
saved_only: false,
auth: authField(false),
2020-09-08 18:44:55 +00:00
};
WebSocketService.Instance.send(wsClient.getUserDetails(form));
}
static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
let user_id = Number(req.path.split('/').pop());
2020-12-24 01:58:27 +00:00
let form: GetUserDetails = {
2020-09-08 18:44:55 +00:00
user_id,
sort: SortType.New,
saved_only: false,
2020-12-24 01:58:27 +00:00
auth: req.auth,
2020-09-08 18:44:55 +00:00
};
return [req.client.getUserDetails(form)];
}
get documentTitle(): string {
2020-12-24 01:58:27 +00:00
return `${i18n.t('create_private_message')} - ${
this.state.site_view.site.name
}`;
2020-09-08 18:44:55 +00:00
}
componentWillUnmount() {
if (isBrowser()) {
this.subscription.unsubscribe();
}
}
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>
2021-02-11 20:35:27 +00:00
<Spinner />
2020-09-08 18:44:55 +00:00
</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}
2020-12-24 01:58:27 +00:00
recipient={this.state.recipient.user}
2020-09-08 18:44:55 +00:00
/>
</div>
</div>
2020-09-08 18:44:55 +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-12-24 01:58:27 +00:00
parseMessage(msg: any) {
let op = wsUserOp(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);
return;
2020-12-24 01:58:27 +00:00
} else if (op == UserOperation.GetUserDetails) {
let data = wsJsonToRes<GetUserDetailsResponse>(msg).data;
this.state.recipient = data.user_view;
2020-09-08 18:44:55 +00:00
this.state.loading = false;
this.setState(this.state);
}
}
}