1 import { Component } from 'inferno';
2 import { Link } from 'inferno-router';
3 import { Subscription } from 'rxjs';
9 } from 'lemmy-js-client';
10 import { CommentNode as CommentNodeI } from '../interfaces';
12 capitalizeFirstLetter,
17 import { WebSocketService, UserService } from '../services';
18 import { i18n } from '../i18next';
19 import { T } from 'inferno-i18next';
20 import { MarkdownTextArea } from './markdown-textarea';
22 interface CommentFormProps {
25 onReplyCancel?(): any;
31 interface CommentFormState {
37 export class CommentForm extends Component<CommentFormProps, CommentFormState> {
38 private subscription: Subscription;
39 private emptyState: CommentFormState = {
40 buttonTitle: !this.props.node
41 ? capitalizeFirstLetter(i18n.t('post'))
43 ? capitalizeFirstLetter(i18n.t('save'))
44 : capitalizeFirstLetter(i18n.t('reply')),
49 constructor(props: any, context: any) {
50 super(props, context);
52 this.handleCommentSubmit = this.handleCommentSubmit.bind(this);
53 this.handleReplyCancel = this.handleReplyCancel.bind(this);
55 this.state = this.emptyState;
57 this.parseMessage = this.parseMessage.bind(this);
58 this.subscription = wsSubscribe(this.parseMessage);
61 componentWillUnmount() {
62 this.subscription.unsubscribe();
68 {UserService.Instance.user ? (
72 ? this.props.node.comment_view.comment.content
75 buttonTitle={this.state.buttonTitle}
76 finished={this.state.finished}
77 replyType={!!this.props.node}
78 focus={this.props.focus}
79 disabled={this.props.disabled}
80 onSubmit={this.handleCommentSubmit}
81 onReplyCancel={this.handleReplyCancel}
84 <div class="alert alert-light" role="alert">
85 <svg class="icon icon-inline mr-2">
86 <use xlinkHref="#icon-alert-triangle"></use>
88 <T i18nKey="must_login" class="d-inline">
90 <Link className="alert-link" to="/login">
100 handleCommentSubmit(msg: { val: string; formId: string }) {
101 let content = msg.val;
102 this.state.formId = msg.formId;
104 let node = this.props.node;
106 if (this.props.edit) {
107 let form: EditComment = {
109 form_id: this.state.formId,
110 edit_id: node.comment_view.comment.id,
111 auth: UserService.Instance.authField(),
113 WebSocketService.Instance.client.editComment(form);
115 let form: CreateComment = {
117 form_id: this.state.formId,
118 post_id: node ? node.comment_view.post.id : this.props.postId,
119 parent_id: node ? node.comment_view.comment.parent_id : null,
120 auth: UserService.Instance.authField(),
122 WebSocketService.Instance.client.createComment(form);
124 this.setState(this.state);
127 handleReplyCancel() {
128 this.props.onReplyCancel();
131 parseMessage(msg: any) {
132 let op = wsUserOp(msg);
134 // Only do the showing and hiding if logged in
135 if (UserService.Instance.user) {
137 op == UserOperation.CreateComment ||
138 op == UserOperation.EditComment
140 let data = wsJsonToRes<CommentResponse>(msg).data;
142 // This only finishes this form, if the randomly generated form_id matches the one received
143 if (this.state.formId == data.form_id) {
144 this.setState({ finished: true });
146 // Necessary because it broke tribute for some reason
147 this.setState({ finished: false });