All files / components/comment index.js

69.23% Statements 18/26
50% Branches 14/28
83.33% Functions 5/6
69.23% Lines 18/26
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140              2x                               1x   1x             1x 1x       1x 1x                                       3x   3x 3x 3x   3x 3x 3x 3x     3x                                                                                           2x                             2x                
import React from 'react';
import PropTypes from 'prop-types';
import marked from 'marked';
import { createHash } from '../../utils';
import { ui as uiConfig, versions } from '../../utils/config';
import './styles.css';
 
const generateLink = (url, regex, target) => {
  if (url && regex && target) {
    const path = url.pathname;
    if (path && path !== '/' && regex) {
      const r = new RegExp(regex, 'i');
      const currentVersion = r.exec(path)[1];
      const result = url.pathname.replace(currentVersion, target);
      return `${url.protocol}//${url.hostname}:${url.port}${result}${url.search}${url.hash}`;
    }
  }
 
  return '#';
};
 
class Comment extends React.Component {
  constructor() {
    super();
 
    this.state = {
      showAvatar: false,
      regex: ''
    };
  }
 
  componentWillMount() {
    uiConfig().then(response => {
      this.setState({
        showAvatar: response.avatar
      });
    });
    versions().then(response => {
      this.setState({
        regex: response.regex
      });
    });
  }
 
  render() {
    const {
      username,
      emailId,
      timestamp,
      comment,
      currentUserIsOwner,
      commentId,
      handleEditUserComment,
      handleDeleteUserComment,
      edited,
      lastUpdated,
      version,
      activeVersion
    } = this.props;
 
    const emailHash = createHash(emailId);
    const output = marked(comment);
    const { showAvatar, regex } = this.state;
 
    const classes = showAvatar ? 'blabbr-comment withAvatar' : 'blabbr-comment';
    let url = '';
    Eif (window && window.parent) {
      url = window.parent.location;
    }
 
    return (
      <article className={classes}>
        <header>
          <h2>{`${username}`}</h2>
 
          <span className="blabbr-time">
            at <time dateTime={timestamp}>{timestamp}</time>
          </span>
 
          {version &&
            <span className="blabbr-version">
              about{' '}
              {version === activeVersion
                ? `v${version}`
                : <a href={generateLink(url, regex, version)}>v{version}</a>}
            </span>}
 
          {showAvatar &&
            <img
              className="avatar"
              src={`https://gravatar.com/avatar/${emailHash}?s=40&r=pg&d=retro`}
              alt={`${username}'s Gravatar`}
            />}
 
          <span className="controls">
            {!!currentUserIsOwner &&
              <button id={commentId} onClick={handleEditUserComment}>
                Edit
              </button>}
            {!!currentUserIsOwner &&
              <button
                id={commentId}
                onClick={handleDeleteUserComment}
                className="remove"
              >
                Remove
              </button>}
          </span>
        </header>
        <div dangerouslySetInnerHTML={{ __html: output }} />
        {edited && <p><small>(edited - {lastUpdated})</small></p>}
      </article>
    );
  }
}
 
Comment.propTypes = {
  emailId: PropTypes.string.isRequired,
  username: PropTypes.string.isRequired,
  timestamp: PropTypes.string.isRequired,
  comment: PropTypes.string.isRequired,
  commentId: PropTypes.string.isRequired,
  currentUserIsOwner: PropTypes.bool.isRequired,
  handleEditUserComment: PropTypes.func.isRequired,
  handleDeleteUserComment: PropTypes.func.isRequired,
  edited: PropTypes.bool,
  lastUpdated: PropTypes.string,
  version: PropTypes.string,
  activeVersion: PropTypes.string
};
 
Comment.defaultProps = {
  edited: false,
  lastUpdated: '',
  version: '',
  activeVersion: ''
};
 
export default Comment;