/* 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"; const PropTypes = require("prop-types"); 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 }, context: any) => void; onCommentAdded?: () => void; orderBy: string; commentsMaxLength: number; } interface AddCommentFormState { disabled: boolean; error: boolean; alignment: number; remainingCharacterCount: number; } /** * 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 static contextTypes: any = { locale: PropTypes.string, toggleTranslations: PropTypes.bool }; public bodyTextArea: HTMLTextAreaElement; public userGroupIdSelect: HTMLSelectElement; constructor(props: AddCommentFormProps) { super(props); this.state = { disabled: true, error: false, alignment: 0, remainingCharacterCount: props.commentsMaxLength }; } 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, commentsMaxLength } = 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: commentsMaxLength, required: "required", pattern: `^(.){0,${commentsMaxLength}}$`, placeholder: I18n.t("components.add_comment_form.form.body.placeholder"), onChange: (evt: React.ChangeEvent) => this._checkCommentBody(evt.target.value, commentsMaxLength as number) }; if (autoFocus) { textAreaProps.autoFocus = "autoFocus"; } return (