Skip to content

Commit

Permalink
Use findTemplate() util
Browse files Browse the repository at this point in the history
  • Loading branch information
ockham committed Jun 4, 2020
1 parent 4b05b62 commit 59d0688
Showing 1 changed file with 11 additions and 33 deletions.
44 changes: 11 additions & 33 deletions packages/edit-site/src/components/template-switcher/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { addQueryArgs } from '@wordpress/url';
import { resolveSelect, useSelect } from '@wordpress/data';
import { useRegistry, useSelect } from '@wordpress/data';
import { useEffect, useState } from '@wordpress/element';
import {
Tooltip,
Expand All @@ -17,14 +16,10 @@ import { Icon, home, plus, undo } from '@wordpress/icons';
/**
* Internal dependencies
*/
import { findTemplate } from '../../utils';
import TemplatePreview from './template-preview';
import ThemePreview from './theme-preview';

/**
* Browser dependencies
*/
const { fetch } = window;

const TEMPLATE_OVERRIDES = {
page: ( slug ) => `page-${ slug }`,
category: ( slug ) => `category-${ slug }`,
Expand Down Expand Up @@ -74,35 +69,18 @@ export default function TemplateSwitcher( {
setThemePreviewVisible( () => false );
};

const registry = useRegistry();
const [ homeId, setHomeId ] = useState();

useEffect( () => {
const effect = async () => {
try {
const { success, data } = await fetch(
addQueryArgs( '/', { '_wp-find-template': true } )
).then( ( res ) => res.json() );
if ( success ) {
let newHomeId = data.ID;
if ( newHomeId === null ) {
const { getEntityRecords } = resolveSelect( 'core' );
newHomeId = (
await getEntityRecords( 'postType', 'wp_template', {
resolved: true,
slug: data.post_name,
} )
)[ 0 ].id;
}
setHomeId( newHomeId );
} else {
throw new Error();
}
} catch ( err ) {
setHomeId( null );
}
};
effect();
}, [] );
findTemplate(
'/',
registry.__experimentalResolveSelect( 'core' ).getEntityRecords
).then(
( newHomeId ) => setHomeId( newHomeId ),
() => setHomeId( null )
);
}, [ registry ] );

const { currentTheme, template, templateParts } = useSelect(
( _select ) => {
Expand Down

0 comments on commit 59d0688

Please sign in to comment.