-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.js
185 lines (166 loc) · 4.56 KB
/
index.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
/**
* External dependencies
*/
import { some, groupBy } from 'lodash';
/**
* WordPress dependencies
*/
import { Button } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { useSelect, useDispatch } from '@wordpress/data';
import { useState, useCallback, useRef } from '@wordpress/element';
import { store as coreStore } from '@wordpress/core-data';
import { __experimentalUseDialog as useDialog } from '@wordpress/compose';
import { close as closeIcon } from '@wordpress/icons';
/**
* Internal dependencies
*/
import EntityTypeList from './entity-type-list';
const TRANSLATED_SITE_PROTPERTIES = {
title: __( 'Title' ),
description: __( 'Tagline' ),
site_logo: __( 'Logo' ),
site_icon: __( 'Icon' ),
show_on_front: __( 'Show on front' ),
page_on_front: __( 'Page on front' ),
};
export default function EntitiesSavedStates( { close } ) {
const saveButtonRef = useRef();
const { dirtyEntityRecords } = useSelect( ( select ) => {
const dirtyRecords = select(
coreStore
).__experimentalGetDirtyEntityRecords();
// Remove site object and decouple into its edited pieces.
const dirtyRecordsWithoutSite = dirtyRecords.filter(
( record ) => ! ( record.kind === 'root' && record.name === 'site' )
);
const siteEdits = select( coreStore ).getEntityRecordEdits(
'root',
'site'
);
const siteEditsAsEntities = [];
for ( const property in siteEdits ) {
siteEditsAsEntities.push( {
kind: 'root',
name: 'site',
title: TRANSLATED_SITE_PROTPERTIES[ property ] || property,
property,
} );
}
const dirtyRecordsWithSiteItems = [
...dirtyRecordsWithoutSite,
...siteEditsAsEntities,
];
return {
dirtyEntityRecords: dirtyRecordsWithSiteItems,
};
}, [] );
const {
saveEditedEntityRecord,
__experimentalSaveSpecifiedEntityEdits: saveSpecifiedEntityEdits,
} = useDispatch( coreStore );
// To group entities by type.
const partitionedSavables = Object.values(
groupBy( dirtyEntityRecords, 'name' )
);
// Unchecked entities to be ignored by save function.
const [ unselectedEntities, _setUnselectedEntities ] = useState( [] );
const setUnselectedEntities = (
{ kind, name, key, property },
checked
) => {
if ( checked ) {
_setUnselectedEntities(
unselectedEntities.filter(
( elt ) =>
elt.kind !== kind ||
elt.name !== name ||
elt.key !== key ||
elt.property !== property
)
);
} else {
_setUnselectedEntities( [
...unselectedEntities,
{ kind, name, key, property },
] );
}
};
const saveCheckedEntities = () => {
const entitiesToSave = dirtyEntityRecords.filter(
( { kind, name, key, property } ) => {
return ! some(
unselectedEntities,
( elt ) =>
elt.kind === kind &&
elt.name === name &&
elt.key === key &&
elt.property === property
);
}
);
close( entitiesToSave );
const siteItemsToSave = [];
entitiesToSave.forEach( ( { kind, name, key, property } ) => {
if ( 'root' === kind && 'site' === name ) {
siteItemsToSave.push( property );
} else {
saveEditedEntityRecord( kind, name, key );
}
} );
saveSpecifiedEntityEdits( 'root', 'site', undefined, siteItemsToSave );
};
// Explicitly define this with no argument passed. Using `close` on
// its own will use the event object in place of the expected saved entities.
const dismissPanel = useCallback( () => close(), [ close ] );
const [ saveDialogRef, saveDialogProps ] = useDialog( {
onClose: () => dismissPanel(),
} );
return (
<div
ref={ saveDialogRef }
{ ...saveDialogProps }
className="entities-saved-states__panel"
>
<div className="entities-saved-states__panel-header">
<Button
ref={ saveButtonRef }
variant="primary"
disabled={
dirtyEntityRecords.length -
unselectedEntities.length ===
0
}
onClick={ saveCheckedEntities }
className="editor-entities-saved-states__save-button"
>
{ __( 'Save' ) }
</Button>
<Button
icon={ closeIcon }
onClick={ dismissPanel }
label={ __( 'Close panel' ) }
/>
</div>
<div className="entities-saved-states__text-prompt">
<strong>{ __( 'Select the changes you want to save' ) }</strong>
<p>
{ __(
'Some changes may affect other areas of your site.'
) }
</p>
</div>
{ partitionedSavables.map( ( list ) => {
return (
<EntityTypeList
key={ list[ 0 ].name }
list={ list }
closePanel={ dismissPanel }
unselectedEntities={ unselectedEntities }
setUnselectedEntities={ setUnselectedEntities }
/>
);
} ) }
</div>
);
}