From c54ecdac89434dc6f77980bcd0d37ee0bf55f124 Mon Sep 17 00:00:00 2001 From: Christopher Speller Date: Tue, 18 Aug 2015 15:38:56 -0400 Subject: Refacting post_info.jsx to be an example for ES6 class syntax --- web/react/components/post.jsx | 3 +- web/react/components/post_info.jsx | 167 ++++++++++++++++++++++++++++--------- 2 files changed, 129 insertions(+), 41 deletions(-) (limited to 'web/react/components') diff --git a/web/react/components/post.jsx b/web/react/components/post.jsx index b798dc7ca..7bc6a8c01 100644 --- a/web/react/components/post.jsx +++ b/web/react/components/post.jsx @@ -3,7 +3,6 @@ var PostHeader = require('./post_header.jsx'); var PostBody = require('./post_body.jsx'); -var PostInfo = require('./post_info.jsx'); var AppDispatcher = require('../dispatcher/app_dispatcher.jsx'); var Constants = require('../utils/constants.jsx'); var UserStore = require('../stores/user_store.jsx'); @@ -13,6 +12,8 @@ var client = require('../utils/client.jsx'); var AsyncClient = require('../utils/async_client.jsx'); var ActionTypes = Constants.ActionTypes; +var PostInfo = require('./post_info.jsx'); + module.exports = React.createClass({ displayName: "Post", handleCommentClick: function(e) { diff --git a/web/react/components/post_info.jsx b/web/react/components/post_info.jsx index f6ab0ed8a..c5b015cb9 100644 --- a/web/react/components/post_info.jsx +++ b/web/react/components/post_info.jsx @@ -6,11 +6,18 @@ var utils = require('../utils/utils.jsx'); var Constants = require('../utils/constants.jsx'); -module.exports = React.createClass({ - getInitialState: function() { - return { }; - }, - render: function() { +export default class PostInfo extends React.Component { + constructor(props) { + super(props); + this.state = {}; + } + shouldShowComment(state, type, isOwner) { + if (state === Constants.POST_FAILED || state === Constants.POST_LOADING) { + return false; + } + return isOwner || (this.props.allowReply === 'true' && type !== 'Comment'); + } + createDropdown() { var post = this.props.post; var isOwner = UserStore.getCurrentId() === post.user_id; var isAdmin = UserStore.getCurrentUser().roles.indexOf('admin') > -1; @@ -20,54 +27,120 @@ module.exports = React.createClass({ type = 'Comment'; } - var comments = ''; - var lastCommentClass = ' comment-icon__container__hide'; - if (this.props.isLastComment) { - lastCommentClass = ' comment-icon__container__show'; + if (!this.shouldShowComment(post.state, type, isOwner)) { + return ''; } - if (this.props.commentCount >= 1 && post.state !== Constants.POST_FAILED && post.state !== Constants.POST_LOADING) { - comments = {this.props.commentCount}; + var dropdownContents = []; + var dataComments = 0; + if (type === 'Post') { + dataComments = this.props.commentCount; } - var showDropdown = isOwner || (this.props.allowReply === 'true' && type !== 'Comment'); - if (post.state === Constants.POST_FAILED || post.state === Constants.POST_LOADING) { - showDropdown = false; + if (isOwner) { + dropdownContents.push( +
  • + + Edit + +
  • + ); } - var dropdownContents = []; - var dropdown; - if (showDropdown) { - var dataComments = 0; - if (type === 'Post') { - dataComments = this.props.commentCount; - } - - if (isOwner) { - dropdownContents.push(
  • Edit
  • ); - } + if (isOwner || isAdmin) { + dropdownContents.push( +
  • + + Delete + +
  • + ); + } - if (isOwner || isAdmin) { - dropdownContents.push(
  • Delete
  • ); - } + if (this.props.allowReply === 'true') { + dropdownContents.push( +
  • + + Reply + +
  • + ); + } - if (this.props.allowReply === 'true') { - dropdownContents.push(
  • Reply
  • ); - } + return ( +
    +
    + + {this.props.commentCount} + ); } + var dropdown = this.createDropdown(); + return ( ); } -}); +} + +PostInfo.defaultProps = { + post: null, + commentCount: 0, + isLastComment: false, + allowReply: false +}; +PostInfo.propTypes = { + post: React.PropTypes.object, + commentCount: React.PropTypes.number, + isLastComment: React.PropTypes.bool, + allowReply: React.PropTypes.string, + handleCommentClick: React.PropTypes.func +}; -- cgit v1.2.3-1-g7c22