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";
|
2023-06-14 12:20:40 +00:00
|
|
|
import { CreateComment, EditComment, Language } from "lemmy-js-client";
|
2021-07-17 20:42:55 +00:00
|
|
|
import { i18n } from "../../i18next";
|
2023-06-13 10:33:27 +00:00
|
|
|
import { CommentNodeI } from "../../interfaces";
|
2023-06-14 12:20:40 +00:00
|
|
|
import { UserService } from "../../services";
|
|
|
|
import { capitalizeFirstLetter, myAuthRequired } from "../../utils";
|
2021-07-17 20:42:55 +00:00
|
|
|
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;
|
2023-06-14 12:20:40 +00:00
|
|
|
finished?: boolean;
|
2020-09-06 16:15:25 +00:00
|
|
|
edit?: boolean;
|
|
|
|
disabled?: boolean;
|
|
|
|
focus?: boolean;
|
2023-06-14 12:20:40 +00:00
|
|
|
onReplyCancel?(): void;
|
2022-09-22 15:14:58 +00:00
|
|
|
allLanguages: Language[];
|
2022-12-19 15:57:29 +00:00
|
|
|
siteLanguages: number[];
|
2023-06-16 17:10:56 +00:00
|
|
|
containerClass?: string;
|
2023-06-14 12:20:40 +00:00
|
|
|
onUpsertComment(form: EditComment | CreateComment): void;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2023-06-14 12:20:40 +00:00
|
|
|
export class CommentForm extends Component<CommentFormProps, any> {
|
2020-09-06 16:15:25 +00:00
|
|
|
constructor(props: any, context: any) {
|
|
|
|
super(props, context);
|
|
|
|
|
|
|
|
this.handleCommentSubmit = this.handleCommentSubmit.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2023-06-05 21:31:12 +00:00
|
|
|
const initialContent =
|
2023-01-04 16:56:24 +00:00
|
|
|
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 (
|
2023-06-20 18:46:16 +00:00
|
|
|
<div
|
|
|
|
className={["comment-form", "mb-3", this.props.containerClass].join(
|
|
|
|
" "
|
|
|
|
)}
|
|
|
|
>
|
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-06-14 12:20:40 +00:00
|
|
|
buttonTitle={this.buttonTitle}
|
|
|
|
finished={this.props.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}
|
2023-06-14 12:20:40 +00:00
|
|
|
onReplyCancel={this.props.onReplyCancel}
|
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">
|
2023-06-20 12:01:29 +00:00
|
|
|
<Icon icon="alert-triangle" classes="icon-inline me-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>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-06-14 12:20:40 +00:00
|
|
|
get buttonTitle(): string {
|
|
|
|
return typeof this.props.node === "number"
|
|
|
|
? capitalizeFirstLetter(i18n.t("post"))
|
|
|
|
: this.props.edit
|
|
|
|
? capitalizeFirstLetter(i18n.t("save"))
|
|
|
|
: capitalizeFirstLetter(i18n.t("reply"));
|
|
|
|
}
|
2022-06-21 21:42:29 +00:00
|
|
|
|
2023-06-14 12:20:40 +00:00
|
|
|
handleCommentSubmit(content: string, form_id: string, language_id?: number) {
|
|
|
|
const { node, onUpsertComment, edit } = this.props;
|
|
|
|
if (typeof node === "number") {
|
|
|
|
const post_id = node;
|
|
|
|
onUpsertComment({
|
|
|
|
content,
|
|
|
|
post_id,
|
|
|
|
language_id,
|
|
|
|
form_id,
|
|
|
|
auth: myAuthRequired(),
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
if (edit) {
|
|
|
|
const comment_id = node.comment_view.comment.id;
|
|
|
|
onUpsertComment({
|
2023-01-04 16:56:24 +00:00
|
|
|
content,
|
2023-06-14 12:20:40 +00:00
|
|
|
comment_id,
|
|
|
|
form_id,
|
2023-01-04 16:56:24 +00:00
|
|
|
language_id,
|
2023-06-14 12:20:40 +00:00
|
|
|
auth: myAuthRequired(),
|
|
|
|
});
|
2023-01-04 16:56:24 +00:00
|
|
|
} else {
|
2023-06-14 12:20:40 +00:00
|
|
|
const post_id = node.comment_view.post.id;
|
|
|
|
const parent_id = node.comment_view.comment.id;
|
|
|
|
this.props.onUpsertComment({
|
|
|
|
content,
|
|
|
|
parent_id,
|
|
|
|
post_id,
|
|
|
|
form_id,
|
|
|
|
language_id,
|
|
|
|
auth: myAuthRequired(),
|
|
|
|
});
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|