-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Site Editor - add query args for current context. (#27124)
* Use query args for routing * Use @wordpress/url instead * Use new URL format * Remove uneeded changes * change name of component * Rename PostRouter component function Co-authored-by: Noah Allen <[email protected]>
- Loading branch information
1 parent
2293b3b
commit 7aff63d
Showing
5 changed files
with
94 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
packages/edit-site/src/components/url-query-controller/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useEffect } from '@wordpress/element'; | ||
import { useDispatch, useSelect } from '@wordpress/data'; | ||
import { getQueryArg, addQueryArgs, removeQueryArgs } from '@wordpress/url'; | ||
|
||
export default function URLQueryController() { | ||
const { setTemplate, setTemplatePart, showHomepage, setPage } = useDispatch( | ||
'core/edit-site' | ||
); | ||
|
||
// Set correct entity on load. | ||
useEffect( () => { | ||
const url = window.location.href; | ||
const postId = getQueryArg( url, 'postId' ); | ||
|
||
if ( ! postId ) { | ||
showHomepage(); | ||
return; | ||
} | ||
|
||
const postType = getQueryArg( url, 'postType' ); | ||
if ( 'page' === postType || 'post' === postType ) { | ||
setPage( { context: { postType, postId } } ); // Resolves correct template based on ID. | ||
} else if ( 'wp_template' === postType ) { | ||
setTemplate( postId ); | ||
} else if ( 'wp_template_part' === postType ) { | ||
setTemplatePart( postId ); | ||
} else { | ||
showHomepage(); | ||
} | ||
}, [] ); | ||
|
||
// Update page URL when context changes. | ||
const pageContext = useCurrentPageContext(); | ||
useEffect( () => { | ||
const newUrl = pageContext | ||
? addQueryArgs( window.location.href, pageContext ) | ||
: removeQueryArgs( window.location.href, 'postType', 'postId' ); | ||
|
||
window.history.replaceState( {}, '', newUrl ); | ||
}, [ pageContext ] ); | ||
|
||
return null; | ||
} | ||
|
||
function useCurrentPageContext() { | ||
return useSelect( ( select ) => { | ||
const { | ||
getTemplateId, | ||
getTemplatePartId, | ||
getTemplateType, | ||
getPage, | ||
} = select( 'core/edit-site' ); | ||
|
||
const page = getPage(); | ||
const templateType = getTemplateType(); | ||
const templateId = getTemplateId(); | ||
const templatePartId = getTemplatePartId(); | ||
|
||
let _postId, _postType; | ||
if ( page?.context?.postId && page?.context?.postType ) { | ||
_postId = page.context.postId; | ||
_postType = page.context.postType; | ||
} else if ( templateType === 'wp_template' && templateId ) { | ||
_postId = templateId; | ||
_postType = templateType; | ||
} else if ( templateType === 'wp_template_part' && templatePartId ) { | ||
_postId = templatePartId; | ||
_postType = templateType; | ||
} | ||
|
||
if ( _postId && _postType ) { | ||
return { postId: _postId, postType: _postType }; | ||
} | ||
|
||
return null; | ||
} ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters