File size: 7,878 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/* eslint-disable wpcalypso/jsx-classname-namespace */
import { Gridicon } from '@automattic/components';
import { Icon, starEmpty } from '@wordpress/icons';
import { localize } from 'i18n-calypso';
import { filter, some } from 'lodash';
import PropTypes from 'prop-types';
import { Component } from 'react';
import { connect } from 'react-redux';
import { gaRecordEvent } from 'calypso/lib/analytics/ga';
import { recordTracksEvent } from 'calypso/lib/analytics/tracks';
import { decodeEntities } from 'calypso/lib/formatting';
import getCurrentRouteParameterized from 'calypso/state/selectors/get-current-route-parameterized';
import { getSelectedSiteId } from 'calypso/state/ui/selectors';
import ButtonsLabelEditor from './label-editor';
import ButtonsPreviewAction from './preview-action';
import ButtonsPreviewButtons from './preview-buttons';
import ButtonsTray from './tray';

class SharingButtonsPreview extends Component {
	static displayName = 'SharingButtonsPreview';

	static propTypes = {
		isPrivateSite: PropTypes.bool,
		style: PropTypes.oneOf( [ 'icon-text', 'icon', 'text', 'official' ] ),
		label: PropTypes.string,
		buttons: PropTypes.array,
		showLike: PropTypes.bool,
		showReblog: PropTypes.bool,
		onLabelChange: PropTypes.func,
		onButtonsChange: PropTypes.func,
	};

	static defaultProps = {
		style: 'icon',
		buttons: [],
		showLike: true,
		showReblog: true,
		onLabelChange: function () {},
		onButtonsChange: function () {},
	};

	state = {
		isEditingLabel: false,
		buttonsTrayVisibility: null,
	};

	toggleEditLabel = () => {
		const { path } = this.props;

		const isEditingLabel = ! this.state.isEditingLabel;
		this.setState( { isEditingLabel: isEditingLabel } );

		if ( isEditingLabel ) {
			this.hideButtonsTray();
			recordTracksEvent( 'calypso_sharing_buttons_edit_text_click', { path } );
			gaRecordEvent( 'Sharing', 'Clicked Edit Text Link' );
		} else {
			recordTracksEvent( 'calypso_sharing_buttons_edit_text_close_click', { path } );
			gaRecordEvent( 'Sharing', 'Clicked Edit Text Done Button' );
		}
	};

	showButtonsTray = ( visibility ) => {
		const { path } = this.props;

		this.setState( {
			isEditingLabel: false,
			buttonsTrayVisibility: visibility,
		} );

		if ( 'hidden' === visibility ) {
			recordTracksEvent( 'calypso_sharing_buttons_more_button_click', { path } );
			gaRecordEvent( 'Sharing', 'Clicked More Button Link', visibility );
		} else {
			recordTracksEvent( 'calypso_sharing_buttons_edit_button_click', { path } );
			gaRecordEvent( 'Sharing', 'Clicked Edit Button Link', visibility );
		}
	};

	hideButtonsTray = () => {
		const { path } = this.props;

		if ( ! this.state.buttonsTrayVisibility ) {
			return;
		}

		// Hide button tray by resetting state to default
		this.setState( { buttonsTrayVisibility: null } );

		recordTracksEvent( 'calypso_sharing_buttons_edit_buttons_close_click', { path } );
		gaRecordEvent( 'Sharing', 'Clicked Edit Buttons Done Button' );
	};

	getButtonsTrayToggleButtonLabel = ( visibility, enabledButtonsExist ) => {
		if ( 'visible' === visibility ) {
			if ( enabledButtonsExist ) {
				return this.props.translate( 'Edit sharing buttons', {
					context: 'Sharing: Buttons edit label',
				} );
			}
			return this.props.translate( 'Add sharing buttons', {
				context: 'Sharing: Buttons edit label',
			} );
		} else if ( enabledButtonsExist ) {
			return this.props.translate( 'Edit “More” buttons', {
				context: 'Sharing: Buttons edit label',
			} );
		}

		return this.props.translate( 'Add “More” button', {
			context: 'Sharing: Buttons edit label',
		} );
	};

	getButtonsTrayToggleButtonElement = ( visibility ) => {
		const enabledButtonsExist = some( this.props.buttons, {
			visibility: visibility,
			enabled: true,
		} );

		return (
			<ButtonsPreviewAction
				active={ null === this.state.buttonsTrayVisibility }
				onClick={ this.showButtonsTray.bind( null, visibility ) }
				icon={ enabledButtonsExist ? 'pencil' : 'plus' }
				position="bottom-left"
			>
				{ this.getButtonsTrayToggleButtonLabel( visibility, enabledButtonsExist ) }
			</ButtonsPreviewAction>
		);
	};

	// 16 is used in the preview to match the buttons on the frontend of the website.
	getReblogButtonElement = () => {
		if ( this.props.showReblog ) {
			return (
				<div className="sharing-buttons-preview-button is-enabled style-icon-text sharing-buttons-preview__reblog">
					{ /* eslint-disable wpcalypso/jsx-gridicon-size */ }
					{ /* 16 is used in the preview to match the buttons on the frontend of the website. */ }
					<Gridicon icon="reblog" size={ 16 } />
					{ /* eslint-disable wpcalypso/jsx-gridicon-size */ }
					{ this.props.translate( 'Reblog' ) }
				</div>
			);
		}
	};

	getLikeButtonElement = () => {
		if ( this.props.showLike ) {
			return (
				<span className="sharing-buttons-preview__like-container">
					<div className="sharing-buttons-preview-button is-enabled style-icon-text sharing-buttons-preview__like">
						{ /* 16 is used in the preview to match the buttons on the frontend of the website. */ }
						<Icon icon={ starEmpty } className="sharing-buttons-preview__like-icon" />
						{ /* eslint-disable wpcalypso/jsx-gridicon-size */ }
						{ this.props.translate( 'Like' ) }
					</div>
					<div className="sharing-buttons-preview__fake-user">
						<img
							src="https://1.gravatar.com/avatar/767fc9c115a1b989744c755db47feb60"
							alt="Matt Mullenweg"
						/>
					</div>
					<div className="sharing-buttons-preview__fake-like">
						{ this.props.translate( '1 like' ) }
					</div>
				</span>
			);
		}
	};

	getPreviewButtonsElement = () => {
		const enabledButtons = filter( this.props.buttons, { enabled: true } );

		if ( enabledButtons.length ) {
			return (
				<ButtonsPreviewButtons
					buttons={ enabledButtons }
					visibility="visible"
					style={ this.props.style }
					showMore={ some( this.props.buttons, { visibility: 'hidden' } ) }
				/>
			);
		}
	};

	render() {
		return (
			<div className="sharing-buttons-preview">
				<ButtonsPreviewAction
					active={ ! this.state.isEditingLabel }
					onClick={ this.toggleEditLabel }
					icon="pencil"
					position="top-left"
				>
					{ this.props.translate( 'Edit label text', {
						context: 'Sharing: Buttons edit label',
					} ) }
				</ButtonsPreviewAction>
				<ButtonsLabelEditor
					active={ this.state.isEditingLabel }
					value={ this.props.label }
					onChange={ this.props.onLabelChange }
					onClose={ this.toggleEditLabel }
					hasEnabledButtons={ some( this.props.buttons, { enabled: true } ) }
				/>

				<h2 className="sharing-buttons-preview__heading">{ this.props.translate( 'Preview' ) }</h2>
				<div className="sharing-buttons-preview__display">
					<span className="sharing-buttons-preview__label">
						{ decodeEntities( this.props.label ) }
					</span>
					<div className="sharing-buttons-preview__buttons">
						{ this.getPreviewButtonsElement() }
					</div>

					<div className="sharing-buttons-preview__reblog-like">
						{ this.getReblogButtonElement() }
						{ this.getLikeButtonElement() }
					</div>
				</div>

				<div className="sharing-buttons-preview__button-tray-actions">
					{ this.getButtonsTrayToggleButtonElement( 'visible' ) }
					{ this.getButtonsTrayToggleButtonElement( 'hidden' ) }
				</div>

				<ButtonsTray
					buttons={ this.props.buttons }
					style={ 'official' === this.props.style ? 'text' : this.props.style }
					visibility={ this.state.buttonsTrayVisibility }
					onButtonsChange={ this.props.onButtonsChange }
					onClose={ this.hideButtonsTray }
					active={ null !== this.state.buttonsTrayVisibility }
					limited={ this.props.isPrivateSite }
				/>
			</div>
		);
	}
}

export default connect( ( state ) => {
	return { path: getCurrentRouteParameterized( state, getSelectedSiteId( state ) ) };
} )( localize( SharingButtonsPreview ) );