summaryrefslogtreecommitdiff
blob: 04efc7ebb1eb7c491bfca49a0f871095271da170 (plain)
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
/**
 * Publicize sharing form component.
 *
 * Displays text area and connection list to allow user
 * to select connections to share to and write a custom
 * sharing message.
 */

/**
 * External dependencies
 */
import classnames from 'classnames';
import { __, _n, sprintf } from '@wordpress/i18n';
import { Component, Fragment } from '@wordpress/element';
import { uniqueId } from 'lodash';

/**
 * Internal dependencies
 */
import PublicizeConnection from './connection';
import PublicizeSettingsButton from './settings-button';

export const MAXIMUM_MESSAGE_LENGTH = 256;

class PublicizeFormUnwrapped extends Component {
	state = {
		hasEditedShareMessage: false,
	};

	fieldId = uniqueId( 'jetpack-publicize-message-field-' );

	/**
	 * Check to see if form should be disabled.
	 *
	 * Checks full connection list to determine if all are disabled.
	 * If they all are, it returns true to disable whole form.
	 *
	 * @return {boolean} True if whole form should be disabled.
	 */
	isDisabled() {
		return this.props.connections.every( connection => ! connection.toggleable );
	}

	getShareMessage() {
		const { shareMessage, defaultShareMessage } = this.props;
		return ! this.state.hasEditedShareMessage && shareMessage === ''
			? defaultShareMessage
			: shareMessage;
	}

	onMessageChange = event => {
		const { messageChange } = this.props;
		this.setState( { hasEditedShareMessage: true } );
		messageChange( event );
	};

	render() {
		const { connections, toggleConnection, refreshCallback } = this.props;
		const shareMessage = this.getShareMessage();
		const charactersRemaining = MAXIMUM_MESSAGE_LENGTH - shareMessage.length;
		const characterCountClass = classnames( 'jetpack-publicize-character-count', {
			'wpas-twitter-length-limit': charactersRemaining <= 0,
		} );

		return (
			<div id="publicize-form">
				<ul className="jetpack-publicize__connections-list">
					{ connections.map( ( { display_name, enabled, id, service_name, toggleable } ) => (
						<PublicizeConnection
							disabled={ ! toggleable }
							enabled={ enabled }
							key={ id }
							id={ id }
							label={ display_name }
							name={ service_name }
							toggleConnection={ toggleConnection }
						/>
					) ) }
				</ul>
				<PublicizeSettingsButton refreshCallback={ refreshCallback } />
				{ connections.some( connection => connection.enabled ) && (
					<Fragment>
						<label className="jetpack-publicize-message-note" htmlFor={ this.fieldId }>
							{ __( 'Customize your message', 'jetpack' ) }
						</label>
						<div className="jetpack-publicize-message-box">
							<textarea
								id={ this.fieldId }
								value={ shareMessage }
								onChange={ this.onMessageChange }
								disabled={ this.isDisabled() }
								maxLength={ MAXIMUM_MESSAGE_LENGTH }
								placeholder={ __(
									"Write a message for your audience here. If you leave this blank, we'll use the post title as the message.",
									'jetpack'
								) }
								rows={ 4 }
							/>
							<div className={ characterCountClass }>
								{ sprintf(
									_n(
										'%d character remaining',
										'%d characters remaining',
										charactersRemaining,
										'jetpack'
									),
									charactersRemaining
								) }
							</div>
						</div>
					</Fragment>
				) }
			</div>
		);
	}
}

export default PublicizeFormUnwrapped;