File size: 9,523 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import { createHigherOrderComponent } from '@wordpress/compose';
import { localize } from 'i18n-calypso';
import PropTypes from 'prop-types';
import { Component } from 'react';
import { connect, useSelector } from 'react-redux';
import QueryJetpackModules from 'calypso/components/data/query-jetpack-modules';
import QuerySiteSettings from 'calypso/components/data/query-site-settings';
import Notice from 'calypso/components/notice';
import NoticeAction from 'calypso/components/notice/notice-action';
import { useActiveThemeQuery } from 'calypso/data/themes/use-active-theme-query';
import PageViewTracker from 'calypso/lib/analytics/page-view-tracker';
import { protectForm } from 'calypso/lib/protect-form';
import { recordGoogleEvent, recordTracksEvent } from 'calypso/state/analytics/actions';
import { activateModule, deactivateModule } from 'calypso/state/jetpack/modules/actions';
import { successNotice, errorNotice } from 'calypso/state/notices/actions';
import getCurrentRouteParameterized from 'calypso/state/selectors/get-current-route-parameterized';
import isFetchingJetpackModules from 'calypso/state/selectors/is-fetching-jetpack-modules';
import isJetpackModuleActive from 'calypso/state/selectors/is-jetpack-module-active';
import isPrivateSite from 'calypso/state/selectors/is-private-site';
import { saveSiteSettings } from 'calypso/state/site-settings/actions';
import {
	getSiteSettings,
	isSavingSiteSettings,
	isSiteSettingsSaveSuccessful,
} from 'calypso/state/site-settings/selectors';
import { isJetpackSite, isJetpackMinimumVersion } from 'calypso/state/sites/selectors';
import { getSelectedSiteId, getSelectedSiteSlug } from 'calypso/state/ui/selectors';
import ButtonsAppearance from './appearance';
import ButtonsBlockAppearance from './components/buttons-block-appearance';
import ButtonsOptions from './options';
import { useSharingButtonsQuery, useSaveSharingButtonsMutation } from './use-sharing-buttons-query';

class SharingButtons extends Component {
	state = {
		values: {},
		buttonsPendingSave: null,
	};

	static propTypes = {
		buttons: PropTypes.array,
		isSaving: PropTypes.bool,
		isSaveSettingsSuccessful: PropTypes.bool,
		isSaveButtonsSuccessful: PropTypes.bool,
		markSaved: PropTypes.func,
		markChanged: PropTypes.func,
		settings: PropTypes.object,
		siteId: PropTypes.number,
		translate: PropTypes.func,
	};

	saveChanges = ( event ) => {
		const { isJetpack, isLikesModuleActive, siteId, path } = this.props;

		event.preventDefault();

		this.props.saveSiteSettings( this.props.siteId, this.state.values );
		if ( this.state.buttonsPendingSave ) {
			this.props.saveSharingButtons( this.state.buttonsPendingSave );
		}
		this.props.recordTracksEvent( 'calypso_sharing_buttons_save_changes_click', { path } );
		this.props.recordGoogleEvent( 'Sharing', 'Clicked Save Changes Button' );

		if ( ! isJetpack || isLikesModuleActive !== false ) {
			return;
		}

		const updatedSettings = this.getUpdatedSettings();
		if ( updatedSettings.disabled_likes ) {
			return;
		}

		this.props.activateModule( siteId, 'likes', true );
	};

	handleChange = ( option, value ) => {
		const pairs = undefined === value ? option : { [ option ]: value };
		this.props.markChanged();
		this.setState( {
			values: Object.assign( {}, this.state.values, pairs ),
		} );
	};

	handleButtonsChange = ( buttons ) => {
		this.props.markChanged();
		this.setState( { buttonsPendingSave: buttons } );
	};

	componentDidUpdate( prevProps ) {
		// Save request has been performed
		if (
			( prevProps.isSavingSettings || prevProps.isSavingButtons ) &&
			! ( this.props.isSavingSettings || this.props.isSavingButtons )
		) {
			if (
				this.props.isSaveSettingsSuccessful &&
				( this.props.isSaveButtonsSuccessful || ! prevProps.buttonsPendingSave )
			) {
				this.props.successNotice( this.props.translate( 'Settings saved successfully!' ) );
				this.props.markSaved();
				// eslint-disable-next-line react/no-did-update-set-state
				this.setState( {
					values: {},
					buttonsPendingSave: null,
				} );
			} else {
				this.props.errorNotice(
					this.props.translate( 'There was a problem saving your changes. Please, try again.' )
				);
			}
		}
	}

	getUpdatedSettings() {
		const { isJetpack, isLikesModuleActive, settings } = this.props;
		const disabledSettings =
			isJetpack && isLikesModuleActive === false
				? {
						// Like button should be disabled if the Likes Jetpack module is deactivated.
						disabled_likes: true,
				  }
				: {};

		return Object.assign( {}, settings, disabledSettings, this.state.values );
	}

	render() {
		const {
			buttons,
			isBlockTheme,
			isJetpack,
			isSavingSettings,
			isSavingButtons,
			settings,
			siteId,
			isSharingButtonsModuleActive,
			isFetchingModules,
			isPrivate,
			siteSlug,
			supportsSharingBlock,
			translate,
		} = this.props;
		const updatedSettings = this.getUpdatedSettings();
		const updatedButtons = this.state.buttonsPendingSave || buttons;
		const isSaving = isSavingSettings || isSavingButtons;
		const isSharingModuleInactive =
			isJetpack && ! isFetchingModules && ! isSharingButtonsModuleActive;

		if ( isBlockTheme && supportsSharingBlock && isSharingModuleInactive ) {
			return <ButtonsBlockAppearance isJetpack={ isJetpack } siteId={ siteId } />;
		}

		return (
			<form
				onSubmit={ this.saveChanges }
				id="sharing-buttons"
				className="buttons__sharing-settings buttons__sharing-buttons"
			>
				<PageViewTracker
					path="/marketing/sharing-buttons/:site"
					title="Marketing > Sharing Buttons"
				/>
				<QuerySiteSettings siteId={ siteId } />
				{ isJetpack && <QueryJetpackModules siteId={ siteId } /> }

				{ /* Rendering notice in a separate function */ }
				{ isSharingModuleInactive && (
					<Notice
						status="is-warning"
						showDismiss={ false }
						text={
							isPrivate
								? translate( 'Adding sharing buttons requires your site to be marked as Public.' )
								: translate(
										'Adding sharing buttons needs the Sharing Buttons module from Jetpack to be enabled.'
								  )
						}
					>
						<NoticeAction
							href={ isPrivate ? '/settings/general/' + siteSlug + '#site-privacy-settings' : null }
							onClick={ isPrivate ? null : () => this.props.activateModule( siteId, 'sharedaddy' ) }
						>
							{ isPrivate ? translate( 'Change settings' ) : translate( 'Enable' ) }
						</NoticeAction>
					</Notice>
				) }
				{ ! isFetchingModules && ! isSharingModuleInactive && isBlockTheme && (
					<Notice
						status="is-info"
						showDismiss={ false }
						text={ translate(
							'You are using a block-based theme. We recommend you disable the legacy sharing feature below and add a sharing button block to your themess template instead.'
						) }
					>
						{ isJetpack && (
							<NoticeAction onClick={ () => this.props.deactivateModule( siteId, 'sharedaddy' ) }>
								{ translate( 'Disable' ) }
							</NoticeAction>
						) }
					</Notice>
				) }

				<ButtonsOptions
					buttons={ buttons }
					settings={ updatedSettings }
					onChange={ this.handleChange }
					saving={ isSaving }
				/>
				<ButtonsAppearance
					buttons={ updatedButtons }
					values={ updatedSettings }
					onChange={ this.handleChange }
					onButtonsChange={ this.handleButtonsChange }
					initialized={ !! buttons && !! settings }
					saving={ isSaving }
				/>
			</form>
		);
	}
}

const withSharingButtons = createHigherOrderComponent(
	( Wrapped ) => ( props ) => {
		const siteId = useSelector( getSelectedSiteId );
		const { data: buttons } = useSharingButtonsQuery( siteId );
		const {
			saveSharingButtons,
			isLoading: isSavingButtons,
			isSuccess: isSaveButtonsSuccessful,
		} = useSaveSharingButtonsMutation( siteId );
		const { data: activeThemeData } = useActiveThemeQuery( siteId, true );
		const isBlockTheme = activeThemeData?.[ 0 ]?.is_block_theme ?? false;

		return (
			<Wrapped
				{ ...props }
				buttons={ buttons }
				saveSharingButtons={ saveSharingButtons }
				isSavingButtons={ isSavingButtons }
				isSaveButtonsSuccessful={ isSaveButtonsSuccessful }
				isBlockTheme={ isBlockTheme }
			/>
		);
	},
	'WithSharingButtons'
);

const connectComponent = connect(
	( state ) => {
		const siteId = getSelectedSiteId( state );
		const siteSlug = getSelectedSiteSlug( state );
		const settings = getSiteSettings( state, siteId );
		const isJetpack = isJetpackSite( state, siteId );
		const isSharingButtonsModuleActive = isJetpackModuleActive( state, siteId, 'sharedaddy' );
		const isLikesModuleActive = isJetpackModuleActive( state, siteId, 'likes' );
		const isFetchingModules = isFetchingJetpackModules( state, siteId );
		const isSavingSettings = isSavingSiteSettings( state, siteId );
		const isSaveSettingsSuccessful = isSiteSettingsSaveSuccessful( state, siteId );
		const isPrivate = isPrivateSite( state, siteId );
		const path = getCurrentRouteParameterized( state, siteId );
		const supportsSharingBlock = ! isJetpack || isJetpackMinimumVersion( state, siteId, '13.1' );

		return {
			isJetpack,
			isSharingButtonsModuleActive,
			isLikesModuleActive,
			isFetchingModules,
			isSavingSettings,
			isSaveSettingsSuccessful,
			isPrivate,
			settings,
			siteId,
			siteSlug,
			path,
			supportsSharingBlock,
		};
	},
	{
		activateModule,
		deactivateModule,
		errorNotice,
		recordGoogleEvent,
		recordTracksEvent,
		saveSiteSettings,
		successNotice,
	}
);

export default connectComponent( protectForm( localize( withSharingButtons( SharingButtons ) ) ) );