-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathtemplate-actions.js
209 lines (202 loc) · 5.49 KB
/
template-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
/**
* WordPress dependencies
*/
import { backup, trash } from '@wordpress/icons';
import { __, sprintf } from '@wordpress/i18n';
import { useDispatch } from '@wordpress/data';
import { useMemo, 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 { 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 function useResetTemplateAction() {
const { revertTemplate } = useDispatch( editSiteStore );
const { saveEditedEntityRecord } = useDispatch( coreStore );
const { createSuccessNotice, createErrorNotice } =
useDispatch( noticesStore );
return useMemo(
() => ( {
id: 'reset-template',
label: __( 'Reset template' ),
isPrimary: true,
icon: backup,
isEligible: isTemplateRevertable,
async callback( template ) {
try {
await revertTemplate( template, { allowUndo: false } );
await saveEditedEntityRecord(
'postType',
template.type,
template.id
);
createSuccessNotice(
sprintf(
/* translators: The template/part's name. */
__( '"%s" reverted.' ),
decodeEntities( template.title.rendered )
),
{
type: 'snackbar',
id: 'edit-site-template-reverted',
}
);
} catch ( error ) {
const fallbackErrorMessage =
template.type === TEMPLATE_POST_TYPE
? __(
'An error occurred while reverting the template.'
)
: __(
'An error occurred while reverting the template part.'
);
const errorMessage =
error.message && error.code !== 'unknown_error'
? error.message
: fallbackErrorMessage;
createErrorNotice( errorMessage, { type: 'snackbar' } );
}
},
} ),
[
createErrorNotice,
createSuccessNotice,
revertTemplate,
saveEditedEntityRecord,
]
);
}
export const deleteTemplateAction = {
id: 'delete-template',
label: __( 'Delete template' ),
isPrimary: true,
icon: trash,
isEligible: isTemplateRemovable,
hideModalHeader: true,
RenderModal: ( { item: template, closeModal } ) => {
const { removeTemplate } = useDispatch( editSiteStore );
return (
<VStack spacing="5">
<Text>
{ sprintf(
// translators: %s: The template or template part's title.
__( 'Are you sure you want to delete "%s"?' ),
decodeEntities( template.title.rendered )
) }
</Text>
<HStack justify="right">
<Button variant="tertiary" onClick={ closeModal }>
{ __( 'Cancel' ) }
</Button>
<Button
variant="primary"
onClick={ () =>
removeTemplate( template, {
allowUndo: false,
} )
}
>
{ __( 'Delete' ) }
</Button>
</HStack>
</VStack>
);
},
};
export const renameTemplateAction = {
id: 'rename-template',
label: __( 'Rename' ),
isEligible: ( template ) =>
isTemplateRemovable( template ) && template.is_custom,
RenderModal: ( { item: template, closeModal } ) => {
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,
}
);
// TODO: this action will be reused in template parts list, so
// let's keep this for a bit, even it's always a `template` now.
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
label={ __( 'Name' ) }
value={ editedTitle }
onChange={ setEditedTitle }
required
/>
<HStack justify="right">
<Button variant="tertiary" onClick={ closeModal }>
{ __( 'Cancel' ) }
</Button>
<Button variant="primary" type="submit">
{ __( 'Save' ) }
</Button>
</HStack>
</VStack>
</form>
);
},
};