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

154 lines
4 KiB
TypeScript
Raw Normal View History

2021-02-22 02:39:04 +00:00
import { Component } from "inferno";
import {
GetPersonDetails,
2021-03-15 18:09:31 +00:00
GetPersonDetailsResponse,
GetSiteResponse,
2020-09-08 18:44:55 +00:00
SortType,
UserOperation,
wsJsonToRes,
wsUserOp,
2021-02-22 02:39:04 +00:00
} from "lemmy-js-client";
import { Subscription } from "rxjs";
import { i18n } from "../../i18next";
import { InitialFetchRequest } from "../../interfaces";
import { UserService, WebSocketService } from "../../services";
2020-09-08 18:44:55 +00:00
import {
getRecipientIdFromProps,
isBrowser,
myAuth,
2020-09-08 18:44:55 +00:00
setIsoData,
toast,
wsClient,
2020-09-08 18:44:55 +00:00
wsSubscribe,
} from "../../utils";
import { HtmlTags } from "../common/html-tags";
import { Spinner } from "../common/icon";
import { PrivateMessageForm } from "./private-message-form";
interface CreatePrivateMessageState {
siteRes: GetSiteResponse;
recipientDetailsRes?: GetPersonDetailsResponse;
2020-09-08 18:44:55 +00:00
recipient_id: number;
loading: boolean;
}
export class CreatePrivateMessage extends Component<
any,
CreatePrivateMessageState
> {
private isoData = setIsoData(this.context);
private subscription?: Subscription;
state: CreatePrivateMessageState = {
siteRes: this.isoData.site_res,
2020-09-08 18:44:55 +00:00
recipient_id: getRecipientIdFromProps(this.props),
loading: true,
};
constructor(props: any, context: any) {
super(props, context);
2021-07-17 20:21:31 +00:00
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.myUserInfo && isBrowser()) {
2021-02-22 02:39:04 +00:00
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 = {
...this.state,
recipientDetailsRes: this.isoData
.routeData[0] as GetPersonDetailsResponse,
loading: false,
};
2020-09-08 18:44:55 +00:00
} else {
2021-03-15 18:09:31 +00:00
this.fetchPersonDetails();
2020-09-08 18:44:55 +00:00
}
}
2021-03-15 18:09:31 +00:00
fetchPersonDetails() {
let form: GetPersonDetails = {
person_id: this.state.recipient_id,
sort: SortType.New,
saved_only: false,
auth: myAuth(false),
};
2021-03-15 18:09:31 +00:00
WebSocketService.Instance.send(wsClient.getPersonDetails(form));
}
static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
let person_id = Number(req.path.split("/").pop());
let form: GetPersonDetails = {
2021-03-15 18:09:31 +00:00
person_id,
sort: SortType.New,
saved_only: false,
2020-12-24 01:58:27 +00:00
auth: req.auth,
};
2021-03-15 18:09:31 +00:00
return [req.client.getPersonDetails(form)];
}
get documentTitle(): string {
let name_ = this.state.recipientDetailsRes?.person_view.person.name;
return name_ ? `${i18n.t("create_private_message")} - ${name_}` : "";
2020-09-08 18:44:55 +00:00
}
componentWillUnmount() {
if (isBrowser()) {
this.subscription?.unsubscribe();
}
}
render() {
let res = this.state.recipientDetailsRes;
return (
<div className="container-lg">
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-07-17 20:21:31 +00:00
<Spinner large />
2020-09-08 18:44:55 +00:00
</h5>
) : (
res && (
<div className="row">
<div className="col-12 col-lg-6 offset-lg-3 mb-4">
<h5>{i18n.t("create_private_message")}</h5>
<PrivateMessageForm
onCreate={this.handlePrivateMessageCreate}
recipient={res.person_view.person}
/>
</div>
</div>
)
2020-09-08 18:44:55 +00:00
)}
</div>
);
}
handlePrivateMessageCreate() {
2021-02-22 02:39:04 +00:00
toast(i18n.t("message_sent"));
// Navigate to the front
this.context.router.history.push("/");
}
2020-12-24 01:58:27 +00:00
parseMessage(msg: any) {
let op = wsUserOp(msg);
2021-04-07 15:54:38 +00:00
console.log(msg);
if (msg.error) {
2021-02-22 02:39:04 +00:00
toast(i18n.t(msg.error), "danger");
this.setState({ loading: false });
return;
2021-03-15 18:09:31 +00:00
} else if (op == UserOperation.GetPersonDetails) {
let data = wsJsonToRes<GetPersonDetailsResponse>(msg);
this.setState({ recipientDetailsRes: data, loading: false });
}
}
}