2021-02-22 02:39:04 +00:00
|
|
|
import { Component } from "inferno";
|
2021-09-18 14:27:49 +00:00
|
|
|
import { T } from "inferno-i18next-dess";
|
2021-02-22 02:39:04 +00:00
|
|
|
import { Link } from "inferno-router";
|
2020-09-06 16:15:25 +00:00
|
|
|
import {
|
2021-07-17 20:42:55 +00:00
|
|
|
CommentResponse,
|
2020-12-24 01:58:27 +00:00
|
|
|
CreateComment,
|
|
|
|
EditComment,
|
2022-09-22 15:14:58 +00:00
|
|
|
Language,
|
2020-09-06 16:15:25 +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";
|
2023-05-11 18:32:32 +00:00
|
|
|
import { CommentNodeI } from "shared/interfaces";
|
2021-07-17 20:42:55 +00:00
|
|
|
import { i18n } from "../../i18next";
|
|
|
|
import { UserService, WebSocketService } from "../../services";
|
2020-12-24 01:58:27 +00:00
|
|
|
import {
|
|
|
|
capitalizeFirstLetter,
|
2023-01-04 16:56:24 +00:00
|
|
|
myAuth,
|
2020-12-24 22:05:57 +00:00
|
|
|
wsClient,
|
2020-12-24 01:58:27 +00:00
|
|
|
wsSubscribe,
|
2021-07-17 20:42:55 +00:00
|
|
|
} from "../../utils";
|
|
|
|
import { Icon } from "../common/icon";
|
|
|
|
import { MarkdownTextArea } from "../common/markdown-textarea";
|
2020-09-06 16:15:25 +00:00
|
|
|
|
|
|
|
interface CommentFormProps {
|
2022-06-21 21:42:29 +00:00
|
|
|
/**
|
|
|
|
* Can either be the parent, or the editable comment. The right side is a postId.
|
|
|
|
*/
|
2023-01-04 16:56:24 +00:00
|
|
|
node: CommentNodeI | number;
|
2020-09-06 16:15:25 +00:00
|
|
|
edit?: boolean;
|
|
|
|
disabled?: boolean;
|
|
|
|
focus?: boolean;
|
2020-12-24 22:05:57 +00:00
|
|
|
onReplyCancel?(): any;
|
2022-09-22 15:14:58 +00:00
|
|
|
allLanguages: Language[];
|
2022-12-19 15:57:29 +00:00
|
|
|
siteLanguages: number[];
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface CommentFormState {
|
|
|
|
buttonTitle: string;
|
|
|
|
finished: boolean;
|
2023-01-04 16:56:24 +00:00
|
|
|
formId?: string;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class CommentForm extends Component<CommentFormProps, CommentFormState> {
|
2023-01-04 16:56:24 +00:00
|
|
|
private subscription?: Subscription;
|
|
|
|
state: CommentFormState = {
|
|
|
|
buttonTitle:
|
|
|
|
typeof this.props.node === "number"
|
|
|
|
? capitalizeFirstLetter(i18n.t("post"))
|
|
|
|
: this.props.edit
|
|
|
|
? capitalizeFirstLetter(i18n.t("save"))
|
|
|
|
: capitalizeFirstLetter(i18n.t("reply")),
|
2020-09-06 16:15:25 +00:00
|
|
|
finished: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props: any, context: any) {
|
|
|
|
super(props, context);
|
|
|
|
|
|
|
|
this.handleCommentSubmit = this.handleCommentSubmit.bind(this);
|
|
|
|
this.handleReplyCancel = this.handleReplyCancel.bind(this);
|
|
|
|
|
2020-09-09 03:13:26 +00:00
|
|
|
this.parseMessage = this.parseMessage.bind(this);
|
|
|
|
this.subscription = wsSubscribe(this.parseMessage);
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
2023-01-04 16:56:24 +00:00
|
|
|
this.subscription?.unsubscribe();
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2023-01-04 16:56:24 +00:00
|
|
|
let initialContent =
|
|
|
|
typeof this.props.node !== "number"
|
|
|
|
? this.props.edit
|
|
|
|
? this.props.node.comment_view.comment.content
|
|
|
|
: undefined
|
|
|
|
: undefined;
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
return (
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="mb-3">
|
2023-01-04 16:56:24 +00:00
|
|
|
{UserService.Instance.myUserInfo ? (
|
2020-09-06 16:15:25 +00:00
|
|
|
<MarkdownTextArea
|
2022-06-21 21:42:29 +00:00
|
|
|
initialContent={initialContent}
|
2022-09-22 15:14:58 +00:00
|
|
|
showLanguage
|
2023-01-04 16:56:24 +00:00
|
|
|
buttonTitle={this.state.buttonTitle}
|
2020-09-06 16:15:25 +00:00
|
|
|
finished={this.state.finished}
|
2023-01-04 16:56:24 +00:00
|
|
|
replyType={typeof this.props.node !== "number"}
|
2020-09-06 16:15:25 +00:00
|
|
|
focus={this.props.focus}
|
|
|
|
disabled={this.props.disabled}
|
|
|
|
onSubmit={this.handleCommentSubmit}
|
|
|
|
onReplyCancel={this.handleReplyCancel}
|
2023-01-04 16:56:24 +00:00
|
|
|
placeholder={i18n.t("comment_here")}
|
2022-09-22 15:14:58 +00:00
|
|
|
allLanguages={this.props.allLanguages}
|
2022-12-19 15:57:29 +00:00
|
|
|
siteLanguages={this.props.siteLanguages}
|
2020-09-06 16:15:25 +00:00
|
|
|
/>
|
|
|
|
) : (
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="alert alert-warning" role="alert">
|
2021-02-11 20:35:27 +00:00
|
|
|
<Icon icon="alert-triangle" classes="icon-inline mr-2" />
|
2020-09-06 16:15:25 +00:00
|
|
|
<T i18nKey="must_login" class="d-inline">
|
|
|
|
#
|
2020-09-09 20:33:40 +00:00
|
|
|
<Link className="alert-link" to="/login">
|
2020-09-06 16:15:25 +00:00
|
|
|
#
|
|
|
|
</Link>
|
|
|
|
</T>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-09-22 15:14:58 +00:00
|
|
|
handleCommentSubmit(msg: {
|
2023-01-04 16:56:24 +00:00
|
|
|
val: string;
|
2022-09-22 15:14:58 +00:00
|
|
|
formId: string;
|
2023-01-04 16:56:24 +00:00
|
|
|
languageId?: number;
|
2022-09-22 15:14:58 +00:00
|
|
|
}) {
|
2020-12-24 01:58:27 +00:00
|
|
|
let content = msg.val;
|
2022-09-22 15:14:58 +00:00
|
|
|
let language_id = msg.languageId;
|
2023-01-04 16:56:24 +00:00
|
|
|
let node = this.props.node;
|
|
|
|
|
|
|
|
this.setState({ formId: msg.formId });
|
2022-06-21 21:42:29 +00:00
|
|
|
|
2023-01-04 16:56:24 +00:00
|
|
|
let auth = myAuth();
|
|
|
|
if (auth) {
|
|
|
|
if (typeof node === "number") {
|
|
|
|
let postId = node;
|
|
|
|
let form: CreateComment = {
|
|
|
|
content,
|
|
|
|
form_id: this.state.formId,
|
|
|
|
post_id: postId,
|
|
|
|
language_id,
|
|
|
|
auth,
|
|
|
|
};
|
|
|
|
WebSocketService.Instance.send(wsClient.createComment(form));
|
|
|
|
} else {
|
2022-06-21 21:42:29 +00:00
|
|
|
if (this.props.edit) {
|
2023-01-04 16:56:24 +00:00
|
|
|
let form: EditComment = {
|
2022-09-22 15:14:58 +00:00
|
|
|
content,
|
2022-06-21 21:42:29 +00:00
|
|
|
form_id: this.state.formId,
|
|
|
|
comment_id: node.comment_view.comment.id,
|
2022-09-22 15:14:58 +00:00
|
|
|
language_id,
|
2023-01-04 16:56:24 +00:00
|
|
|
auth,
|
|
|
|
};
|
2022-06-21 21:42:29 +00:00
|
|
|
WebSocketService.Instance.send(wsClient.editComment(form));
|
|
|
|
} else {
|
2023-01-04 16:56:24 +00:00
|
|
|
let form: CreateComment = {
|
|
|
|
content,
|
2022-06-21 21:42:29 +00:00
|
|
|
form_id: this.state.formId,
|
|
|
|
post_id: node.comment_view.post.id,
|
2023-01-04 16:56:24 +00:00
|
|
|
parent_id: node.comment_view.comment.id,
|
2022-09-22 15:14:58 +00:00
|
|
|
language_id,
|
2023-01-04 16:56:24 +00:00
|
|
|
auth,
|
|
|
|
};
|
2022-06-21 21:42:29 +00:00
|
|
|
WebSocketService.Instance.send(wsClient.createComment(form));
|
|
|
|
}
|
2023-01-04 16:56:24 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleReplyCancel() {
|
2023-01-04 16:56:24 +00:00
|
|
|
this.props.onReplyCancel?.();
|
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
|
|
|
|
|
|
|
// Only do the showing and hiding if logged in
|
2023-01-04 16:56:24 +00:00
|
|
|
if (UserService.Instance.myUserInfo) {
|
2020-09-06 16:15:25 +00:00
|
|
|
if (
|
2020-12-24 01:58:27 +00:00
|
|
|
op == UserOperation.CreateComment ||
|
|
|
|
op == UserOperation.EditComment
|
2020-09-06 16:15:25 +00:00
|
|
|
) {
|
2023-01-04 16:56:24 +00:00
|
|
|
let data = wsJsonToRes<CommentResponse>(msg);
|
2020-09-06 16:15:25 +00:00
|
|
|
|
|
|
|
// This only finishes this form, if the randomly generated form_id matches the one received
|
2023-02-03 02:26:18 +00:00
|
|
|
if (this.state.formId && this.state.formId == data.form_id) {
|
2020-09-06 16:15:25 +00:00
|
|
|
this.setState({ finished: true });
|
|
|
|
|
2020-12-24 01:58:27 +00:00
|
|
|
// Necessary because it broke tribute for some reason
|
2020-09-06 16:15:25 +00:00
|
|
|
this.setState({ finished: false });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|