@wordpress/fields 0.0.4 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (192) hide show
  1. package/CHANGELOG.md +2 -0
  2. package/README.md +20 -0
  3. package/build/actions/delete-post.js +130 -0
  4. package/build/actions/delete-post.js.map +1 -0
  5. package/build/actions/{pattern/duplicate-pattern.js → duplicate-pattern.js} +1 -1
  6. package/build/actions/duplicate-pattern.js.map +1 -0
  7. package/build/actions/{base-post/duplicate-post.js → duplicate-post.js} +2 -2
  8. package/build/actions/duplicate-post.js.map +1 -0
  9. package/build/actions/duplicate-post.native.js.map +1 -0
  10. package/build/actions/{pattern/export-pattern.js → export-pattern.js} +1 -1
  11. package/build/actions/export-pattern.js.map +1 -0
  12. package/build/actions/export-pattern.native.js.map +1 -0
  13. package/build/actions/index.js +105 -32
  14. package/build/actions/index.js.map +1 -1
  15. package/build/actions/{common/permanently-delete-post.js → permanently-delete-post.js} +1 -1
  16. package/build/actions/permanently-delete-post.js.map +1 -0
  17. package/build/actions/rename-post.js +124 -0
  18. package/build/actions/rename-post.js.map +1 -0
  19. package/build/actions/{base-post/reorder-page.js → reorder-page.js} +1 -1
  20. package/build/actions/reorder-page.js.map +1 -0
  21. package/build/actions/reorder-page.native.js.map +1 -0
  22. package/build/actions/reset-post.js +206 -0
  23. package/build/actions/reset-post.js.map +1 -0
  24. package/build/actions/restore-post.js +112 -0
  25. package/build/actions/restore-post.js.map +1 -0
  26. package/build/actions/trash-post.js +144 -0
  27. package/build/actions/trash-post.js.map +1 -0
  28. package/build/actions/view-post-revisions.js.map +1 -0
  29. package/build/actions/view-post.js.map +1 -0
  30. package/build/index.native.js +2 -2
  31. package/build/index.native.js.map +1 -1
  32. package/build/mutation/index.js +139 -0
  33. package/build/mutation/index.js.map +1 -0
  34. package/build/types.js.map +1 -1
  35. package/build-module/actions/delete-post.js +123 -0
  36. package/build-module/actions/delete-post.js.map +1 -0
  37. package/build-module/actions/{pattern/duplicate-pattern.js → duplicate-pattern.js} +1 -1
  38. package/build-module/actions/duplicate-pattern.js.map +1 -0
  39. package/build-module/actions/{base-post/duplicate-post.js → duplicate-post.js} +2 -2
  40. package/build-module/actions/duplicate-post.js.map +1 -0
  41. package/build-module/actions/duplicate-post.native.js.map +1 -0
  42. package/build-module/actions/{pattern/export-pattern.js → export-pattern.js} +1 -1
  43. package/build-module/actions/export-pattern.js.map +1 -0
  44. package/build-module/actions/export-pattern.native.js.map +1 -0
  45. package/build-module/actions/index.js +15 -3
  46. package/build-module/actions/index.js.map +1 -1
  47. package/build-module/actions/{common/permanently-delete-post.js → permanently-delete-post.js} +1 -1
  48. package/build-module/actions/permanently-delete-post.js.map +1 -0
  49. package/build-module/actions/rename-post.js +118 -0
  50. package/build-module/actions/rename-post.js.map +1 -0
  51. package/build-module/actions/{base-post/reorder-page.js → reorder-page.js} +1 -1
  52. package/build-module/actions/reorder-page.js.map +1 -0
  53. package/build-module/actions/reorder-page.native.js.map +1 -0
  54. package/build-module/actions/reset-post.js +198 -0
  55. package/build-module/actions/reset-post.js.map +1 -0
  56. package/build-module/actions/restore-post.js +104 -0
  57. package/build-module/actions/restore-post.js.map +1 -0
  58. package/build-module/actions/trash-post.js +137 -0
  59. package/build-module/actions/trash-post.js.map +1 -0
  60. package/build-module/actions/view-post-revisions.js.map +1 -0
  61. package/build-module/actions/view-post.js.map +1 -0
  62. package/build-module/index.native.js +2 -2
  63. package/build-module/index.native.js.map +1 -1
  64. package/build-module/mutation/index.js +131 -0
  65. package/build-module/mutation/index.js.map +1 -0
  66. package/build-module/types.js.map +1 -1
  67. package/build-types/actions/delete-post.d.ts +5 -0
  68. package/build-types/actions/delete-post.d.ts.map +1 -0
  69. package/build-types/actions/{pattern/duplicate-pattern.d.ts → duplicate-pattern.d.ts} +1 -1
  70. package/build-types/actions/duplicate-pattern.d.ts.map +1 -0
  71. package/build-types/actions/{base-post/duplicate-post.d.ts → duplicate-post.d.ts} +1 -1
  72. package/build-types/actions/duplicate-post.d.ts.map +1 -0
  73. package/build-types/actions/duplicate-post.native.d.ts.map +1 -0
  74. package/build-types/actions/{pattern/export-pattern.d.ts → export-pattern.d.ts} +1 -1
  75. package/build-types/actions/export-pattern.d.ts.map +1 -0
  76. package/build-types/actions/export-pattern.native.d.ts.map +1 -0
  77. package/build-types/actions/index.d.ts +15 -3
  78. package/build-types/actions/index.d.ts.map +1 -1
  79. package/build-types/actions/{common/permanently-delete-post.d.ts → permanently-delete-post.d.ts} +1 -1
  80. package/build-types/actions/permanently-delete-post.d.ts.map +1 -0
  81. package/build-types/actions/rename-post.d.ts +5 -0
  82. package/build-types/actions/rename-post.d.ts.map +1 -0
  83. package/build-types/actions/{base-post/reorder-page.d.ts → reorder-page.d.ts} +1 -1
  84. package/build-types/actions/reorder-page.d.ts.map +1 -0
  85. package/build-types/actions/reorder-page.native.d.ts.map +1 -0
  86. package/build-types/actions/reset-post.d.ts +5 -0
  87. package/build-types/actions/reset-post.d.ts.map +1 -0
  88. package/build-types/actions/restore-post.d.ts +5 -0
  89. package/build-types/actions/restore-post.d.ts.map +1 -0
  90. package/build-types/actions/trash-post.d.ts +5 -0
  91. package/build-types/actions/trash-post.d.ts.map +1 -0
  92. package/build-types/actions/{common/view-post-revisions.d.ts → view-post-revisions.d.ts} +1 -1
  93. package/build-types/actions/view-post-revisions.d.ts.map +1 -0
  94. package/build-types/actions/{base-post/view-post.d.ts → view-post.d.ts} +1 -1
  95. package/build-types/actions/view-post.d.ts.map +1 -0
  96. package/build-types/index.native.d.ts +2 -2
  97. package/build-types/index.native.d.ts.map +1 -1
  98. package/build-types/mutation/index.d.ts +34 -0
  99. package/build-types/mutation/index.d.ts.map +1 -0
  100. package/build-types/types.d.ts +1 -0
  101. package/build-types/types.d.ts.map +1 -1
  102. package/package.json +21 -20
  103. package/src/actions/delete-post.tsx +203 -0
  104. package/src/actions/{pattern/duplicate-pattern.tsx → duplicate-pattern.tsx} +2 -2
  105. package/src/actions/{base-post/duplicate-post.tsx → duplicate-post.tsx} +3 -3
  106. package/src/actions/{pattern/export-pattern.tsx → export-pattern.tsx} +2 -2
  107. package/src/actions/index.ts +15 -3
  108. package/src/actions/{common/permanently-delete-post.tsx → permanently-delete-post.tsx} +2 -2
  109. package/src/actions/rename-post.tsx +145 -0
  110. package/src/actions/{base-post/reorder-page.tsx → reorder-page.tsx} +2 -2
  111. package/src/actions/reset-post.tsx +300 -0
  112. package/src/actions/restore-post.tsx +134 -0
  113. package/src/actions/trash-post.tsx +198 -0
  114. package/src/actions/{common/view-post-revisions.tsx → view-post-revisions.tsx} +1 -1
  115. package/src/actions/{base-post/view-post.tsx → view-post.tsx} +1 -1
  116. package/src/index.native.ts +2 -2
  117. package/src/mutation/index.ts +184 -0
  118. package/src/types.ts +1 -0
  119. package/tsconfig.json +2 -2
  120. package/tsconfig.tsbuildinfo +1 -1
  121. package/build/actions/base-post/duplicate-post.js.map +0 -1
  122. package/build/actions/base-post/duplicate-post.native.js.map +0 -1
  123. package/build/actions/base-post/index.js +0 -42
  124. package/build/actions/base-post/index.js.map +0 -1
  125. package/build/actions/base-post/reorder-page.js.map +0 -1
  126. package/build/actions/base-post/reorder-page.native.js.map +0 -1
  127. package/build/actions/base-post/view-post.js.map +0 -1
  128. package/build/actions/common/index.js +0 -21
  129. package/build/actions/common/index.js.map +0 -1
  130. package/build/actions/common/permanently-delete-post.js.map +0 -1
  131. package/build/actions/common/view-post-revisions.js.map +0 -1
  132. package/build/actions/pattern/duplicate-pattern.js.map +0 -1
  133. package/build/actions/pattern/export-pattern.js.map +0 -1
  134. package/build/actions/pattern/export-pattern.native.js.map +0 -1
  135. package/build/actions/pattern/index.js +0 -28
  136. package/build/actions/pattern/index.js.map +0 -1
  137. package/build/wordpress-editor.d.js +0 -2
  138. package/build/wordpress-editor.d.js.map +0 -1
  139. package/build-module/actions/base-post/duplicate-post.js.map +0 -1
  140. package/build-module/actions/base-post/duplicate-post.native.js.map +0 -1
  141. package/build-module/actions/base-post/index.js +0 -6
  142. package/build-module/actions/base-post/index.js.map +0 -1
  143. package/build-module/actions/base-post/reorder-page.js.map +0 -1
  144. package/build-module/actions/base-post/reorder-page.native.js.map +0 -1
  145. package/build-module/actions/base-post/view-post.js.map +0 -1
  146. package/build-module/actions/common/index.js +0 -3
  147. package/build-module/actions/common/index.js.map +0 -1
  148. package/build-module/actions/common/permanently-delete-post.js.map +0 -1
  149. package/build-module/actions/common/view-post-revisions.js.map +0 -1
  150. package/build-module/actions/pattern/duplicate-pattern.js.map +0 -1
  151. package/build-module/actions/pattern/export-pattern.js.map +0 -1
  152. package/build-module/actions/pattern/export-pattern.native.js.map +0 -1
  153. package/build-module/actions/pattern/index.js +0 -4
  154. package/build-module/actions/pattern/index.js.map +0 -1
  155. package/build-module/wordpress-editor.d.js +0 -2
  156. package/build-module/wordpress-editor.d.js.map +0 -1
  157. package/build-types/actions/base-post/duplicate-post.d.ts.map +0 -1
  158. package/build-types/actions/base-post/duplicate-post.native.d.ts.map +0 -1
  159. package/build-types/actions/base-post/index.d.ts +0 -6
  160. package/build-types/actions/base-post/index.d.ts.map +0 -1
  161. package/build-types/actions/base-post/reorder-page.d.ts.map +0 -1
  162. package/build-types/actions/base-post/reorder-page.native.d.ts.map +0 -1
  163. package/build-types/actions/base-post/view-post.d.ts.map +0 -1
  164. package/build-types/actions/common/index.d.ts +0 -3
  165. package/build-types/actions/common/index.d.ts.map +0 -1
  166. package/build-types/actions/common/permanently-delete-post.d.ts.map +0 -1
  167. package/build-types/actions/common/view-post-revisions.d.ts.map +0 -1
  168. package/build-types/actions/pattern/duplicate-pattern.d.ts.map +0 -1
  169. package/build-types/actions/pattern/export-pattern.d.ts.map +0 -1
  170. package/build-types/actions/pattern/export-pattern.native.d.ts.map +0 -1
  171. package/build-types/actions/pattern/index.d.ts +0 -4
  172. package/build-types/actions/pattern/index.d.ts.map +0 -1
  173. package/src/actions/base-post/index.ts +0 -5
  174. package/src/actions/common/index.ts +0 -2
  175. package/src/actions/pattern/index.ts +0 -3
  176. package/src/wordpress-editor.d.ts +0 -1
  177. /package/build/actions/{base-post/duplicate-post.native.js → duplicate-post.native.js} +0 -0
  178. /package/build/actions/{pattern/export-pattern.native.js → export-pattern.native.js} +0 -0
  179. /package/build/actions/{base-post/reorder-page.native.js → reorder-page.native.js} +0 -0
  180. /package/build/actions/{common/view-post-revisions.js → view-post-revisions.js} +0 -0
  181. /package/build/actions/{base-post/view-post.js → view-post.js} +0 -0
  182. /package/build-module/actions/{base-post/duplicate-post.native.js → duplicate-post.native.js} +0 -0
  183. /package/build-module/actions/{pattern/export-pattern.native.js → export-pattern.native.js} +0 -0
  184. /package/build-module/actions/{base-post/reorder-page.native.js → reorder-page.native.js} +0 -0
  185. /package/build-module/actions/{common/view-post-revisions.js → view-post-revisions.js} +0 -0
  186. /package/build-module/actions/{base-post/view-post.js → view-post.js} +0 -0
  187. /package/build-types/actions/{base-post/duplicate-post.native.d.ts → duplicate-post.native.d.ts} +0 -0
  188. /package/build-types/actions/{pattern/export-pattern.native.d.ts → export-pattern.native.d.ts} +0 -0
  189. /package/build-types/actions/{base-post/reorder-page.native.d.ts → reorder-page.native.d.ts} +0 -0
  190. /package/src/actions/{base-post/duplicate-post.native.tsx → duplicate-post.native.tsx} +0 -0
  191. /package/src/actions/{pattern/export-pattern.native.tsx → export-pattern.native.tsx} +0 -0
  192. /package/src/actions/{base-post/reorder-page.native.tsx → reorder-page.native.tsx} +0 -0
@@ -0,0 +1,203 @@
1
+ /**
2
+ * WordPress dependencies
3
+ */
4
+ import { trash } from '@wordpress/icons';
5
+ import { __, _n, sprintf } from '@wordpress/i18n';
6
+ import { useState } from '@wordpress/element';
7
+ import {
8
+ Button,
9
+ __experimentalText as Text,
10
+ __experimentalHStack as HStack,
11
+ __experimentalVStack as VStack,
12
+ } from '@wordpress/components';
13
+ // @ts-ignore
14
+ import { privateApis as patternsPrivateApis } from '@wordpress/patterns';
15
+ import type { Action } from '@wordpress/dataviews';
16
+ import { decodeEntities } from '@wordpress/html-entities';
17
+
18
+ /**
19
+ * Internal dependencies
20
+ */
21
+ import {
22
+ getItemTitle,
23
+ isTemplateOrTemplatePart,
24
+ isTemplateRemovable,
25
+ } from './utils';
26
+ import type { Pattern, Template, TemplatePart } from '../types';
27
+ import type { NoticeSettings } from '../mutation';
28
+ import { deletePostWithNotices } from '../mutation';
29
+ import { unlock } from '../lock-unlock';
30
+
31
+ const { PATTERN_TYPES } = unlock( patternsPrivateApis );
32
+
33
+ // This action is used for templates, patterns and template parts.
34
+ // Every other post type uses the similar `trashPostAction` which
35
+ // moves the post to trash.
36
+ const deletePostAction: Action< Template | TemplatePart | Pattern > = {
37
+ id: 'delete-post',
38
+ label: __( 'Delete' ),
39
+ isPrimary: true,
40
+ icon: trash,
41
+ isEligible( post ) {
42
+ if ( isTemplateOrTemplatePart( post ) ) {
43
+ return isTemplateRemovable( post );
44
+ }
45
+ // We can only remove user patterns.
46
+ return post.type === PATTERN_TYPES.user;
47
+ },
48
+ supportsBulk: true,
49
+ hideModalHeader: true,
50
+ RenderModal: ( { items, closeModal, onActionPerformed } ) => {
51
+ const [ isBusy, setIsBusy ] = useState( false );
52
+ const isResetting = items.every(
53
+ ( item ) => isTemplateOrTemplatePart( item ) && item?.has_theme_file
54
+ );
55
+ return (
56
+ <VStack spacing="5">
57
+ <Text>
58
+ { items.length > 1
59
+ ? sprintf(
60
+ // translators: %d: number of items to delete.
61
+ _n(
62
+ 'Delete %d item?',
63
+ 'Delete %d items?',
64
+ items.length
65
+ ),
66
+ items.length
67
+ )
68
+ : sprintf(
69
+ // translators: %s: The template or template part's titles
70
+ __( 'Delete "%s"?' ),
71
+ getItemTitle( items[ 0 ] )
72
+ ) }
73
+ </Text>
74
+ <HStack justify="right">
75
+ <Button
76
+ variant="tertiary"
77
+ onClick={ closeModal }
78
+ disabled={ isBusy }
79
+ accessibleWhenDisabled
80
+ __next40pxDefaultSize
81
+ >
82
+ { __( 'Cancel' ) }
83
+ </Button>
84
+ <Button
85
+ variant="primary"
86
+ onClick={ async () => {
87
+ setIsBusy( true );
88
+ const notice: NoticeSettings<
89
+ Template | TemplatePart | Pattern
90
+ > = {
91
+ success: {
92
+ messages: {
93
+ getMessage: ( item ) => {
94
+ return isResetting
95
+ ? sprintf(
96
+ /* translators: The template/part's name. */
97
+ __( '"%s" reset.' ),
98
+ decodeEntities(
99
+ getItemTitle( item )
100
+ )
101
+ )
102
+ : sprintf(
103
+ /* translators: The template/part's name. */
104
+ __( '"%s" deleted.' ),
105
+ decodeEntities(
106
+ getItemTitle( item )
107
+ )
108
+ );
109
+ },
110
+ getBatchMessage: () => {
111
+ return isResetting
112
+ ? __( 'Items reset.' )
113
+ : __( 'Items deleted.' );
114
+ },
115
+ },
116
+ },
117
+ error: {
118
+ messages: {
119
+ getMessage: ( error ) => {
120
+ if ( error.size === 1 ) {
121
+ return [ ...error ][ 0 ];
122
+ }
123
+ return isResetting
124
+ ? __(
125
+ 'An error occurred while reverting the item.'
126
+ )
127
+ : __(
128
+ 'An error occurred while deleting the item.'
129
+ );
130
+ },
131
+ getBatchMessage: ( errors ) => {
132
+ if ( errors.size === 0 ) {
133
+ return isResetting
134
+ ? __(
135
+ 'An error occurred while reverting the items.'
136
+ )
137
+ : __(
138
+ 'An error occurred while deleting the items.'
139
+ );
140
+ }
141
+
142
+ if ( errors.size === 1 ) {
143
+ return isResetting
144
+ ? sprintf(
145
+ /* translators: %s: an error message */
146
+ __(
147
+ 'An error occurred while reverting the items: %s'
148
+ ),
149
+ [ ...errors ][ 0 ]
150
+ )
151
+ : sprintf(
152
+ /* translators: %s: an error message */
153
+ __(
154
+ 'An error occurred while deleting the items: %s'
155
+ ),
156
+ [ ...errors ][ 0 ]
157
+ );
158
+ }
159
+
160
+ return isResetting
161
+ ? sprintf(
162
+ /* translators: %s: a list of comma separated error messages */
163
+ __(
164
+ 'Some errors occurred while reverting the items: %s'
165
+ ),
166
+ [ ...errors ].join(
167
+ ','
168
+ )
169
+ )
170
+ : sprintf(
171
+ /* translators: %s: a list of comma separated error messages */
172
+ __(
173
+ 'Some errors occurred while deleting the items: %s'
174
+ ),
175
+ [ ...errors ].join(
176
+ ','
177
+ )
178
+ );
179
+ },
180
+ },
181
+ },
182
+ };
183
+
184
+ await deletePostWithNotices( items, notice, {
185
+ onActionPerformed,
186
+ } );
187
+ setIsBusy( false );
188
+ closeModal?.();
189
+ } }
190
+ isBusy={ isBusy }
191
+ disabled={ isBusy }
192
+ accessibleWhenDisabled
193
+ __next40pxDefaultSize
194
+ >
195
+ { __( 'Delete' ) }
196
+ </Button>
197
+ </HStack>
198
+ </VStack>
199
+ );
200
+ },
201
+ };
202
+
203
+ export default deletePostAction;
@@ -9,8 +9,8 @@ import type { Action } from '@wordpress/dataviews';
9
9
  /**
10
10
  * Internal dependencies
11
11
  */
12
- import { unlock } from '../../lock-unlock';
13
- import type { Pattern } from '../../types';
12
+ import { unlock } from '../lock-unlock';
13
+ import type { Pattern } from '../types';
14
14
 
15
15
  // Patterns.
16
16
  const { CreatePatternModalContents, useDuplicatePatternProps } =
@@ -18,9 +18,9 @@ import type { Action } from '@wordpress/dataviews';
18
18
  /**
19
19
  * Internal dependencies
20
20
  */
21
- import { titleField } from '../../fields';
22
- import type { BasePost, CoreDataError } from '../../types';
23
- import { getItemTitle } from '../utils';
21
+ import { titleField } from '../fields';
22
+ import type { BasePost, CoreDataError } from '../types';
23
+ import { getItemTitle } from './utils';
24
24
 
25
25
  const fields = [ titleField ];
26
26
  const formDuplicateAction = {
@@ -15,8 +15,8 @@ import type { Action } from '@wordpress/dataviews';
15
15
  /**
16
16
  * Internal dependencies
17
17
  */
18
- import type { Pattern } from '../../types';
19
- import { getItemTitle } from '../utils';
18
+ import type { Pattern } from '../types';
19
+ import { getItemTitle } from './utils';
20
20
 
21
21
  function getJsonFromItem( item: Pattern ) {
22
22
  return JSON.stringify(
@@ -1,3 +1,15 @@
1
- export * from './base-post';
2
- export * from './common';
3
- export * from './pattern';
1
+ export { default as viewPost } from './view-post';
2
+ export { default as reorderPage } from './reorder-page';
3
+ export { default as reorderPageNative } from './reorder-page.native';
4
+ export { default as duplicatePost } from './duplicate-post';
5
+ export { default as duplicatePostNative } from './duplicate-post.native';
6
+ export { default as renamePost } from './rename-post';
7
+ export { default as resetPost } from './reset-post';
8
+ export { default as duplicatePattern } from './duplicate-pattern';
9
+ export { default as exportPattern } from './export-pattern';
10
+ export { default as exportPatternNative } from './export-pattern.native';
11
+ export { default as viewPostRevisions } from './view-post-revisions';
12
+ export { default as permanentlyDeletePost } from './permanently-delete-post';
13
+ export { default as restorePost } from './restore-post';
14
+ export { default as trashPost } from './trash-post';
15
+ export { default as deletePost } from './delete-post';
@@ -10,8 +10,8 @@ import { trash } from '@wordpress/icons';
10
10
  /**
11
11
  * Internal dependencies
12
12
  */
13
- import { getItemTitle, isTemplateOrTemplatePart } from '../utils';
14
- import type { CoreDataError, PostWithPermissions } from '../../types';
13
+ import { getItemTitle, isTemplateOrTemplatePart } from './utils';
14
+ import type { CoreDataError, PostWithPermissions } from '../types';
15
15
 
16
16
  const permanentlyDeletePost: Action< PostWithPermissions > = {
17
17
  id: 'permanently-delete',
@@ -0,0 +1,145 @@
1
+ /**
2
+ * WordPress dependencies
3
+ */
4
+ import { useDispatch } from '@wordpress/data';
5
+ import { store as coreStore } from '@wordpress/core-data';
6
+ import { __ } from '@wordpress/i18n';
7
+ import { useState } from '@wordpress/element';
8
+ // @ts-ignore
9
+ import { privateApis as patternsPrivateApis } from '@wordpress/patterns';
10
+ import {
11
+ Button,
12
+ TextControl,
13
+ __experimentalHStack as HStack,
14
+ __experimentalVStack as VStack,
15
+ } from '@wordpress/components';
16
+ import type { Action } from '@wordpress/dataviews';
17
+ import { store as noticesStore } from '@wordpress/notices';
18
+
19
+ /**
20
+ * Internal dependencies
21
+ */
22
+
23
+ import { unlock } from '../lock-unlock';
24
+ import {
25
+ getItemTitle,
26
+ isTemplateRemovable,
27
+ isTemplate,
28
+ isTemplatePart,
29
+ TEMPLATE_ORIGINS,
30
+ TEMPLATE_PART_POST_TYPE,
31
+ TEMPLATE_POST_TYPE,
32
+ } from './utils';
33
+ import type { CoreDataError, PostWithPermissions } from '../types';
34
+
35
+ // Patterns.
36
+ const { PATTERN_TYPES } = unlock( patternsPrivateApis );
37
+
38
+ const renamePost: Action< PostWithPermissions > = {
39
+ id: 'rename-post',
40
+ label: __( 'Rename' ),
41
+ isEligible( post ) {
42
+ if ( post.status === 'trash' ) {
43
+ return false;
44
+ }
45
+ // Templates, template parts and patterns have special checks for renaming.
46
+ if (
47
+ ! [
48
+ TEMPLATE_POST_TYPE,
49
+ TEMPLATE_PART_POST_TYPE,
50
+ ...Object.values( PATTERN_TYPES ),
51
+ ].includes( post.type )
52
+ ) {
53
+ return post.permissions?.update;
54
+ }
55
+
56
+ // In the case of templates, we can only rename custom templates.
57
+ if ( isTemplate( post ) ) {
58
+ return (
59
+ isTemplateRemovable( post ) &&
60
+ post.is_custom &&
61
+ post.permissions?.update
62
+ );
63
+ }
64
+
65
+ if ( isTemplatePart( post ) ) {
66
+ return (
67
+ post.source === TEMPLATE_ORIGINS.custom &&
68
+ ! post?.has_theme_file &&
69
+ post.permissions?.update
70
+ );
71
+ }
72
+
73
+ return post.type === PATTERN_TYPES.user && post.permissions?.update;
74
+ },
75
+ RenderModal: ( { items, closeModal, onActionPerformed } ) => {
76
+ const [ item ] = items;
77
+ const [ title, setTitle ] = useState( () => getItemTitle( item ) );
78
+ const { editEntityRecord, saveEditedEntityRecord } =
79
+ useDispatch( coreStore );
80
+ const { createSuccessNotice, createErrorNotice } =
81
+ useDispatch( noticesStore );
82
+
83
+ async function onRename( event: React.FormEvent ) {
84
+ event.preventDefault();
85
+ try {
86
+ await editEntityRecord( 'postType', item.type, item.id, {
87
+ title,
88
+ } );
89
+ // Update state before saving rerenders the list.
90
+ setTitle( '' );
91
+ closeModal?.();
92
+ // Persist edited entity.
93
+ await saveEditedEntityRecord( 'postType', item.type, item.id, {
94
+ throwOnError: true,
95
+ } );
96
+ createSuccessNotice( __( 'Name updated' ), {
97
+ type: 'snackbar',
98
+ } );
99
+ onActionPerformed?.( items );
100
+ } catch ( error ) {
101
+ const typedError = error as CoreDataError;
102
+ const errorMessage =
103
+ typedError.message && typedError.code !== 'unknown_error'
104
+ ? typedError.message
105
+ : __( 'An error occurred while updating the name' );
106
+ createErrorNotice( errorMessage, { type: 'snackbar' } );
107
+ }
108
+ }
109
+
110
+ return (
111
+ <form onSubmit={ onRename }>
112
+ <VStack spacing="5">
113
+ <TextControl
114
+ __nextHasNoMarginBottom
115
+ __next40pxDefaultSize
116
+ label={ __( 'Name' ) }
117
+ value={ title }
118
+ onChange={ setTitle }
119
+ required
120
+ />
121
+ <HStack justify="right">
122
+ <Button
123
+ __next40pxDefaultSize
124
+ variant="tertiary"
125
+ onClick={ () => {
126
+ closeModal?.();
127
+ } }
128
+ >
129
+ { __( 'Cancel' ) }
130
+ </Button>
131
+ <Button
132
+ __next40pxDefaultSize
133
+ variant="primary"
134
+ type="submit"
135
+ >
136
+ { __( 'Save' ) }
137
+ </Button>
138
+ </HStack>
139
+ </VStack>
140
+ </form>
141
+ );
142
+ },
143
+ };
144
+
145
+ export default renamePost;
@@ -17,8 +17,8 @@ import type { Action, RenderModalProps } from '@wordpress/dataviews';
17
17
  /**
18
18
  * Internal dependencies
19
19
  */
20
- import type { CoreDataError, BasePost } from '../../types';
21
- import { orderField } from '../../fields';
20
+ import type { CoreDataError, BasePost } from '../types';
21
+ import { orderField } from '../fields';
22
22
 
23
23
  const fields = [ orderField ];
24
24
  const formOrderAction = {