From 419804d259ccb3d13e9f015bed2a07d586300a2f Mon Sep 17 00:00:00 2001 From: Jarda Snajdr Date: Tue, 30 Apr 2024 12:00:42 +0200 Subject: [PATCH] Site Editor: useLocation instead of window.location.href --- .../page-patterns/dataviews-pattern-actions.js | 17 ++++++++--------- .../src/components/page-patterns/index.js | 9 +++------ .../src/components/pattern-modal/duplicate.js | 7 ++++--- .../sidebar-navigation-screen-patterns/index.js | 11 +++++++---- 4 files changed, 22 insertions(+), 22 deletions(-) diff --git a/packages/edit-site/src/components/page-patterns/dataviews-pattern-actions.js b/packages/edit-site/src/components/page-patterns/dataviews-pattern-actions.js index 50baed5658e92..12a2b99e7dbcb 100644 --- a/packages/edit-site/src/components/page-patterns/dataviews-pattern-actions.js +++ b/packages/edit-site/src/components/page-patterns/dataviews-pattern-actions.js @@ -7,7 +7,6 @@ import { downloadZip } from 'client-zip'; /** * WordPress dependencies */ -import { getQueryArgs } from '@wordpress/url'; import { downloadBlob } from '@wordpress/blob'; import { __, _x, sprintf } from '@wordpress/i18n'; import { @@ -39,7 +38,7 @@ import { } from '../../utils/constants'; import { CreateTemplatePartModalContents } from '../create-template-part-modal'; -const { useHistory } = unlock( routerPrivateApis ); +const { useHistory, useLocation } = unlock( routerPrivateApis ); const { CreatePatternModalContents, useDuplicatePatternProps } = unlock( patternsPrivateApis ); @@ -365,9 +364,9 @@ export const duplicatePatternAction = { modalHeader: _x( 'Duplicate pattern', 'action label' ), RenderModal: ( { items, closeModal } ) => { const [ item ] = items; - const { categoryId = PATTERN_DEFAULT_CATEGORY } = getQueryArgs( - window.location.href - ); + const { + params: { categoryId = PATTERN_DEFAULT_CATEGORY }, + } = useLocation(); const isThemePattern = item.type === PATTERN_TYPES.theme; const history = useHistory(); function onPatternSuccess( { pattern } ) { @@ -401,11 +400,11 @@ export const duplicateTemplatePartAction = { RenderModal: ( { items, closeModal } ) => { const [ item ] = items; const { createSuccessNotice } = useDispatch( noticesStore ); - const { categoryId = PATTERN_DEFAULT_CATEGORY } = getQueryArgs( - window.location.href - ); + const { + params: { categoryId = PATTERN_DEFAULT_CATEGORY }, + } = useLocation(); const history = useHistory(); - async function onTemplatePartSuccess( templatePart ) { + function onTemplatePartSuccess( templatePart ) { createSuccessNotice( sprintf( // translators: %s: The new template part's title e.g. 'Call to action (copy)'. diff --git a/packages/edit-site/src/components/page-patterns/index.js b/packages/edit-site/src/components/page-patterns/index.js index 1a649a0ef0542..ea313e7e1dede 100644 --- a/packages/edit-site/src/components/page-patterns/index.js +++ b/packages/edit-site/src/components/page-patterns/index.js @@ -12,7 +12,6 @@ import { Tooltip, Flex, } from '@wordpress/components'; -import { getQueryArgs } from '@wordpress/url'; import { __, _x } from '@wordpress/i18n'; import { useState, @@ -67,7 +66,7 @@ const { ExperimentalBlockEditorProvider, useGlobalStyle } = unlock( blockEditorPrivateApis ); const { usePostActions } = unlock( editorPrivateApis ); -const { useHistory } = unlock( routerPrivateApis ); +const { useHistory, useLocation } = unlock( routerPrivateApis ); const EMPTY_ARRAY = []; const defaultConfigPerViewType = { @@ -253,10 +252,8 @@ function Title( { item, categoryId } ) { export default function DataviewsPatterns() { const { - categoryType, - categoryId: categoryIdFromURL, - path, - } = getQueryArgs( window.location.href ); + params: { categoryType, categoryId: categoryIdFromURL, path }, + } = useLocation(); const type = categoryType || ( path === '/wp_template_part/all' diff --git a/packages/edit-site/src/components/pattern-modal/duplicate.js b/packages/edit-site/src/components/pattern-modal/duplicate.js index e59decbf8b260..18cef4a613380 100644 --- a/packages/edit-site/src/components/pattern-modal/duplicate.js +++ b/packages/edit-site/src/components/pattern-modal/duplicate.js @@ -4,7 +4,6 @@ import { useDispatch, useSelect } from '@wordpress/data'; import { privateApis as patternsPrivateApis } from '@wordpress/patterns'; import { privateApis as routerPrivateApis } from '@wordpress/router'; -import { getQueryArgs } from '@wordpress/url'; import { privateApis as editorPrivateApis } from '@wordpress/editor'; /** @@ -16,12 +15,14 @@ import { unlock } from '../../lock-unlock'; import useEditedEntityRecord from '../use-edited-entity-record'; const { DuplicatePatternModal } = unlock( patternsPrivateApis ); -const { useHistory } = unlock( routerPrivateApis ); +const { useHistory, useLocation } = unlock( routerPrivateApis ); const { interfaceStore } = unlock( editorPrivateApis ); export default function PatternDuplicateModal() { const { record } = useEditedEntityRecord(); - const { categoryType, categoryId } = getQueryArgs( window.location.href ); + const { + params: { categoryType, categoryId }, + } = useLocation(); const { closeModal } = useDispatch( interfaceStore ); const history = useHistory(); diff --git a/packages/edit-site/src/components/sidebar-navigation-screen-patterns/index.js b/packages/edit-site/src/components/sidebar-navigation-screen-patterns/index.js index d795bfb9a893a..5952c53e2ba54 100644 --- a/packages/edit-site/src/components/sidebar-navigation-screen-patterns/index.js +++ b/packages/edit-site/src/components/sidebar-navigation-screen-patterns/index.js @@ -7,10 +7,10 @@ import { } from '@wordpress/components'; import { getTemplatePartIcon } from '@wordpress/editor'; import { __ } from '@wordpress/i18n'; -import { getQueryArgs } from '@wordpress/url'; import { store as coreStore } from '@wordpress/core-data'; import { useSelect } from '@wordpress/data'; import { file } from '@wordpress/icons'; +import { privateApis as routerPrivateApis } from '@wordpress/router'; /** * Internal dependencies @@ -26,6 +26,9 @@ import { } from '../../utils/constants'; import usePatternCategories from './use-pattern-categories'; import useTemplatePartAreas from './use-template-part-areas'; +import { unlock } from '../../lock-unlock'; + +const { useLocation } = unlock( routerPrivateApis ); function CategoriesGroup( { path, @@ -109,9 +112,9 @@ function CategoriesGroup( { const EMPTY_ARRAY = []; export default function SidebarNavigationScreenPatterns() { - const { categoryType, categoryId, path } = getQueryArgs( - window.location.href - ); + const { + params: { categoryType, categoryId, path }, + } = useLocation(); const isTemplatePartsPath = path === '/wp_template_part/all'; const currentCategory = categoryId ||