1 import { Component, linkEvent } from 'inferno';
2 import { CommentNode as CommentNodeI, CommentForm as CommentFormI } from '../interfaces';
3 import { capitalizeFirstLetter } from '../utils';
4 import { WebSocketService, UserService } from '../services';
5 import * as autosize from 'autosize';
6 import { i18n } from '../i18next';
7 import { T } from 'inferno-i18next';
9 interface CommentFormProps {
12 onReplyCancel?(): any;
17 interface CommentFormState {
18 commentForm: CommentFormI;
22 export class CommentForm extends Component<CommentFormProps, CommentFormState> {
24 private emptyState: CommentFormState = {
28 post_id: this.props.node ? this.props.node.comment.post_id : this.props.postId,
29 creator_id: UserService.Instance.user ? UserService.Instance.user.id : null,
31 buttonTitle: !this.props.node ? capitalizeFirstLetter(i18n.t('post')) : this.props.edit ? capitalizeFirstLetter(i18n.t('edit')) : capitalizeFirstLetter(i18n.t('reply')),
34 constructor(props: any, context: any) {
35 super(props, context);
38 this.state = this.emptyState;
40 if (this.props.node) {
41 if (this.props.edit) {
42 this.state.commentForm.edit_id = this.props.node.comment.id;
43 this.state.commentForm.parent_id = this.props.node.comment.parent_id;
44 this.state.commentForm.content = this.props.node.comment.content;
45 this.state.commentForm.creator_id = this.props.node.comment.creator_id;
47 // A reply gets a new parent id
48 this.state.commentForm.parent_id = this.props.node.comment.id;
54 autosize(document.querySelectorAll('textarea'));
60 <form onSubmit={linkEvent(this, this.handleCommentSubmit)}>
61 <div class="form-group row">
62 <div class="col-sm-12">
63 <textarea class="form-control" value={this.state.commentForm.content} onInput={linkEvent(this, this.handleCommentContentChange)} required disabled={this.props.disabled} rows={2} maxLength={10000} />
67 <div class="col-sm-12">
68 <button type="submit" class="btn btn-sm btn-secondary mr-2" disabled={this.props.disabled}>{this.state.buttonTitle}</button>
69 {this.props.node && <button type="button" class="btn btn-sm btn-secondary" onClick={linkEvent(this, this.handleReplyCancel)}><T i18nKey="cancel">#</T></button>}
77 handleCommentSubmit(i: CommentForm, event: any) {
78 event.preventDefault();
80 WebSocketService.Instance.editComment(i.state.commentForm);
82 WebSocketService.Instance.createComment(i.state.commentForm);
85 i.state.commentForm.content = undefined;
89 i.props.onReplyCancel();
92 autosize.update(document.querySelector('textarea'));
95 handleCommentContentChange(i: CommentForm, event: any) {
96 i.state.commentForm.content = event.target.value;
100 handleReplyCancel(i: CommentForm) {
101 i.props.onReplyCancel();