Skip to content

Commit 76842c4

Browse files
ellatrixmcsf
authored andcommitted
Template activation: redirect to 'Create Templates' after duplication (#72727)
Co-authored-by: ellatrix <[email protected]> Co-authored-by: mcsf <[email protected]>
1 parent 1346ddb commit 76842c4

File tree

4 files changed

+44
-8
lines changed

4 files changed

+44
-8
lines changed

packages/edit-site/src/components/page-templates/index.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ import {
3939
} from './fields';
4040
import { defaultLayouts, getDefaultView } from './view-utils';
4141

42-
const { usePostActions, templateTitleField } = unlock( editorPrivateApis );
42+
const { usePostActions, usePostFields, templateTitleField } =
43+
unlock( editorPrivateApis );
4344
const { useHistory, useLocation } = unlock( routerPrivateApis );
4445
const { useEntityRecordsWithPermissions } = unlock( corePrivateApis );
4546

@@ -210,6 +211,10 @@ export default function PageTemplates() {
210211
[ history, path, view?.type ]
211212
);
212213

214+
const postTypeFields = usePostFields( {
215+
postType: TEMPLATE_POST_TYPE,
216+
} );
217+
const dateField = postTypeFields.find( ( field ) => field.id === 'date' );
213218
const themeField = useThemeField();
214219
const fields = useMemo( () => {
215220
const _fields = [
@@ -221,6 +226,9 @@ export default function PageTemplates() {
221226
];
222227
if ( activeView === 'user' ) {
223228
_fields.push( themeField );
229+
if ( dateField ) {
230+
_fields.push( dateField );
231+
}
224232
}
225233
const elements = [];
226234
for ( const author in users ) {
@@ -234,7 +242,7 @@ export default function PageTemplates() {
234242
elements,
235243
} );
236244
return _fields;
237-
}, [ users, activeView, themeField ] );
245+
}, [ users, activeView, themeField, dateField ] );
238246

239247
const { data, paginationInfo } = useMemo( () => {
240248
return filterSortAndPaginate( records, view, fields );
@@ -251,6 +259,7 @@ export default function PageTemplates() {
251259
typeof newItem.title === 'string'
252260
? newItem.title
253261
: newItem.title?.rendered;
262+
history.navigate( `/template?activeView=user` );
254263
createSuccessNotice(
255264
sprintf(
256265
// translators: %s: Title of the created post or template, e.g: "Hello world".
@@ -358,11 +367,6 @@ export default function PageTemplates() {
358367
<duplicateAction.RenderModal
359368
items={ [ selectedRegisteredTemplate ] }
360369
closeModal={ () => setSelectedRegisteredTemplate() }
361-
onActionPerformed={ ( [ item ] ) => {
362-
history.navigate(
363-
`/${ item.type }/${ item.id }?canvas=edit`
364-
);
365-
} }
366370
/>
367371
</Modal>
368372
) }

packages/edit-site/src/components/page-templates/view-utils.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ const DEFAULT_VIEW = {
2828
export function getDefaultView( activeView ) {
2929
return {
3030
...DEFAULT_VIEW,
31+
sort:
32+
activeView === 'user'
33+
? {
34+
field: 'date',
35+
direction: 'desc',
36+
}
37+
: DEFAULT_VIEW.sort,
3138
filters: ! [ 'active', 'user' ].includes( activeView )
3239
? [
3340
{

test/e2e/specs/site-editor/browser-history.spec.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,29 @@ test.describe( 'Site editor browser history', () => {
2323
.locator( '.fields-field__title', { hasText: 'Index' } )
2424
.click();
2525
await page.getByRole( 'button', { name: 'Duplicate' } ).click();
26+
await expect( page ).toHaveURL(
27+
'/wp-admin/site-editor.php?p=%2Ftemplate&activeView=user'
28+
);
29+
await page
30+
.getByRole( 'button', { name: 'Index (Copy)' } )
31+
.first()
32+
.click();
2633
await expect( page ).toHaveURL(
2734
/\/wp-admin\/site-editor\.php\?p=%2Fwp_template%2F\d+&canvas=edit$/
2835
);
2936

3037
// Navigate back to the template list
3138
await page.goBack();
39+
await expect( page ).toHaveURL(
40+
'/wp-admin/site-editor.php?p=%2Ftemplate&activeView=user'
41+
);
42+
await page.goBack();
3243
await expect( page ).toHaveURL(
3344
'/wp-admin/site-editor.php?p=%2Ftemplate'
3445
);
35-
3646
// Navigate back to the dashboard
3747
await page.goBack();
48+
await expect( page ).toHaveURL( '/wp-admin/site-editor.php' );
3849
await page.goBack();
3950
await expect( page ).toHaveURL( '/wp-admin/index.php' );
4051
} );

test/e2e/specs/site-editor/template-registration.spec.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ test.describe( 'Block template registration', () => {
5454
// Verify the template contents are rendered in the editor.
5555
await page.getByText( 'Plugin Template' ).click();
5656
await page.getByRole( 'button', { name: 'Duplicate' } ).click();
57+
await page.waitForURL(
58+
'/wp-admin/site-editor.php?p=%2Ftemplate&activeView=user'
59+
);
60+
await page
61+
.getByRole( 'button', { name: 'Plugin Template (Copy)' } )
62+
.first()
63+
.click();
5764
await expect(
5865
editor.canvas.getByText( 'This is a plugin-registered template.' )
5966
).toBeVisible();
@@ -196,6 +203,13 @@ test.describe( 'Block template registration', () => {
196203
);
197204
await page.getByText( 'Plugin Template' ).click();
198205
await page.getByRole( 'button', { name: 'Duplicate' } ).click();
206+
await page.waitForURL(
207+
'/wp-admin/site-editor.php?p=%2Ftemplate&activeView=user'
208+
);
209+
await page
210+
.getByRole( 'button', { name: 'Plugin Template (Copy)' } )
211+
.first()
212+
.click();
199213
await expect(
200214
editor.canvas.getByText( 'This is a plugin-registered template.' )
201215
).toBeVisible();

0 commit comments

Comments
 (0)