1 import { Component, linkEvent } from 'inferno';
2 import { CommentNode as CommentNodeI, CommentForm as CommentFormI } from '../interfaces';
3 import { WebSocketService, UserService } from '../services';
4 import * as autosize from 'autosize';
6 interface CommentFormProps {
14 interface CommentFormState {
15 commentForm: CommentFormI;
19 export class CommentForm extends Component<CommentFormProps, CommentFormState> {
21 private emptyState: CommentFormState = {
25 post_id: this.props.node ? this.props.node.comment.post_id : this.props.postId,
26 creator_id: UserService.Instance.user ? UserService.Instance.user.id : null,
28 buttonTitle: !this.props.node ? "Post" : this.props.edit ? "Edit" : "Reply",
31 constructor(props: any, context: any) {
32 super(props, context);
34 this.state = this.emptyState;
36 if (this.props.node) {
37 if (this.props.edit) {
38 this.state.commentForm.edit_id = this.props.node.comment.id;
39 this.state.commentForm.parent_id = this.props.node.comment.parent_id;
40 this.state.commentForm.content = this.props.node.comment.content;
41 this.state.commentForm.creator_id = this.props.node.comment.creator_id;
43 // A reply gets a new parent id
44 this.state.commentForm.parent_id = this.props.node.comment.id;
50 autosize(document.querySelectorAll('textarea'));
56 <form onSubmit={linkEvent(this, this.handleCommentSubmit)}>
57 <div class="form-group row">
58 <div class="col-sm-12">
59 <textarea class="form-control" value={this.state.commentForm.content} onInput={linkEvent(this, this.handleCommentContentChange)} required disabled={this.props.disabled} rows={2} maxLength={10000} />
63 <div class="col-sm-12">
64 <button type="submit" class="btn btn-sm btn-secondary mr-2" disabled={this.props.disabled}>{this.state.buttonTitle}</button>
65 {this.props.node && <button type="button" class="btn btn-sm btn-secondary" onClick={linkEvent(this, this.handleReplyCancel)}>Cancel</button>}
73 handleCommentSubmit(i: CommentForm, event: any) {
74 event.preventDefault();
76 WebSocketService.Instance.editComment(i.state.commentForm);
78 WebSocketService.Instance.createComment(i.state.commentForm);
81 i.state.commentForm.content = undefined;
85 i.props.onReplyCancel();
89 handleCommentContentChange(i: CommentForm, event: any) {
90 i.state.commentForm.content = event.target.value;
94 handleReplyCancel(i: CommentForm) {
95 i.props.onReplyCancel();