summaryrefslogtreecommitdiff
blob: cb76b54f060e7c2b38a4c38c40e988214dadda8a (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
/**
 * Higher Order Publicize sharing form composition.
 *
 * Uses Gutenberg data API to dispatch publicize form data to
 * editor post data in format to match 'publicize' field schema.
 */

/**
 * External dependencies
 */
import { get } from 'lodash';
import { compose } from '@wordpress/compose';
import { withSelect, withDispatch } from '@wordpress/data';

/**
 * Internal dependencies
 */
import PublicizeFormUnwrapped, { MAXIMUM_MESSAGE_LENGTH } from './form-unwrapped';

const PublicizeForm = compose( [
	withSelect( select => {
		const meta = select( 'core/editor' ).getEditedPostAttribute( 'meta' );
		const postTitle = select( 'core/editor' ).getEditedPostAttribute( 'title' );
		const message = get( meta, [ 'jetpack_publicize_message' ], '' );

		return {
			connections: select( 'core/editor' ).getEditedPostAttribute(
				'jetpack_publicize_connections'
			),
			defaultShareMessage: postTitle.substr( 0, MAXIMUM_MESSAGE_LENGTH ),
			shareMessage: message.substr( 0, MAXIMUM_MESSAGE_LENGTH ),
		};
	} ),
	withDispatch( ( dispatch, { connections } ) => ( {
		/**
		 * Toggle connection enable/disable state based on checkbox.
		 *
		 * Saves enable/disable value to connections property in editor
		 * in field 'jetpack_publicize_connections'.
		 *
		 * @param {number}  id ID of the connection being enabled/disabled
		 */
		toggleConnection( id ) {
			const newConnections = connections.map( connection => ( {
				...connection,
				enabled: connection.id === id ? ! connection.enabled : connection.enabled,
			} ) );

			dispatch( 'core/editor' ).editPost( {
				jetpack_publicize_connections: newConnections,
			} );
		},

		/**
		 * Handler for when sharing message is edited.
		 *
		 * Saves edited message to state and to the editor
		 * in field 'jetpack_publicize_message'.
		 *
		 * @param {object} event Change event data from textarea element.
		 */
		messageChange( event ) {
			dispatch( 'core/editor' ).editPost( {
				meta: {
					jetpack_publicize_message: event.target.value,
				},
			} );
		},
	} ) ),
] )( PublicizeFormUnwrapped );

export default PublicizeForm;