Skip to content

Commit 53b643c

Browse files
Mamadukat-hamanoR1shabh-Gupta
authored
BrowserURL: Refactor to a function component and use hooks (#71035)
Co-authored-by: Mamaduka <mamaduka@git.wordpress.org> Co-authored-by: t-hamano <wildworks@git.wordpress.org> Co-authored-by: R1shabh-Gupta <rishabhwp@git.wordpress.org>
1 parent 6778edd commit 53b643c

File tree

2 files changed

+74
-74
lines changed

2 files changed

+74
-74
lines changed
Lines changed: 29 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/**
22
* WordPress dependencies
33
*/
4-
import { Component } from '@wordpress/element';
5-
import { withSelect } from '@wordpress/data';
4+
import { useEffect, useState } from '@wordpress/element';
5+
import { useSelect } from '@wordpress/data';
66
import { addQueryArgs } from '@wordpress/url';
77
import { store as editorStore } from '@wordpress/editor';
88

@@ -17,65 +17,35 @@ export function getPostEditURL( postId ) {
1717
return addQueryArgs( 'post.php', { post: postId, action: 'edit' } );
1818
}
1919

20-
export class BrowserURL extends Component {
21-
constructor() {
22-
super( ...arguments );
20+
export default function BrowserURL() {
21+
const [ historyId, setHistoryId ] = useState( null );
22+
const { postId, postStatus } = useSelect( ( select ) => {
23+
const { getCurrentPost } = select( editorStore );
24+
const post = getCurrentPost();
25+
let { id, status, type } = post;
26+
const isTemplate = [ 'wp_template', 'wp_template_part' ].includes(
27+
type
28+
);
29+
if ( isTemplate ) {
30+
id = post.wp_id;
31+
}
2332

24-
this.state = {
25-
historyId: null,
33+
return {
34+
postId: id,
35+
postStatus: status,
2636
};
27-
}
28-
29-
componentDidUpdate( prevProps ) {
30-
const { postId, postStatus } = this.props;
31-
const { historyId } = this.state;
32-
33-
if (
34-
( postId !== prevProps.postId || postId !== historyId ) &&
35-
postStatus !== 'auto-draft' &&
36-
postId
37-
) {
38-
this.setBrowserURL( postId );
37+
}, [] );
38+
39+
useEffect( () => {
40+
if ( postId && postId !== historyId && postStatus !== 'auto-draft' ) {
41+
window.history.replaceState(
42+
{ id: postId },
43+
'Post ' + postId,
44+
getPostEditURL( postId )
45+
);
46+
setHistoryId( postId );
3947
}
40-
}
48+
}, [ postId, postStatus, historyId ] );
4149

42-
/**
43-
* Replaces the browser URL with a post editor link for the given post ID.
44-
*
45-
* Note it is important that, since this function may be called when the
46-
* editor first loads, the result generated `getPostEditURL` matches that
47-
* produced by the server. Otherwise, the URL will change unexpectedly.
48-
*
49-
* @param {number} postId Post ID for which to generate post editor URL.
50-
*/
51-
setBrowserURL( postId ) {
52-
window.history.replaceState(
53-
{ id: postId },
54-
'Post ' + postId,
55-
getPostEditURL( postId )
56-
);
57-
58-
this.setState( () => ( {
59-
historyId: postId,
60-
} ) );
61-
}
62-
63-
render() {
64-
return null;
65-
}
50+
return null;
6651
}
67-
68-
export default withSelect( ( select ) => {
69-
const { getCurrentPost } = select( editorStore );
70-
const post = getCurrentPost();
71-
let { id, status, type } = post;
72-
const isTemplate = [ 'wp_template', 'wp_template_part' ].includes( type );
73-
if ( isTemplate ) {
74-
id = post.wp_id;
75-
}
76-
77-
return {
78-
postId: id,
79-
postStatus: status,
80-
};
81-
} )( BrowserURL );

packages/edit-post/src/components/browser-url/test/index.js

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,26 @@
33
*/
44
import { render } from '@testing-library/react';
55

6+
/**
7+
* WordPress dependencies
8+
*/
9+
import { useSelect } from '@wordpress/data';
10+
611
/**
712
* Internal dependencies
813
*/
9-
import { getPostEditURL, BrowserURL } from '../';
14+
import { default as BrowserURL, getPostEditURL } from '../';
15+
16+
jest.mock( '@wordpress/data/src/components/use-select', () => jest.fn() );
17+
18+
function setupUseSelectMock( { postId, postStatus } ) {
19+
useSelect.mockImplementation( () => {
20+
return {
21+
postId,
22+
postStatus,
23+
};
24+
} );
25+
}
1026

1127
describe( 'getPostEditURL', () => {
1228
it( 'should generate relative path with post and action arguments', () => {
@@ -32,20 +48,28 @@ describe( 'BrowserURL', () => {
3248
} );
3349

3450
it( 'not update URL if post is auto-draft', () => {
35-
const { rerender } = render( <BrowserURL /> );
36-
37-
rerender( <BrowserURL postId={ 1 } postStatus="auto-draft" /> );
51+
setupUseSelectMock( {
52+
postId: 1,
53+
postStatus: 'auto-draft',
54+
} );
3855

56+
render( <BrowserURL /> );
3957
expect( replaceStateSpy ).not.toHaveBeenCalled();
4058
} );
4159

4260
it( 'update URL if post is no longer auto-draft', () => {
61+
setupUseSelectMock( {
62+
postId: 1,
63+
postStatus: 'auto-draft',
64+
} );
4365
const { rerender } = render( <BrowserURL /> );
4466

45-
rerender( <BrowserURL postId={ 1 } postStatus="auto-draft" /> );
46-
47-
rerender( <BrowserURL postId={ 1 } postStatus="draft" /> );
67+
setupUseSelectMock( {
68+
postId: 1,
69+
postStatus: 'draft',
70+
} );
4871

72+
rerender( <BrowserURL /> );
4973
expect( replaceStateSpy ).toHaveBeenCalledWith(
5074
{ id: 1 },
5175
'Post 1',
@@ -54,26 +78,32 @@ describe( 'BrowserURL', () => {
5478
} );
5579

5680
it( 'not update URL if history is already set', () => {
81+
setupUseSelectMock( {
82+
postId: 1,
83+
postStatus: 'draft',
84+
} );
5785
const { rerender } = render( <BrowserURL /> );
5886

59-
rerender( <BrowserURL postId={ 1 } postStatus="draft" /> );
60-
6187
replaceStateSpy.mockReset();
6288

63-
rerender( <BrowserURL postId={ 1 } postStatus="draft" /> );
64-
89+
rerender( <BrowserURL /> );
6590
expect( replaceStateSpy ).not.toHaveBeenCalled();
6691
} );
6792

6893
it( 'update URL if post ID changes', () => {
94+
setupUseSelectMock( {
95+
postId: 1,
96+
postStatus: 'draft',
97+
} );
6998
const { rerender } = render( <BrowserURL /> );
7099

71-
rerender( <BrowserURL postId={ 1 } postStatus="draft" /> );
72-
100+
setupUseSelectMock( {
101+
postId: 2,
102+
postStatus: 'draft',
103+
} );
73104
replaceStateSpy.mockReset();
74105

75-
rerender( <BrowserURL postId={ 2 } postStatus="draft" /> );
76-
106+
rerender( <BrowserURL /> );
77107
expect( replaceStateSpy ).toHaveBeenCalledWith(
78108
{ id: 2 },
79109
'Post 2',

0 commit comments

Comments
 (0)