1 import { Component } from 'inferno';
2 import { Link } from 'inferno-router';
3 import { Subscription } from 'rxjs';
5 CommentNode as CommentNodeI,
6 CommentForm as CommentFormI,
10 } from 'lemmy-js-client';
11 import { capitalizeFirstLetter, wsJsonToRes, wsSubscribe } from '../utils';
12 import { WebSocketService, UserService } from '../services';
13 import { i18n } from '../i18next';
14 import { T } from 'inferno-i18next';
15 import { MarkdownTextArea } from './markdown-textarea';
17 interface CommentFormProps {
20 onReplyCancel?(): any;
26 interface CommentFormState {
27 commentForm: CommentFormI;
32 export class CommentForm extends Component<CommentFormProps, CommentFormState> {
33 private subscription: Subscription;
34 private emptyState: CommentFormState = {
38 post_id: this.props.node
39 ? this.props.node.comment.post_id
41 creator_id: UserService.Instance.user
42 ? UserService.Instance.user.id
45 buttonTitle: !this.props.node
46 ? capitalizeFirstLetter(i18n.t('post'))
48 ? capitalizeFirstLetter(i18n.t('save'))
49 : capitalizeFirstLetter(i18n.t('reply')),
53 constructor(props: any, context: any) {
54 super(props, context);
56 this.handleCommentSubmit = this.handleCommentSubmit.bind(this);
57 this.handleReplyCancel = this.handleReplyCancel.bind(this);
59 this.state = this.emptyState;
61 if (this.props.node) {
62 if (this.props.edit) {
63 this.state.commentForm.edit_id = this.props.node.comment.id;
64 this.state.commentForm.parent_id = this.props.node.comment.parent_id;
65 this.state.commentForm.content = this.props.node.comment.content;
66 this.state.commentForm.creator_id = this.props.node.comment.creator_id;
68 // A reply gets a new parent id
69 this.state.commentForm.parent_id = this.props.node.comment.id;
73 this.parseMessage = this.parseMessage.bind(this);
74 this.subscription = wsSubscribe(this.parseMessage);
77 componentWillUnmount() {
78 this.subscription.unsubscribe();
84 {UserService.Instance.user ? (
86 initialContent={this.state.commentForm.content}
87 buttonTitle={this.state.buttonTitle}
88 finished={this.state.finished}
89 replyType={!!this.props.node}
90 focus={this.props.focus}
91 disabled={this.props.disabled}
92 onSubmit={this.handleCommentSubmit}
93 onReplyCancel={this.handleReplyCancel}
96 <div class="alert alert-light" role="alert">
97 <svg class="icon icon-inline mr-2">
98 <use xlinkHref="#icon-alert-triangle"></use>
100 <T i18nKey="must_login" class="d-inline">
102 <Link className="alert-link" to="/login">
112 handleCommentSubmit(msg: { val: string; formId: string }) {
113 this.state.commentForm.content = msg.val;
114 this.state.commentForm.form_id = msg.formId;
115 if (this.props.edit) {
116 WebSocketService.Instance.editComment(this.state.commentForm);
118 WebSocketService.Instance.createComment(this.state.commentForm);
120 this.setState(this.state);
123 handleReplyCancel() {
124 this.props.onReplyCancel();
127 parseMessage(msg: WebSocketJsonResponse) {
128 let res = wsJsonToRes(msg);
130 // Only do the showing and hiding if logged in
131 if (UserService.Instance.user) {
133 res.op == UserOperation.CreateComment ||
134 res.op == UserOperation.EditComment
136 let data = res.data as CommentResponse;
138 // This only finishes this form, if the randomly generated form_id matches the one received
139 if (this.state.commentForm.form_id == data.form_id) {
140 this.setState({ finished: true });
142 // Necessary because it broke tribute for some reaso
143 this.setState({ finished: false });