File size: 2,546 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
import clsx from 'clsx';
import { localize, translate } from 'i18n-calypso';
import { omitBy } from 'lodash';
import PropTypes from 'prop-types';
import { createElement, PureComponent } from 'react';
import LikeIcons from './icons';
import './style.scss';

class LikeButton extends PureComponent {
	static propTypes = {
		liked: PropTypes.bool,
		showZeroCount: PropTypes.bool,
		likeCount: PropTypes.number,
		showLabel: PropTypes.bool,
		tagName: PropTypes.oneOfType( [ PropTypes.string, PropTypes.object ] ),
		onLikeToggle: PropTypes.func,
		likedLabel: PropTypes.string,
		iconSize: PropTypes.number,
		animateLike: PropTypes.bool,
		postId: PropTypes.number,
		slug: PropTypes.string,
		icon: PropTypes.object,
		defaultLabel: PropTypes.string,
	};

	static defaultProps = {
		liked: false,
		showZeroCount: false,
		likeCount: 0,
		showLabel: true,
		iconSize: 24,
		animateLike: true,
		postId: null,
		slug: null,
		icon: null,
		defaultLabel: '',
	};

	constructor( props ) {
		super( props );

		this.toggleLiked = this.toggleLiked.bind( this );
	}

	toggleLiked( event ) {
		if ( event ) {
			event.preventDefault();
		}
		if ( this.props.onLikeToggle ) {
			this.props.onLikeToggle( ! this.props.liked );
		}
	}

	render() {
		const {
			likeCount,
			tagName: containerTag = 'li',
			showZeroCount,
			postId,
			slug,
			onMouseEnter,
			onMouseLeave,
			icon,
			defaultLabel,
		} = this.props;
		const showLikeCount = likeCount > 0 || showZeroCount;
		const isLink = containerTag === 'a';
		const containerClasses = {
			'like-button': true,
			'ignore-click': true,
			'is-mini': this.props.isMini,
			'is-animated': this.props.animateLike,
			'has-count': showLikeCount,
			'has-label': this.props.showLabel,
		};

		if ( this.props.liked ) {
			containerClasses[ 'is-liked' ] = true;
		}

		const labelElement = (
			<span className="like-button__label">
				<span className="like-button__label-count">
					{ showLikeCount ? likeCount : defaultLabel }
				</span>
			</span>
		);

		const likeIcons = icon || <LikeIcons size={ this.props.iconSize } />;
		const href = isLink ? `/stats/post/${ postId }/${ slug }` : null;
		return createElement(
			containerTag,
			omitBy(
				{
					href,
					className: clsx( containerClasses ),
					onClick: ! isLink ? this.toggleLiked : null,
					onMouseEnter,
					onMouseLeave,
					'aria-label': this.props.liked ? translate( 'Liked' ) : translate( 'Like' ),
				},
				( prop ) => prop === null
			),
			likeIcons,
			labelElement
		);
	}
}

export default localize( LikeButton );