-
Notifications
You must be signed in to change notification settings - Fork 277
/
settings-modules.js
294 lines (267 loc) · 7.86 KB
/
settings-modules.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
/**
* SettingsModules component.
*
* Site Kit by Google, Copyright 2019 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.
*/
import SettingsModule from './settings-module';
import Layout from 'GoogleComponents/layout/layout';
import Notification from 'GoogleComponents/notifications/notification';
import SettingsOverlay from './settings-overlay';
const { __ } = wp.i18n;
const { map, filter, sortBy } = lodash;
const { Component, Fragment } = wp.element;
const { applyFilters } = wp.hooks;
class SettingsModules extends Component {
constructor( props ) {
super( props );
this.state = {
error: false,
isEditing: {},
openModules: {},
isSaving: false,
};
this.mapToModule = this.mapToModule.bind( this );
this.updateModulesList = this.updateModulesList.bind( this );
this.handleButtonAction = this.handleButtonAction.bind( this );
this.handleAccordion = this.handleAccordion.bind( this );
}
componentDidMount() {
if ( googlesitekit.editmodule && googlesitekit.modules[ googlesitekit.editmodule ].active ) {
this.handleButtonAction( `${googlesitekit.editmodule}-module`, 'edit' );
}
}
updateModulesList() {
this.forceUpdate();
}
handleAccordion( module, e ) {
// Set focus on heading when clicked.
e.target.closest( '.googlesitekit-settings-module__header' ).focus();
this.setState( prevState => {
return {
openModules: {
...prevState.openModules,
[ module ]: ! prevState.openModules[ module ],
}
};
} );
}
/**
* Handle clicks on the Edit, Cancel and Confirm buttons.
*
* @param {string} module The module slug.
* @param {string} action The action being performed, one of 'edit', 'cancel' or 'confirm'.
* @param {boolean} nothingToSave Skip saving for this click.
*/
handleButtonAction( module, action, nothingToSave = false ) {
if ( 'confirm' === action ) {
const modulePromise = applyFilters( 'googlekit.SettingsConfirmed', false, module );
if ( nothingToSave ) {
this.setState( prevState => {
return {
isSaving: false,
error: false,
isEditing: {
...prevState.isEditing,
[ module ]: ! prevState.isEditing[ module ]
}
};
} );
return;
}
this.setState( { isSaving: module } );
if ( ! modulePromise ) {
return;
}
modulePromise.then( () => {
this.setState( prevState => {
return {
isSaving: false,
error: false,
isEditing: {
...prevState.isEditing,
[ module ]: ! prevState.isEditing[ module ]
}
};
} );
} ).catch( ( err ) => {
this.setState( {
isSaving: false,
error: {
errorCode: err.code,
errorMsg: err.message,
},
} );
} );
} else {
this.setState( prevState => {
return {
isEditing: {
...prevState.isEditing,
[ module ]: ! prevState.isEditing[ module ]
},
error: false, // Reset error state when switching modules.
};
} );
}
}
settingsModuleComponent( module, isSaving ) {
const { provides } = googlesitekit.modules[ module.slug ];
const { isEditing, openModules, error } = this.state;
const isOpen = openModules[ module.slug ] || false;
return (
<SettingsModule
key={ module.slug + '-module' }
slug={ module.slug }
name={ module.name }
description={ module.description }
homepage={ module.homepage }
learnmore={ module.learnMore }
active={ module.active }
hasSettings= { module.hasSettings }
autoActivate={ module.autoActivate }
updateModulesList={ this.updateModulesList }
handleEdit={ this.handleButtonAction }
handleConfirm
isEditing={ isEditing }
isOpen={ isOpen }
handleAccordion={ this.handleAccordion }
handleDialog={ this.handleDialog }
provides={ provides }
isSaving={ isSaving }
screenId={ module.screenId }
error={ error }
/>
);
}
/**
* Return list of modules markup.
*
* @param {object} modules List of modules
* @param {boolean} active Sets styling for active modules, helps with parent/child grouping.
*/
mapToModule( modules, active = false ) {
const { isSaving } = this.state;
if ( active ) {
return map( modules, function mapFn( module ) {
return (
<Fragment key={ module.slug + '-module-wrapper' }>
{ this.settingsModuleComponent( module, isSaving ) }
</Fragment>
);
}.bind( this ) );
} else {
return map( modules, function mapFn( module ) {
return (
<div
className="mdc-layout-grid__cell mdc-layout-grid__cell--span-4"
key={ module.slug + '-module-wrapper' }
>
{ this.settingsModuleComponent( module, isSaving ) }
</div>
);
}.bind( this ) );
}
}
render() {
const { isEditing } = this.state;
const { activeTab } = this.props;
const modulesBeingEdited = filter( isEditing, ( module ) => module );
const editActive = 0 < modulesBeingEdited.length;
if ( ! googlesitekit || ! googlesitekit.modules ) {
return null;
}
const { modules } = googlesitekit;
const activeModules = this.mapToModule(
sortBy(
filter(
modules,
function( module ) {
return module.active;
}
),
'sort'
),
true
);
const inactiveModules = this.mapToModule(
sortBy(
filter(
modules,
{
active: false,
}
),
'sort'
)
);
const inactiveModulesAvailable = 0 < inactiveModules.length;
return (
<Fragment>
{ /* Active Modules*/ }
{ (
0 < activeModules.length && // If we have active modules.
0 === activeTab ) && // If <SettingsApp/> is on the Connected tab. TODO this could be removed after refactoring this into separate components.
<div className="
mdc-layout-grid__cell
mdc-layout-grid__cell--span-12
">
<Layout>
{ activeModules }
</Layout>
</div>
}
{ /* Inactive Modules */ }
{ 1 === activeTab && // If <SettingsApp/> is on the Add tab. TODO this could be removed after refactoring this into separate components.
inactiveModulesAvailable && // We have inactive modules available.
<div className="
mdc-layout-grid__cell
mdc-layout-grid__cell--span-12
">
<Layout
header
title={ __( 'Connect More Services to Gain More Insights', 'google-site-kit' ) }
relative
>
<div className="mdc-layout-grid">
<div className="mdc-layout-grid__inner">
{ 0 < inactiveModules.length && inactiveModules }
</div>
</div>
{ /* TODO: Need some input here with regards to changing state */ }
{ editActive && <SettingsOverlay/> }
</Layout>
</div>
}
{ 1 === activeTab && // If <SettingsApp/> is on the Add tab. TODO this could be removed after refactoring this into separate components.
! inactiveModulesAvailable && // If we have no active modules.
<div className="
mdc-layout-grid__cell
mdc-layout-grid__cell--span-12
">
<Notification
id="no-more-modules"
title={ __( 'Congrats, you’ve connected all services!', 'google-site-kit' ) }
description={ __( 'We’re working on adding new services to Site Kit by Google all the time, so please check back in the future.', 'google-site-kit' ) }
format="small"
smallImage={ `${ googlesitekit.admin.assetsRoot }images/thumbs-up.png` }
type="win-success"
/>
</div>
}
</Fragment>
);
}
}
export default SettingsModules;