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,
|
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,
|
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;
|
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,
|
|
|
|
});
|
2020-09-06 16:15:25 +00:00
|
|
|
return (
|
|
|
|
<div class="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}
|
|
|
|
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"))}
|
2020-09-06 16:15:25 +00:00
|
|
|
/>
|
|
|
|
) : (
|
2022-01-27 18:44:08 +00:00
|
|
|
<div class="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>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleCommentSubmit(msg: { val: string; formId: string }) {
|
2020-12-24 01:58:27 +00:00
|
|
|
let content = msg.val;
|
2022-06-21 21:42:29 +00:00
|
|
|
this.state.formId = Some(msg.formId);
|
|
|
|
|
|
|
|
this.props.node.match({
|
|
|
|
left: node => {
|
|
|
|
if (this.props.edit) {
|
|
|
|
let form = new EditComment({
|
|
|
|
content,
|
|
|
|
form_id: this.state.formId,
|
|
|
|
comment_id: node.comment_view.comment.id,
|
|
|
|
auth: auth().unwrap(),
|
|
|
|
});
|
|
|
|
WebSocketService.Instance.send(wsClient.editComment(form));
|
|
|
|
} else {
|
|
|
|
let form = new CreateComment({
|
|
|
|
content,
|
|
|
|
form_id: this.state.formId,
|
|
|
|
post_id: node.comment_view.post.id,
|
|
|
|
parent_id: Some(node.comment_view.comment.id),
|
|
|
|
auth: auth().unwrap(),
|
|
|
|
});
|
|
|
|
WebSocketService.Instance.send(wsClient.createComment(form));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
right: postId => {
|
|
|
|
let form = new CreateComment({
|
|
|
|
content,
|
|
|
|
form_id: this.state.formId,
|
|
|
|
post_id: postId,
|
|
|
|
parent_id: None,
|
|
|
|
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 });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|