import { Button, Gridicon } from '@automattic/components'; import clsx from 'clsx'; import { localize } from 'i18n-calypso'; import { get, includes, isEqual } from 'lodash'; import PropTypes from 'prop-types'; import { Component } from 'react'; import { connect } from 'react-redux'; import scrollTo from 'calypso/lib/scroll-to'; import { getMinimumComment } from 'calypso/my-sites/comments/comment/utils'; import { requestAdminMenu } from 'calypso/state/admin-menu/actions'; import { bumpStat, composeAnalytics, recordTracksEvent, withAnalytics, } from 'calypso/state/analytics/actions'; import { changeCommentStatus, deleteComment, likeComment, unlikeComment, } from 'calypso/state/comments/actions'; import { getSiteComment } from 'calypso/state/comments/selectors'; import { removeNotice, successNotice } from 'calypso/state/notices/actions'; const commentActions = { unapproved: [ 'like', 'approve', 'edit', 'reply', 'spam', 'trash' ], approved: [ 'like', 'approve', 'edit', 'reply', 'spam', 'trash', 'view' ], spam: [ 'approve', 'delete' ], trash: [ 'approve', 'spam', 'delete' ], }; const noop = () => {}; export class CommentActions extends Component { static propTypes = { siteId: PropTypes.number, postId: PropTypes.number, commentId: PropTypes.number, canModerateComment: PropTypes.bool, commentsListQuery: PropTypes.object, getCommentOffsetTop: PropTypes.func, redirect: PropTypes.func, toggleEditMode: PropTypes.func, toggleReply: PropTypes.func, updateLastUndo: PropTypes.func, }; static defaultProps = { updateLastUndo: noop, }; shouldComponentUpdate = ( nextProps ) => ! isEqual( this.props, nextProps ); delete = () => { if ( typeof window === 'undefined' || window.confirm( this.props.translate( 'Delete this comment permanently?' ) ) ) { this.props.deletePermanently(); this.props.redirect?.(); } }; hasAction = ( action ) => includes( commentActions[ this.props.commentStatus ], action ); setSpam = () => this.setStatus( 'spam' ); setStatus = ( status ) => { const { changeStatus, commentIsLiked, commentStatus, unlike, updateLastUndo } = this.props; const alsoUnlike = commentIsLiked && 'approved' !== status; updateLastUndo( null ); changeStatus( status, { alsoUnlike, previousStatus: commentStatus, } ); if ( alsoUnlike ) { unlike(); } this.showNotice( status ); // Refresh the admin menu on update of status to ensure count shown is not stale. this.props.refreshAdminMenu(); }; setTrash = () => this.setStatus( 'trash' ); showNotice = ( status ) => { const { minimumComment, translate } = this.props; this.props.removeNotice( 'comment-notice' ); const message = get( { approved: translate( 'Comment approved.' ), unapproved: translate( 'Comment unapproved.' ), spam: translate( 'Comment marked as spam.' ), trash: translate( 'Comment moved to trash.' ), }, status ); const noticeOptions = { button: translate( 'Undo' ), id: 'comment-notice', isPersistent: true, onClick: this.undo( status, minimumComment ), }; this.props.successNotice( message, noticeOptions ); }; undo = ( status, previousCommentData ) => () => { const { changeStatus, commentId, like, unlike, updateLastUndo } = this.props; const { isLiked: wasLiked, status: previousStatus } = previousCommentData; const alsoApprove = 'approved' !== status && 'approved' === previousStatus; const alsoUnlike = ! wasLiked && 'approved' !== previousStatus; updateLastUndo( commentId ); changeStatus( previousStatus, { alsoUnlike, isUndo: true, previousStatus: status, } ); if ( wasLiked ) { like( { alsoApprove } ); } else if ( alsoUnlike ) { unlike(); } this.props.removeNotice( 'comment-notice' ); }; toggleApproved = () => this.setStatus( this.props.commentIsApproved ? 'unapproved' : 'approved' ); toggleEditMode = () => { this.props.toggleEditMode(); scrollTo( { x: 0, y: this.props.getCommentOffsetTop() } ); }; toggleLike = () => { const { commentIsLiked, commentStatus, like, unlike } = this.props; if ( commentIsLiked ) { return unlike(); } const alsoApprove = 'unapproved' === commentStatus; like( { alsoApprove } ); if ( alsoApprove ) { this.setStatus( 'approved' ); } }; render() { const { canModerateComment, commentIsApproved, commentIsLiked, commentURL, toggleReply, translate, } = this.props; return (
{ this.hasAction( 'approve' ) && ( ) } { this.hasAction( 'spam' ) && ( ) } { this.hasAction( 'trash' ) && ( ) } { this.hasAction( 'delete' ) && ( ) } { this.hasAction( 'like' ) && ( ) } { this.hasAction( 'edit' ) && ( ) } { this.hasAction( 'reply' ) && ( ) } { this.hasAction( 'view' ) && ( ) }
); } } const mapStateToProps = ( state, { siteId, commentId } ) => { const comment = getSiteComment( state, siteId, commentId ); const commentStatus = get( comment, 'status' ); return { canModerateComment: get( comment, 'can_moderate', false ), commentIsApproved: 'approved' === commentStatus, commentIsLiked: get( comment, 'i_like' ), commentStatus, commentURL: get( comment, 'URL' ), minimumComment: getMinimumComment( comment ), }; }; const mapDispatchToProps = ( dispatch, { siteId, postId, commentId, commentsListQuery } ) => ( { changeStatus: ( status, analytics = { alsoUnlike: false, isUndo: false } ) => dispatch( withAnalytics( composeAnalytics( recordTracksEvent( 'calypso_comment_management_change_status', { also_unlike: analytics.alsoUnlike, is_undo: analytics.isUndo, previous_status: analytics.previousStatus, status, } ), bumpStat( 'calypso_comment_management', 'comment_status_changed_to_' + status ) ), changeCommentStatus( siteId, postId, commentId, status, commentsListQuery ) ) ), deletePermanently: () => dispatch( withAnalytics( composeAnalytics( recordTracksEvent( 'calypso_comment_management_delete' ), bumpStat( 'calypso_comment_management', 'comment_deleted' ) ), deleteComment( siteId, postId, commentId, { showSuccessNotice: true }, commentsListQuery ) ) ), like: ( analytics = { alsoApprove: false } ) => dispatch( withAnalytics( composeAnalytics( recordTracksEvent( 'calypso_comment_management_like', { also_approve: analytics.alsoApprove, } ), bumpStat( 'calypso_comment_management', 'comment_liked' ) ), likeComment( siteId, postId, commentId ) ) ), removeNotice: ( noticeId ) => dispatch( removeNotice( noticeId ) ), successNotice: ( text, options ) => dispatch( successNotice( text, options ) ), unlike: () => dispatch( withAnalytics( composeAnalytics( recordTracksEvent( 'calypso_comment_management_unlike' ), bumpStat( 'calypso_comment_management', 'comment_unliked' ) ), unlikeComment( siteId, postId, commentId ) ) ), refreshAdminMenu: () => dispatch( requestAdminMenu( siteId ) ), } ); export default connect( mapStateToProps, mapDispatchToProps )( localize( CommentActions ) );