File size: 4,312 Bytes
1e92f2d |
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 |
import config from '@automattic/calypso-config';
import { getUrlParts } from '@automattic/calypso-url';
import { isMobile } from '@automattic/viewport';
import { createRef, Component, Fragment } from 'react';
import { connect } from 'react-redux';
import LikeButtonContainer from 'calypso/blocks/like-button';
import PostLikesPopover from 'calypso/blocks/post-likes/popover';
import QueryPostLikes from 'calypso/components/data/query-post-likes';
import { navigate } from 'calypso/lib/navigate';
import { createAccountUrl } from 'calypso/lib/paths';
import isReaderTagEmbedPage from 'calypso/lib/reader/is-reader-tag-embed-page';
import ReaderLikeIcon from 'calypso/reader/components/icons/like-icon';
import { recordAction, recordGaEvent, recordTrackForPost } from 'calypso/reader/stats';
import { isUserLoggedIn } from 'calypso/state/current-user/selectors';
import { getPostLikeCount } from 'calypso/state/posts/selectors/get-post-like-count';
import { isLikedPost } from 'calypso/state/posts/selectors/is-liked-post';
import { markPostSeen } from 'calypso/state/reader/posts/actions';
import { getPostByKey } from 'calypso/state/reader/posts/selectors';
import './style.scss';
class ReaderLikeButton extends Component {
state = {
showLikesPopover: false,
};
hidePopoverTimeout = null;
likeButtonRef = createRef();
componentWillUnmount() {
clearTimeout( this.hidePopoverTimeout );
}
onLikeToggle = ( liked ) => {
if ( this.props.isLoggedIn ) {
return this.recordLikeToggle( liked );
}
// Redirect to create account page
const { pathname } = getUrlParts( window.location.href );
if ( isReaderTagEmbedPage( window.location ) ) {
return window.open(
createAccountUrl( { redirectTo: pathname, ref: 'reader-lp' } ),
'_blank'
);
}
// Do not redirect to create account page when not logged in and the login window component is enabled
if ( ! config.isEnabled( 'reader/login-window' ) ) {
return navigate( createAccountUrl( { redirectTo: pathname, ref: 'reader-lp' } ) );
}
};
recordLikeToggle = ( liked ) => {
const post = this.props.post || this.props.postByKey;
recordAction( liked ? 'liked_post' : 'unliked_post' );
recordGaEvent( liked ? 'Clicked Like Post' : 'Clicked Unlike Post' );
recordTrackForPost(
liked ? 'calypso_reader_article_liked' : 'calypso_reader_article_unliked',
post,
{ context: this.props.fullPost ? 'full-post' : 'card' }
);
if ( liked && ! this.props.fullPost && ! post._seen ) {
this.props.markPostSeen( post, this.props.site );
}
};
showLikesPopover = () => {
if ( isMobile() ) {
return;
}
clearTimeout( this.hidePopoverTimeout );
this.setState( { showLikesPopover: true } );
};
hideLikesPopover = () => {
if ( isMobile() ) {
return;
}
this.hidePopoverTimeout = setTimeout( () => {
this.setState( { showLikesPopover: false } );
}, 200 );
};
render() {
const { siteId, postId, likeCount, iLike, iconSize } = this.props;
const { showLikesPopover } = this.state;
const hasEnoughLikes = ( likeCount > 0 && ! iLike ) || ( likeCount > 1 && iLike );
const likeIcon = ReaderLikeIcon( {
liked: iLike,
iconSize: iconSize,
} );
return (
<Fragment>
<QueryPostLikes siteId={ siteId } postId={ postId } />
<LikeButtonContainer
{ ...this.props }
ref={ this.likeButtonRef }
onMouseEnter={ this.showLikesPopover }
onMouseLeave={ this.hideLikesPopover }
onLikeToggle={ this.onLikeToggle }
likeSource="reader"
icon={ likeIcon }
/>
{ showLikesPopover && siteId && postId && hasEnoughLikes && (
<PostLikesPopover
className="reader-likes-popover ignore-click" // eslint-disable-line wpcalypso/jsx-classname-namespace
onMouseEnter={ this.showLikesPopover }
onMouseLeave={ this.hideLikesPopover }
siteId={ siteId }
postId={ postId }
showDisplayNames
context={ this.likeButtonRef.current }
/>
) }
</Fragment>
);
}
}
export default connect(
( state, { siteId, postId } ) => {
return {
postByKey: getPostByKey( state, {
blogId: siteId,
postId,
} ),
likeCount: getPostLikeCount( state, siteId, postId ),
iLike: isLikedPost( state, siteId, postId ),
isLoggedIn: isUserLoggedIn( state ),
};
},
{ markPostSeen }
)( ReaderLikeButton );
|