File size: 1,849 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
import { Gridicon } from '@automattic/components';
import { localize } from 'i18n-calypso';
import PropTypes from 'prop-types';
import { createElement, Component } from 'react';

const noop = () => {};

class ConversationFollowButton extends Component {
	static propTypes = {
		isFollowing: PropTypes.bool.isRequired,
		onFollowToggle: PropTypes.func,
		tagName: PropTypes.oneOfType( [ PropTypes.string, PropTypes.func ] ),
		followIcon: PropTypes.object,
		followingIcon: PropTypes.object,
	};

	static defaultProps = {
		isFollowing: false,
		onFollowToggle: noop,
		tagName: 'button',
		followIcon: null,
		followingIcon: null,
	};

	toggleFollow = ( event ) => {
		if ( event ) {
			event.preventDefault();
		}

		this.props.onFollowToggle( ! this.props.isFollowing );
	};

	render() {
		const { isFollowing, translate } = this.props;
		const buttonClasses = [
			'button',
			'has-icon',
			'conversation-follow-button',
			this.props.className,
		];
		const iconSize = 20;
		const label = isFollowing
			? translate( 'Following conversation' )
			: translate( 'Follow conversation' );

		if ( this.props.isFollowing ) {
			buttonClasses.push( 'is-following' );
		}

		const followingIcon = this.props.followingIcon || (
			<Gridicon key="following" icon="reader-following-conversation" size={ iconSize } />
		);
		const followIcon = this.props.followIcon || (
			<Gridicon key="follow" icon="reader-follow-conversation" size={ iconSize } />
		);
		const followLabelElement = (
			<span key="label" className="conversation-follow-button__label">
				{ label }
			</span>
		);

		return createElement(
			this.props.tagName,
			{
				onClick: this.toggleFollow,
				className: buttonClasses.join( ' ' ),
				title: label,
			},
			[ followingIcon, followIcon, followLabelElement ]
		);
	}
}

export default localize( ConversationFollowButton );