/* eslint-disable no-return-assign, react/no-unused-prop-types, max-lines */ import * as classnames from "classnames"; import * as React from "react"; import { graphql } from "react-apollo"; import * as uuid from "uuid"; import Icon from "../application/icon.component"; const { I18n, Translate } = require("react-i18nify"); import { AddCommentFormCommentableFragment, AddCommentFormSessionFragment, addCommentMutation, CommentFragment, GetCommentsQuery } from "../support/schema"; interface AddCommentFormProps { session: AddCommentFormSessionFragment & { user: any; } | null; commentable: AddCommentFormCommentableFragment; rootCommentable: AddCommentFormCommentableFragment; showTitle?: boolean; submitButtonClassName?: string; autoFocus?: boolean; arguable?: boolean; userAllowedToComment?: boolean; addComment?: (data: { body: string, alignment: number, userGroupId?: string }) => void; onCommentAdded?: () => void; orderBy: string; } interface AddCommentFormState { disabled: boolean; error: boolean; alignment: number; remainingCharacterCount: number; } export const MAX_LENGTH = 1000; /** * Renders a form to create new comments. * @class * @augments Component */ export class AddCommentForm extends React.Component { public static defaultProps = { showTitle: true, submitButtonClassName: "button button--sc", arguable: false, autoFocus: false }; public bodyTextArea: HTMLTextAreaElement; public userGroupIdSelect: HTMLSelectElement; constructor(props: AddCommentFormProps) { super(props); this.state = { disabled: true, error: false, alignment: 0, remainingCharacterCount: MAX_LENGTH }; } public render() { return (
{this._renderHeading()} {this._renderAccountMessage()} {this._renderOpinionButtons()} {this._renderForm()}
); } public componentDidMount() { this._attachMentions(); } /** * Trick to reuse input_mentions.js logic */ private _attachMentions() { window.$(document).trigger("attach-mentions-element", this.bodyTextArea); } /** * Render the form heading based on showTitle prop * @private * @returns {Void|DOMElement} - The heading or an empty element */ private _renderHeading() { const { showTitle } = this.props; if (showTitle) { return (

{I18n.t("components.add_comment_form.title")}

); } return null; } /** * Render a message telling the user to sign in or sign up to leave a comment. * @private * @returns {Void|DOMElement} - The message or an empty element. */ private _renderAccountMessage() { const { session } = this.props; if (!session) { return (

); } return null; } /** * Render the add comment form if session is present. * @private * @returns {Void|DOMElement} - The add comment form on an empty element. */ private _renderForm() { const { session, submitButtonClassName, commentable: { id, type } } = this.props; const { disabled, remainingCharacterCount } = this.state; if (session) { return (
{this._renderCommentAs()}
{this._renderTextArea()}
{this._renderTextAreaError()} {I18n.t("components.add_comment_form.remaining_characters", { count: remainingCharacterCount })}
); } return null; } /** * Render the form heading based on showTitle prop * @private * @returns {Void|DOMElement} - The heading or an empty element */ private _renderTextArea() { const { commentable: { id, type }, autoFocus } = this.props; const { error } = this.state; const className = classnames({ "is-invalid-input": error }); const textAreaProps: any = { ref: (textarea: HTMLTextAreaElement) => {this.bodyTextArea = textarea; }, id: `add-comment-${type}-${id}`, className, rows: "4", maxLength: MAX_LENGTH, required: "required", pattern: `^(.){0,${MAX_LENGTH}}$`, placeholder: I18n.t("components.add_comment_form.form.body.placeholder"), onChange: (evt: React.ChangeEvent) => this._checkCommentBody(evt.target.value) }; if (autoFocus) { textAreaProps.autoFocus = "autoFocus"; } return (