-
Notifications
You must be signed in to change notification settings - Fork 274
/
Footer.js
330 lines (296 loc) · 8.84 KB
/
Footer.js
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/**
* Footer component for SettingsActiveModule.
*
* Site Kit by Google, Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* External dependencies
*/
import PropTypes from 'prop-types';
import { useHistory, useParams } from 'react-router-dom';
import { isEmpty } from 'lodash';
/**
* WordPress dependencies
*/
import { __, sprintf } from '@wordpress/i18n';
import { Fragment, useCallback } from '@wordpress/element';
/**
* Internal dependencies
*/
import { Button, SpinnerButton } from 'googlesitekit-components';
import Data from 'googlesitekit-data';
import { FORM_SETUP } from '../../../modules/analytics/datastore/constants';
import { CORE_MODULES } from '../../../googlesitekit/modules/datastore/constants';
import { CORE_FORMS } from '../../../googlesitekit/datastore/forms/constants';
import { MODULES_ANALYTICS_4 } from '../../../modules/analytics-4/datastore/constants';
import { Cell, Grid, Row } from '../../../material-components';
import PencilIcon from '../../../../svg/icons/pencil.svg';
import TrashIcon from '../../../../svg/icons/trash.svg';
import Link from '../../Link';
import { trackEvent } from '../../../util';
import { clearCache } from '../../../googlesitekit/api/cache';
import { CORE_UI } from '../../../googlesitekit/datastore/ui/constants';
import { CORE_USER } from '../../../googlesitekit/datastore/user/constants';
import useViewContext from '../../../hooks/useViewContext';
const { useDispatch, useSelect } = Data;
export default function Footer( props ) {
const { slug } = props;
const viewContext = useViewContext();
const history = useHistory();
const { action, moduleSlug } = useParams();
const isEditing = action === 'edit' && moduleSlug === slug;
const errorKey = `module-${ slug }-error`;
const dialogActiveKey = `module-${ slug }-dialogActive`;
const isSavingKey = `module-${ slug }-isSaving`;
const canSubmitChanges = useSelect( ( select ) =>
select( CORE_MODULES ).canSubmitChanges( slug )
);
const module = useSelect( ( select ) =>
select( CORE_MODULES ).getModule( slug )
);
const moduleConnected = useSelect( ( select ) =>
select( CORE_MODULES ).isModuleConnected( slug )
);
const dialogActive = useSelect( ( select ) =>
select( CORE_UI ).getValue( dialogActiveKey )
);
const isSaving = useSelect( ( select ) =>
select( CORE_UI ).getValue( isSavingKey )
);
const enableGA4PropertyTooltip = useSelect( ( select ) =>
select( CORE_FORMS ).getValue( FORM_SETUP, 'enableGA4PropertyTooltip' )
);
const { setValues } = useDispatch( CORE_FORMS );
const dismissGA4PropertyTooltip = useCallback( () => {
if ( slug !== 'analytics' || ! enableGA4PropertyTooltip ) {
return null;
}
setValues( FORM_SETUP, {
enableGA4PropertyTooltip: false,
} );
}, [ slug, enableGA4PropertyTooltip, setValues ] );
const moduleHomepage = useSelect( ( select ) => {
if ( ! module || isEmpty( module.homepage ) ) {
return undefined;
}
return select( CORE_USER ).getAccountChooserURL( module.homepage );
} );
const { submitChanges } = useDispatch( CORE_MODULES );
const { clearErrors } = useDispatch( module?.storeName ) || {};
const { setValue } = useDispatch( CORE_UI );
const hasSettings = !! module?.SettingsEditComponent;
const handleClose = useCallback( async () => {
await trackEvent(
`${ viewContext }_module-list`,
'cancel_module_settings',
slug
);
await clearErrors?.();
history.push( `/connected-services/${ slug }` );
if ( slug === 'analytics' ) {
dismissGA4PropertyTooltip();
}
}, [ clearErrors, history, viewContext, slug, dismissGA4PropertyTooltip ] );
const handleConfirm = useCallback(
async ( event ) => {
event.preventDefault();
setValue( isSavingKey, true );
const { error: submissionError } = await submitChanges( slug );
setValue( isSavingKey, false );
if ( submissionError ) {
setValue( errorKey, submissionError );
} else {
await trackEvent(
`${ viewContext }_module-list`,
'update_module_settings',
slug
);
await clearErrors?.();
history.push( `/connected-services/${ slug }` );
if ( slug === 'analytics' ) {
dismissGA4PropertyTooltip();
}
await clearCache();
}
},
[
setValue,
isSavingKey,
submitChanges,
slug,
errorKey,
clearErrors,
history,
viewContext,
dismissGA4PropertyTooltip,
]
);
const handleDialog = useCallback( () => {
setValue( dialogActiveKey, ! dialogActive );
}, [ dialogActive, dialogActiveKey, setValue ] );
const handleEdit = useCallback( () => {
trackEvent(
`${ viewContext }_module-list`,
'edit_module_settings',
slug
);
}, [ slug, viewContext ] );
// Check if the resolution for the specified selector has finished.
// This allows us to determine if the data needed by the module is still being loaded.
// The primary reason for this loading check is to disable the submit button
// while the necessary data for the settings is still being loaded, preventing
// premature interactions by the user.
const isLoading = useSelect( ( select ) => {
const resolutionMapping = {
analytics: 'getAccountSummaries',
tagmanager: 'getAccounts',
'search-console': 'getMatchedProperties',
};
const resolutionSelector = resolutionMapping[ slug ];
if ( ! module || ! resolutionSelector ) {
return false;
}
// Since the GA4 accounts are loaded from `account-summaries` of the `analytics-4` store
// for the `getAccountSummaries` selector, we need to use the `analytics-4` store name
// instead of the module store name.
const storeName =
slug === 'analytics' ? MODULES_ANALYTICS_4 : module.storeName;
return ! select( storeName ).hasFinishedResolution(
resolutionSelector
);
} );
let buttonText = __( 'Save', 'google-site-kit' );
if ( canSubmitChanges ) {
buttonText = __( 'Apply changes', 'google-site-kit' );
}
if ( isSaving ) {
buttonText = __( 'Saving…', 'google-site-kit' );
}
if ( ! module ) {
return null;
}
const { name, forceActive } = module;
let primaryColumn = null;
let secondaryColumn = null;
if ( isEditing || isSaving ) {
primaryColumn = (
<Fragment>
{ hasSettings && moduleConnected ? (
<SpinnerButton
disabled={ isSaving || isLoading }
onClick={ handleConfirm }
isSaving={ isSaving }
>
{ buttonText }
</SpinnerButton>
) : (
<Button onClick={ handleClose }>
{ __( 'Close', 'google-site-kit' ) }
</Button>
) }
{ hasSettings && (
<Button
tertiary
className="googlesitekit-settings-module__footer-cancel"
onClick={ handleClose }
>
{ __( 'Cancel', 'google-site-kit' ) }
</Button>
) }
</Fragment>
);
} else if ( hasSettings || ! forceActive ) {
primaryColumn = (
<Link
className="googlesitekit-settings-module__edit-button"
to={ `/connected-services/${ slug }/edit` }
onClick={ handleEdit }
aria-label={ sprintf(
/* translators: %s: module name */
__( 'Edit %s settings', 'google-site-kit' ),
name
) }
trailingIcon={
<PencilIcon
className="googlesitekit-settings-module__edit-button-icon"
width={ 10 }
height={ 10 }
/>
}
>
{ __( 'Edit', 'google-site-kit' ) }
</Link>
);
}
if ( isEditing && ! forceActive ) {
secondaryColumn = (
<Link
className="googlesitekit-settings-module__remove-button"
onClick={ handleDialog }
danger
trailingIcon={
<TrashIcon
className="googlesitekit-settings-module__remove-button-icon"
width={ 13 }
height={ 13 }
/>
}
>
{ sprintf(
/* translators: %s: module name */
__( 'Disconnect %s from Site Kit', 'google-site-kit' ),
name
) }
</Link>
);
} else if ( ! isEditing && moduleHomepage ) {
secondaryColumn = (
<Link
href={ moduleHomepage }
className="googlesitekit-settings-module__cta-button"
external
>
{ sprintf(
/* translators: %s: module name */
__( 'See full details in %s', 'google-site-kit' ),
name
) }
</Link>
);
}
return (
<footer className="googlesitekit-settings-module__footer">
<Grid>
<Row>
<Cell lgSize={ 6 } mdSize={ 8 } smSize={ 4 }>
{ primaryColumn }
</Cell>
<Cell
lgSize={ 6 }
mdSize={ 8 }
smSize={ 4 }
alignMiddle
lgAlignRight
>
{ secondaryColumn }
</Cell>
</Row>
</Grid>
</footer>
);
}
Footer.propTypes = {
slug: PropTypes.string.isRequired,
};