2022-06-21 21:42:29 +00:00
|
|
|
import { None, Option, Some } from "@sniptt/monads";
|
2021-02-22 02:39:04 +00:00
|
|
|
import { Component } from "inferno";
|
2020-09-06 16:15:25 +00:00
|
|
|
import {
|
2021-07-17 20:42:55 +00:00
|
|
|
GetPersonDetails,
|
2021-03-15 18:09:31 +00:00
|
|
|
GetPersonDetailsResponse,
|
2022-06-21 21:42:29 +00:00
|
|
|
GetSiteResponse,
|
2020-09-08 18:44:55 +00:00
|
|
|
SortType,
|
2021-07-17 20:42:55 +00:00
|
|
|
UserOperation,
|
2022-06-21 21:42:29 +00:00
|
|
|
wsJsonToRes,
|
|
|
|
wsUserOp,
|
2021-02-22 02:39:04 +00:00
|
|
|
} from "lemmy-js-client";
|
2021-07-17 20:42:55 +00:00
|
|
|
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 {
|
2022-06-21 21:42:29 +00:00
|
|
|
auth,
|
2020-09-08 18:44:55 +00:00
|
|
|
getRecipientIdFromProps,
|
|
|
|
isBrowser,
|
|
|
|
setIsoData,
|
|
|
|
toast,
|
2020-12-24 22:05:57 +00:00
|
|
|
wsClient,
|
2020-09-08 18:44:55 +00:00
|
|
|
wsSubscribe,
|
2021-07-17 20:42:55 +00:00
|
|
|
} from "../../utils";
|
|
|
|
import { HtmlTags } from "../common/html-tags";
|
|
|
|
import { Spinner } from "../common/icon";
|
|
|
|
import { PrivateMessageForm } from "./private-message-form";
|
2020-09-06 16:15:25 +00:00
|
|
|
|
|
|
|
interface CreatePrivateMessageState {
|
2022-06-21 21:42:29 +00:00
|
|
|
siteRes: GetSiteResponse;
|
|
|
|
recipientDetailsRes: Option<GetPersonDetailsResponse>;
|
2020-09-08 18:44:55 +00:00
|
|
|
recipient_id: number;
|
|
|
|
loading: boolean;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class CreatePrivateMessage extends Component<
|
2021-02-22 02:24:09 +00:00
|
|
|
any,
|
2020-09-06 16:15:25 +00:00
|
|
|
CreatePrivateMessageState
|
|
|
|
> {
|
2022-06-21 21:42:29 +00:00
|
|
|
private isoData = setIsoData(this.context, GetPersonDetailsResponse);
|
2020-09-06 16:15:25 +00:00
|
|
|
private subscription: Subscription;
|
|
|
|
private emptyState: CreatePrivateMessageState = {
|
2022-06-21 21:42:29 +00:00
|
|
|
siteRes: this.isoData.site_res,
|
|
|
|
recipientDetailsRes: None,
|
2020-09-08 18:44:55 +00:00
|
|
|
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;
|
2021-07-17 20:21:31 +00:00
|
|
|
this.handlePrivateMessageCreate =
|
|
|
|
this.handlePrivateMessageCreate.bind(this);
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2020-09-08 18:44:55 +00:00
|
|
|
this.parseMessage = this.parseMessage.bind(this);
|
|
|
|
this.subscription = wsSubscribe(this.parseMessage);
|
|
|
|
|
2022-06-21 21:42:29 +00:00
|
|
|
if (UserService.Instance.myUserInfo.isNone() && isBrowser()) {
|
2021-02-22 02:39:04 +00:00
|
|
|
toast(i18n.t("not_logged_in"), "danger");
|
2020-09-06 16:15:25 +00:00
|
|
|
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) {
|
2022-06-21 21:42:29 +00:00
|
|
|
this.state.recipientDetailsRes = Some(
|
|
|
|
this.isoData.routeData[0] as GetPersonDetailsResponse
|
|
|
|
);
|
2020-09-08 18:44:55 +00:00
|
|
|
this.state.loading = false;
|
|
|
|
} else {
|
2021-03-15 18:09:31 +00:00
|
|
|
this.fetchPersonDetails();
|
2020-09-08 18:44:55 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2021-03-15 18:09:31 +00:00
|
|
|
fetchPersonDetails() {
|
2022-06-21 21:42:29 +00:00
|
|
|
let form = new GetPersonDetails({
|
|
|
|
person_id: Some(this.state.recipient_id),
|
|
|
|
sort: Some(SortType.New),
|
|
|
|
saved_only: Some(false),
|
|
|
|
username: None,
|
|
|
|
page: None,
|
|
|
|
limit: None,
|
|
|
|
community_id: None,
|
|
|
|
auth: auth(false).ok(),
|
|
|
|
});
|
2021-03-15 18:09:31 +00:00
|
|
|
WebSocketService.Instance.send(wsClient.getPersonDetails(form));
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2020-11-12 21:56:46 +00:00
|
|
|
static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
|
2022-06-21 21:42:29 +00:00
|
|
|
let person_id = Some(Number(req.path.split("/").pop()));
|
|
|
|
let form = new GetPersonDetails({
|
2021-03-15 18:09:31 +00:00
|
|
|
person_id,
|
2022-06-21 21:42:29 +00:00
|
|
|
sort: Some(SortType.New),
|
|
|
|
saved_only: Some(false),
|
|
|
|
username: None,
|
|
|
|
page: None,
|
|
|
|
limit: None,
|
|
|
|
community_id: None,
|
2020-12-24 01:58:27 +00:00
|
|
|
auth: req.auth,
|
2022-06-21 21:42:29 +00:00
|
|
|
});
|
2021-03-15 18:09:31 +00:00
|
|
|
return [req.client.getPersonDetails(form)];
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get documentTitle(): string {
|
2022-06-21 21:42:29 +00:00
|
|
|
return this.state.recipientDetailsRes.match({
|
|
|
|
some: res =>
|
|
|
|
`${i18n.t("create_private_message")} - ${res.person_view.person.name}`,
|
|
|
|
none: "",
|
|
|
|
});
|
2020-09-08 18:44:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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}
|
2022-06-21 21:42:29 +00:00
|
|
|
description={None}
|
|
|
|
image={None}
|
2020-09-11 18:09:21 +00:00
|
|
|
/>
|
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>
|
|
|
|
) : (
|
2022-06-21 21:42:29 +00:00
|
|
|
this.state.recipientDetailsRes.match({
|
|
|
|
some: res => (
|
|
|
|
<div class="row">
|
|
|
|
<div class="col-12 col-lg-6 offset-lg-3 mb-4">
|
|
|
|
<h5>{i18n.t("create_private_message")}</h5>
|
|
|
|
<PrivateMessageForm
|
|
|
|
privateMessageView={None}
|
|
|
|
onCreate={this.handlePrivateMessageCreate}
|
|
|
|
recipient={res.person_view.person}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
),
|
|
|
|
none: <></>,
|
|
|
|
})
|
2020-09-08 18:44:55 +00:00
|
|
|
)}
|
2020-09-06 16:15:25 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
handlePrivateMessageCreate() {
|
2021-02-22 02:39:04 +00:00
|
|
|
toast(i18n.t("message_sent"));
|
2020-09-06 16:15:25 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
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);
|
2020-09-06 16:15:25 +00:00
|
|
|
if (msg.error) {
|
2021-02-22 02:39:04 +00:00
|
|
|
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;
|
2021-03-15 18:09:31 +00:00
|
|
|
} else if (op == UserOperation.GetPersonDetails) {
|
2022-06-21 21:42:29 +00:00
|
|
|
let data = wsJsonToRes<GetPersonDetailsResponse>(
|
|
|
|
msg,
|
|
|
|
GetPersonDetailsResponse
|
|
|
|
);
|
|
|
|
this.state.recipientDetailsRes = Some(data);
|
2020-09-08 18:44:55 +00:00
|
|
|
this.state.loading = false;
|
2020-09-06 16:15:25 +00:00
|
|
|
this.setState(this.state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|