-
Notifications
You must be signed in to change notification settings - Fork 4k
/
actions.js
277 lines (270 loc) · 6.99 KB
/
actions.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
/**
* WordPress dependencies
*/
import { __, sprintf, _n } from '@wordpress/i18n';
import { useDispatch } from '@wordpress/data';
import { useState } from '@wordpress/element';
import { store as coreStore } from '@wordpress/core-data';
import { store as noticesStore } from '@wordpress/notices';
import { decodeEntities } from '@wordpress/html-entities';
import {
Button,
TextControl,
__experimentalText as Text,
__experimentalHStack as HStack,
__experimentalVStack as VStack,
} from '@wordpress/components';
/**
* Internal dependencies
*/
import { unlock } from '../../lock-unlock';
import { store as editSiteStore } from '../../store';
import isTemplateRevertable from '../../utils/is-template-revertable';
import isTemplateRemovable from '../../utils/is-template-removable';
import { TEMPLATE_POST_TYPE } from '../../utils/constants';
export const resetTemplateAction = {
id: 'reset-template',
label: __( 'Clear customizations' ),
isEligible: isTemplateRevertable,
supportsBulk: true,
hideModalHeader: true,
RenderModal: ( { items, closeModal, onPerform } ) => {
const { revertTemplate } = useDispatch( editSiteStore );
const { saveEditedEntityRecord } = useDispatch( coreStore );
const { createSuccessNotice, createErrorNotice } =
useDispatch( noticesStore );
const onConfirm = async () => {
try {
for ( const template of items ) {
await revertTemplate( template, {
allowUndo: false,
} );
await saveEditedEntityRecord(
'postType',
template.type,
template.id
);
}
createSuccessNotice(
items.length > 1
? sprintf(
/* translators: The number of items. */
__( '%s items reverted.' ),
items.length
)
: sprintf(
/* translators: The template/part's name. */
__( '"%s" reverted.' ),
decodeEntities( items[ 0 ].title.rendered )
),
{
type: 'snackbar',
id: 'edit-site-template-reverted',
}
);
} catch ( error ) {
let fallbackErrorMessage;
if ( items[ 0 ].type === TEMPLATE_POST_TYPE ) {
fallbackErrorMessage =
items.length === 1
? __(
'An error occurred while reverting the template.'
)
: __(
'An error occurred while reverting the templates.'
);
} else {
fallbackErrorMessage =
items.length === 1
? __(
'An error occurred while reverting the template part.'
)
: __(
'An error occurred while reverting the template parts.'
);
}
const errorMessage =
error.message && error.code !== 'unknown_error'
? error.message
: fallbackErrorMessage;
createErrorNotice( errorMessage, { type: 'snackbar' } );
}
};
return (
<VStack spacing="5">
<Text>
{ __(
'Are you sure you want to clear these customizations?'
) }
</Text>
<HStack justify="right">
<Button variant="tertiary" onClick={ closeModal }>
{ __( 'Cancel' ) }
</Button>
<Button
variant="primary"
onClick={ async () => {
await onConfirm( items );
onPerform?.();
closeModal();
} }
>
{ __( 'Clear' ) }
</Button>
</HStack>
</VStack>
);
},
};
export const deleteTemplateAction = {
id: 'delete-template',
label: __( 'Delete' ),
isEligible: isTemplateRemovable,
supportsBulk: true,
hideModalHeader: true,
RenderModal: ( { items: templates, closeModal, onPerform } ) => {
const { removeTemplates } = unlock( useDispatch( editSiteStore ) );
return (
<VStack spacing="5">
<Text>
{ templates.length > 1
? sprintf(
// translators: %d: number of items to delete.
_n(
'Delete %d item?',
'Delete %d items?',
templates.length
),
templates.length
)
: sprintf(
// translators: %s: The template or template part's titles
__( 'Delete "%s"?' ),
decodeEntities(
templates?.[ 0 ]?.title?.rendered
)
) }
</Text>
<HStack justify="right">
<Button variant="tertiary" onClick={ closeModal }>
{ __( 'Cancel' ) }
</Button>
<Button
variant="primary"
onClick={ async () => {
await removeTemplates( templates, {
allowUndo: false,
} );
onPerform?.();
closeModal();
} }
>
{ __( 'Delete' ) }
</Button>
</HStack>
</VStack>
);
},
};
export const renameTemplateAction = {
id: 'rename-template',
label: __( 'Rename' ),
isEligible: ( template ) => {
// We can only remove templates or template parts that can be removed.
// Additionally in the case of templates, we can only remove custom templates.
if (
! isTemplateRemovable( template ) ||
( template.type === TEMPLATE_POST_TYPE && ! template.is_custom )
) {
return false;
}
return true;
},
RenderModal: ( { items: templates, closeModal } ) => {
const template = templates[ 0 ];
const title = decodeEntities( template.title.rendered );
const [ editedTitle, setEditedTitle ] = useState( title );
const {
editEntityRecord,
__experimentalSaveSpecifiedEntityEdits: saveSpecifiedEntityEdits,
} = useDispatch( coreStore );
const { createSuccessNotice, createErrorNotice } =
useDispatch( noticesStore );
async function onTemplateRename( event ) {
event.preventDefault();
try {
await editEntityRecord(
'postType',
template.type,
template.id,
{
title: editedTitle,
}
);
// Update state before saving rerenders the list.
setEditedTitle( '' );
closeModal();
// Persist edited entity.
await saveSpecifiedEntityEdits(
'postType',
template.type,
template.id,
[ 'title' ], // Only save title to avoid persisting other edits.
{
throwOnError: true,
}
);
createSuccessNotice(
template.type === TEMPLATE_POST_TYPE
? __( 'Template renamed.' )
: __( 'Template part renamed.' ),
{
type: 'snackbar',
}
);
} catch ( error ) {
const fallbackErrorMessage =
template.type === TEMPLATE_POST_TYPE
? __( 'An error occurred while renaming the template.' )
: __(
'An error occurred while renaming the template part.'
);
const errorMessage =
error.message && error.code !== 'unknown_error'
? error.message
: fallbackErrorMessage;
createErrorNotice( errorMessage, { type: 'snackbar' } );
}
}
return (
<form onSubmit={ onTemplateRename }>
<VStack spacing="5">
<TextControl
__nextHasNoMarginBottom
__next40pxDefaultSize
label={ __( 'Name' ) }
value={ editedTitle }
onChange={ setEditedTitle }
required
/>
<HStack justify="right">
<Button
variant="tertiary"
onClick={ closeModal }
__next40pxDefaultSize
>
{ __( 'Cancel' ) }
</Button>
<Button
variant="primary"
type="submit"
__next40pxDefaultSize
>
{ __( 'Save' ) }
</Button>
</HStack>
</VStack>
</form>
);
},
};