Skip to content

Commit

Permalink
Pass siteUrl in as a param
Browse files Browse the repository at this point in the history
  • Loading branch information
talldan committed Dec 13, 2021
1 parent 8230866 commit 76d8810
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@
* WordPress dependencies
*/
import apiFetch from '@wordpress/api-fetch';
import { select } from '@wordpress/data';
import { addQueryArgs } from '@wordpress/url';

/**
* Internal dependencies
*/
import history from '../../utils/history';
import { store as editSiteStore } from '../../store';
import getIsListPage from '../../utils/get-is-list-page';

function getNeedsHomepageRedirect( params ) {
Expand All @@ -22,7 +20,7 @@ function getNeedsHomepageRedirect( params ) {
);
}

async function getHomepageParams() {
async function getHomepageParams( siteUrl ) {
const siteSettings = await apiFetch( { path: '/wp/v2/settings' } );
if ( ! siteSettings ) {
return;
Expand All @@ -44,7 +42,6 @@ async function getHomepageParams() {
// Else get the home template.
// This matches the logic in `__experimentalGetTemplateForLink`.
// (packages/core-data/src/resolvers.js)
const { siteUrl } = select( editSiteStore ).getSettings();
const template = await window
.fetch( addQueryArgs( siteUrl, { '_wp-find-template': true } ) )
.then( ( res ) => res.json() )
Expand All @@ -60,12 +57,12 @@ async function getHomepageParams() {
};
}

export default async function redirectToHomepage() {
export default async function redirectToHomepage( siteUrl ) {
const searchParams = new URLSearchParams( history.location.search );
const params = Object.fromEntries( searchParams.entries() );

if ( getNeedsHomepageRedirect( params ) ) {
const homepageParams = await getHomepageParams();
const homepageParams = await getHomepageParams( siteUrl );

if ( homepageParams ) {
history.replace( homepageParams );
Expand Down
7 changes: 4 additions & 3 deletions packages/edit-site/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@ export async function reinitializeEditor( target, settings ) {
// The site editor relies on `postType` and `postId` params in the URL to
// define what's being edited. When visiting via the dashboard link, these
// won't be present. Do a client side redirect to the 'homepage' if that's
// the case. This requires editor settings, so dispatch that first.
dispatch( editSiteStore ).updateSettings( settings );
await redirectToHomepage();
// the case.
await redirectToHomepage( settings.siteUrl );

// This will be a no-op if the target doesn't have any React nodes.
unmountComponentAtNode( target );
Expand All @@ -49,6 +48,8 @@ export async function reinitializeEditor( target, settings ) {
// We dispatch actions and update the store synchronously before rendering
// so that we won't trigger unnecessary re-renders with useEffect.
{
dispatch( editSiteStore ).updateSettings( settings );

// Keep the defaultTemplateTypes in the core/editor settings too,
// so that they can be selected with core/editor selectors in any editor.
// This is needed because edit-site doesn't initialize with EditorProvider,
Expand Down

0 comments on commit 76d8810

Please sign in to comment.