<commit_before>import React from 'react';
import { Meteor } from 'meteor/meteor';
import Paper from 'material-ui/Paper';
import Divider from 'material-ui/Divider';
import TextField from 'material-ui/TextField';
import FlatButton from 'material-ui/FlatButton';
import { Scrollbars } from 'react-custom-scrollbars';

import * as GiveawaysHelper from '../../../util/giveaways';
import * as UsersHelper from '../../../util/users';
import * as LayoutHelper from '../../../util/layout';

import { insertComment } from '../../../api/giveaway-comments/methods';

const CommentsList = (comments, owner) => (
  <Scrollbars
    autoHide
    autoHeight
    autoHeightMin={1}
    autoHeightMax={500}>
    { comments.map(CommentRow(owner)) }
  </Scrollbars>
);

const CommentRow = (owner) => ({ content, user, createdAt }, index) => (
  <div className="comment-row" key={index}>
    { LayoutHelper.twoColumns(
        UsersHelper.getAvatar(user, 40, { margin: "0 auto", display: "flex" }),
        <div className="comment-body">
          <h5 className="comment-username">{ UsersHelper.getFullNameWithLabelIfEqual(user, owner, "Author") }</h5>
          { GiveawaysHelper.commentBody(content) }
          <p className="timestamp small-text">{ moment(createdAt).fromNow() }</p>
        </div>,
        40
      ) }
  </div>
);

const NoComments = () => (
  <p>
    <em>No comments yet. Add yours?</em>
  </p>
);

export class GiveawayCommentsCard extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      ga: null,
      addCommentValue: ""
    };
  }

  handlePostComment(event) {
    const content = this.state.addCommentValue;
    const userId = Meteor.userId();
    const giveawayId = this.state.ga._id;

    if (!content.length || !userId)
      return false;

    this.setState({ addCommentValue: "" });

    insertComment.call({ giveawayId, content, userId }, (error) => {
      if (error) {
        Bert.alert(error.reason, 'danger');
      } else {
        Bert.alert('Comment added.', 'success');
      }
    });
  }

  componentWillMount() {
    this.setState({ ga: this.props.ga });
  }

  render() {
    const { ga, comments, owner } = this.props;

    return (
      <Paper className="giveaway giveaway-card">
        <div className="flex-row">
          <div className="col col-xs-12">
            <h3>Comments</h3>

            <div className="comments-list">
              { comments.length ? CommentsList(comments, owner) : NoComments() }
            </div>

            <Divider style={{ marginTop: 15 }} />

            <div className="add-comment-form">
              { LayoutHelper.threeColumns(
                  UsersHelper.getAvatar(Meteor.user(), 40, { margin: "6px auto", display: "flex" }),
                  <TextField
                    id="add-comment-field"
                    name="add-comment"
                    value={ this.state.addCommentValue }
                    onChange={ event => this.setState({ addCommentValue: event.target.value }) }
                    multiLine={true}
                    fullWidth={true}
                    underlineShow={false}
                    hintText="Add a comment..."
                    hintStyle={{ fontSize: 14 }}
                    textareaStyle={{ fontSize: 14 }} />,
                  <FlatButton onTouchTap={ this.handlePostComment.bind(this) } label="Post" style={{ margin: "6px auto" }} />,
                  40
                ) }
            </div>

          </div>
        </div>
      </Paper>
    );
  }
}
<commit_msg>Hide add comment form for non-logged in users<commit_after>import React from 'react';
import { Meteor } from 'meteor/meteor';
import Paper from 'material-ui/Paper';
import Divider from 'material-ui/Divider';
import TextField from 'material-ui/TextField';
import FlatButton from 'material-ui/FlatButton';
import { Scrollbars } from 'react-custom-scrollbars';

import * as GiveawaysHelper from '../../../util/giveaways';
import * as UsersHelper from '../../../util/users';
import * as LayoutHelper from '../../../util/layout';

import { insertComment } from '../../../api/giveaway-comments/methods';

const CommentsList = (comments, owner) => (
  <Scrollbars
    autoHide
    autoHeight
    autoHeightMin={1}
    autoHeightMax={500}>
    { comments.map(CommentRow(owner)) }
  </Scrollbars>
);

const CommentRow = (owner) => ({ content, user, createdAt }, index) => (
  <div className="comment-row" key={index}>
    { LayoutHelper.twoColumns(
        UsersHelper.getAvatar(user, 40, { margin: "0 auto", display: "flex" }),
        <div className="comment-body">
          <h5 className="comment-username">{ UsersHelper.getFullNameWithLabelIfEqual(user, owner, "Author") }</h5>
          { GiveawaysHelper.commentBody(content) }
          <p className="timestamp small-text">{ moment(createdAt).fromNow() }</p>
        </div>,
        40
      ) }
  </div>
);

const NoComments = () => {
  const cta = Meteor.user() ? "Add yours?" : "Login to comment!";

  return (
    <p>
      <em>No comments yet. { cta }</em>
    </p>
  );
}

export class GiveawayCommentsCard extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      ga: null,
      addCommentValue: ""
    };
  }

  handlePostComment(event) {
    const content = this.state.addCommentValue;
    const userId = Meteor.userId();
    const giveawayId = this.state.ga._id;

    if (!content.length || !userId)
      return false;

    this.setState({ addCommentValue: "" });

    insertComment.call({ giveawayId, content, userId }, (error) => {
      if (error) {
        Bert.alert(error.reason, 'danger');
      } else {
        Bert.alert('Comment added.', 'success');
      }
    });
  }

  AddComments() {
    return (
      <div>
        <Divider style={{ marginTop: 15 }} />

        <div className="add-comment-form">
          { LayoutHelper.threeColumns(
              UsersHelper.getAvatar(Meteor.user(), 40, { margin: "6px auto", display: "flex" }),
              <TextField
                id="add-comment-field"
                name="add-comment"
                value={ this.state.addCommentValue }
                onChange={ event => this.setState({ addCommentValue: event.target.value }) }
                multiLine={true}
                fullWidth={true}
                underlineShow={false}
                hintText="Add a comment..."
                hintStyle={{ fontSize: 14 }}
                textareaStyle={{ fontSize: 14 }} />,
              <FlatButton onTouchTap={ this.handlePostComment.bind(this) } label="Post" style={{ margin: "6px auto" }} />,
              40
            ) }
        </div>
      </div>
    );
  }

  componentWillMount() {
    this.setState({ ga: this.props.ga });
  }

  render() {
    const { ga, comments, owner } = this.props;

    return (
      <Paper className="giveaway giveaway-card">
        <div className="flex-row">
          <div className="col col-xs-12">
            <h3>Comments</h3>

            <div className="comments-list">
              { comments.length ? CommentsList(comments, owner) : NoComments() }
            </div>

            { Meteor.user() ? this.AddComments() : null }

          </div>
        </div>
      </Paper>
    );
  }
}

{'fraction_non_alphanumeric': 0.15411195577055978, 'fraction_numerical': 0.00746371803731859, 'mean_word_length': 2.214571301643714, 'pattern_counts': {'":': 0, '<': 63, '<?xml version=': 0, '>': 75, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3747139', 'n_tokens_mistral': 2200, 'n_tokens_neox': 2104, 'n_words': 461}