2022-06-21 21:42:29 +00:00
|
|
|
import { Either, None, Option, Some } from "@sniptt/monads";
|
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 {
|
2022-07-30 13:28:08 +00:00
|
|
|
CommentNode as CommentNodeI,
|
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";
|
|
|
|
import { i18n } from "../../i18next";
|
|
|
|
import { UserService, WebSocketService } from "../../services";
|
2020-12-24 01:58:27 +00:00
|
|
|
import {
|
2022-06-21 21:42:29 +00:00
|
|
|
auth,
|
2020-12-24 01:58:27 +00:00
|
|
|
capitalizeFirstLetter,
|
2022-09-22 15:14:58 +00:00
|
|
|
myFirstDiscussionLanguageId,
|
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.
|
|
|
|
*/
|
|
|
|
node: Either<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;
|
2022-06-21 21:42:29 +00:00
|
|
|
formId: Option<string>;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class CommentForm extends Component<CommentFormProps, CommentFormState> {
|
|
|
|
private subscription: Subscription;
|
|
|
|
private emptyState: CommentFormState = {
|
2022-06-21 21:42:29 +00:00
|
|
|
buttonTitle: this.props.node.isRight()
|
2021-02-22 02:39:04 +00:00
|
|
|
? capitalizeFirstLetter(i18n.t("post"))
|
2020-09-06 16:15:25 +00:00
|
|
|
: this.props.edit
|
2021-02-22 02:39:04 +00:00
|
|
|
? capitalizeFirstLetter(i18n.t("save"))
|
|
|
|
: capitalizeFirstLetter(i18n.t("reply")),
|
2020-09-06 16:15:25 +00:00
|
|
|
finished: false,
|
2022-06-21 21:42:29 +00:00
|
|
|
formId: None,
|
2020-09-06 16:15:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props: any, context: any) {
|
|
|
|
super(props, context);
|
|
|
|
|
|
|
|
this.handleCommentSubmit = this.handleCommentSubmit.bind(this);
|
|
|
|
this.handleReplyCancel = this.handleReplyCancel.bind(this);
|
|
|
|
|
|
|
|
this.state = this.emptyState;
|
|
|
|
|
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() {
|
|
|
|
this.subscription.unsubscribe();
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2022-06-21 21:42:29 +00:00
|
|
|
let initialContent = this.props.node.match({
|
|
|
|
left: node =>
|
|
|
|
this.props.edit ? Some(node.comment_view.comment.content) : None,
|
|
|
|
right: () => None,
|
|
|
|
});
|
2022-09-22 15:14:58 +00:00
|
|
|
|
|
|
|
let selectedLang = this.props.node
|
|
|
|
.left()
|
|
|
|
.map(n => n.comment_view.comment.language_id)
|
2022-12-19 15:57:29 +00:00
|
|
|
.or(
|
|
|
|
myFirstDiscussionLanguageId(
|
|
|
|
this.props.allLanguages,
|
|
|
|
this.props.siteLanguages,
|
|
|
|
UserService.Instance.myUserInfo
|
|
|
|
)
|
|
|
|
);
|
2022-09-22 15:14:58 +00:00
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
return (
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="mb-3">
|
2022-06-21 21:42:29 +00:00
|
|
|
{UserService.Instance.myUserInfo.isSome() ? (
|
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
|
|
|
initialLanguageId={selectedLang}
|
|
|
|
showLanguage
|
2022-06-21 21:42:29 +00:00
|
|
|
buttonTitle={Some(this.state.buttonTitle)}
|
|
|
|
maxLength={None}
|
2020-09-06 16:15:25 +00:00
|
|
|
finished={this.state.finished}
|
2022-06-21 21:42:29 +00:00
|
|
|
replyType={this.props.node.isLeft()}
|
2020-09-06 16:15:25 +00:00
|
|
|
focus={this.props.focus}
|
|
|
|
disabled={this.props.disabled}
|
|
|
|
onSubmit={this.handleCommentSubmit}
|
|
|
|
onReplyCancel={this.handleReplyCancel}
|
2022-06-21 21:42:29 +00:00
|
|
|
placeholder={Some(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: {
|
|
|
|
val: Option<string>;
|
|
|
|
formId: string;
|
|
|
|
languageId: Option<number>;
|
|
|
|
}) {
|
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;
|
2022-09-22 15:03:35 +00:00
|
|
|
this.setState({ formId: Some(msg.formId) });
|
2022-06-21 21:42:29 +00:00
|
|
|
|
|
|
|
this.props.node.match({
|
|
|
|
left: node => {
|
|
|
|
if (this.props.edit) {
|
|
|
|
let form = new EditComment({
|
2022-09-22 15:14:58 +00:00
|
|
|
content,
|
2022-08-17 23:26:50 +00:00
|
|
|
distinguished: None,
|
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,
|
2022-06-21 21:42:29 +00:00
|
|
|
auth: auth().unwrap(),
|
|
|
|
});
|
|
|
|
WebSocketService.Instance.send(wsClient.editComment(form));
|
|
|
|
} else {
|
|
|
|
let form = new CreateComment({
|
2022-09-22 15:14:58 +00:00
|
|
|
content: content.unwrap(),
|
2022-06-21 21:42:29 +00:00
|
|
|
form_id: this.state.formId,
|
|
|
|
post_id: node.comment_view.post.id,
|
|
|
|
parent_id: Some(node.comment_view.comment.id),
|
2022-09-22 15:14:58 +00:00
|
|
|
language_id,
|
2022-06-21 21:42:29 +00:00
|
|
|
auth: auth().unwrap(),
|
|
|
|
});
|
|
|
|
WebSocketService.Instance.send(wsClient.createComment(form));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
right: postId => {
|
|
|
|
let form = new CreateComment({
|
2022-09-22 15:14:58 +00:00
|
|
|
content: content.unwrap(),
|
2022-06-21 21:42:29 +00:00
|
|
|
form_id: this.state.formId,
|
|
|
|
post_id: postId,
|
|
|
|
parent_id: None,
|
2022-09-22 15:14:58 +00:00
|
|
|
language_id,
|
2022-06-21 21:42:29 +00:00
|
|
|
auth: auth().unwrap(),
|
|
|
|
});
|
|
|
|
WebSocketService.Instance.send(wsClient.createComment(form));
|
|
|
|
},
|
|
|
|
});
|
2020-09-06 16:15:25 +00:00
|
|
|
this.setState(this.state);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleReplyCancel() {
|
|
|
|
this.props.onReplyCancel();
|
|
|
|
}
|
|
|
|
|
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
|
2022-06-21 21:42:29 +00:00
|
|
|
if (UserService.Instance.myUserInfo.isSome()) {
|
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
|
|
|
) {
|
2022-06-21 21:42:29 +00:00
|
|
|
let data = wsJsonToRes<CommentResponse>(msg, CommentResponse);
|
2020-09-06 16:15:25 +00:00
|
|
|
|
|
|
|
// This only finishes this form, if the randomly generated form_id matches the one received
|
2022-06-21 21:42:29 +00:00
|
|
|
if (this.state.formId.unwrapOr("") == data.form_id.unwrapOr("")) {
|
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 });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|