-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
Copy pathrename-menu-item.js
120 lines (108 loc) · 2.81 KB
/
rename-menu-item.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
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { useState } from '@wordpress/element';
import { useDispatch } from '@wordpress/data';
import {
Button,
MenuItem,
Modal,
TextControl,
__experimentalHStack as HStack,
__experimentalVStack as VStack,
} from '@wordpress/components';
import { store as coreStore } from '@wordpress/core-data';
import { store as noticesStore } from '@wordpress/notices';
import { decodeEntities } from '@wordpress/html-entities';
export default function RenameMenuItem( { template, onClose } ) {
const title = decodeEntities( template.title.rendered );
const [ editedTitle, setEditedTitle ] = useState( title );
const [ isModalOpen, setIsModalOpen ] = useState( false );
const {
editEntityRecord,
__experimentalSaveSpecifiedEntityEdits: saveSpecifiedEntityEdits,
} = useDispatch( coreStore );
const { createSuccessNotice, createErrorNotice } =
useDispatch( noticesStore );
if ( template.type === 'wp_template' && ! template.is_custom ) {
return null;
}
async function onTemplateRename( event ) {
event.preventDefault();
try {
await editEntityRecord( 'postType', template.type, template.id, {
title: editedTitle,
} );
// Update state before saving rerenders the list.
setEditedTitle( '' );
setIsModalOpen( false );
onClose();
// Persist edited entity.
await saveSpecifiedEntityEdits(
'postType',
template.type,
template.id,
[ 'title' ], // Only save title to avoid persisting other edits.
{
throwOnError: true,
}
);
createSuccessNotice( __( 'Entity renamed.' ), {
type: 'snackbar',
} );
} catch ( error ) {
const errorMessage =
error.message && error.code !== 'unknown_error'
? error.message
: __( 'An error occurred while renaming the entity.' );
createErrorNotice( errorMessage, { type: 'snackbar' } );
}
}
return (
<>
<MenuItem
onClick={ () => {
setIsModalOpen( true );
setEditedTitle( title );
} }
>
{ __( 'Rename' ) }
</MenuItem>
{ isModalOpen && (
<Modal
title={ __( 'Rename' ) }
onRequestClose={ () => {
setIsModalOpen( false );
} }
overlayClassName="edit-site-list__rename-modal"
>
<form onSubmit={ onTemplateRename }>
<VStack spacing="5">
<TextControl
__nextHasNoMarginBottom
label={ __( 'Name' ) }
value={ editedTitle }
onChange={ setEditedTitle }
required
/>
<HStack justify="right">
<Button
variant="tertiary"
onClick={ () => {
setIsModalOpen( false );
} }
>
{ __( 'Cancel' ) }
</Button>
<Button variant="primary" type="submit">
{ __( 'Save' ) }
</Button>
</HStack>
</VStack>
</form>
</Modal>
) }
</>
);
}