Skip to content

Commit

Permalink
Move home page resolution to client
Browse files Browse the repository at this point in the history
  • Loading branch information
noahtallen committed Jul 7, 2020
1 parent 98d0f9c commit c3d21df
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 15 deletions.
3 changes: 3 additions & 0 deletions lib/edit-site-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ function gutenberg_edit_site_init( $hook ) {
}
$settings['styles'] = gutenberg_get_editor_styles();

$settings['showOnFront'] = get_option( 'show_on_front' );
$settings['pageOnFront'] = get_option( 'page_on_front' );

// This is so other parts of the code can hook their own settings.
// Example: Global Styles.
global $post;
Expand Down
10 changes: 9 additions & 1 deletion packages/edit-site/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,15 @@ const fetchLinkSuggestions = ( search, { perPage = 20 } = {} ) =>
export function initialize( id, settings ) {
settings.__experimentalFetchLinkSuggestions = fetchLinkSuggestions;

registerEditSiteStore( { settings } );
const initialState = {
settings,
home: {
showOnFront: settings.showOnFront,
pageOnFront: settings.pageOnFront,
},
};

registerEditSiteStore( initialState );

registerCoreBlocks();
if ( process.env.GUTENBERG_PHASE === 2 ) {
Expand Down
36 changes: 28 additions & 8 deletions packages/edit-site/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,19 +107,39 @@ export function setTemplatePart( templatePartId ) {
/**
* Resolves the template for a page and sets them.
*
* @param {Object} page The page object.
* @param {string} page.type The page type.
* @param {string} page.slug The page slug.
* @param {string} page.path The page path.
* @param {Object} page.context The page context.
* @param {Object} page The page object.
* @param {string} page.type The page type.
* @param {string} page.slug The page slug.
* @param {string} page.path The page path.
* @param {Object} page.context The page context.
* @param {number?} templateId An optional template ID for the page to avoid a second fetch if we already know it.
*
* @return {Object} Action object.
*/
export function* setPage( page ) {
const templateId = yield findTemplate( page.path );
export function* setPage( page, templateId ) {
const resolvedTemplate = templateId ?? ( yield findTemplate( page.path ) );
return {
type: 'SET_PAGE',
page,
templateId,
templateId: resolvedTemplate,
};
}

/**
* Sets up the initial template and page for edit site, and fetches other
* information it needs.
*
* @param {Object} initialPage The initial page to load in the site editor.
*/
export function* setupState( initialPage ) {
const homeTemplateId = yield findTemplate( '/' );
dispatch( {
type: 'SET_HOME_TEMPLATE',
homeTemplateId,
} );

yield setPage(
initialPage,
initialPage.path === '/' ? homeTemplateId : null
);
}
14 changes: 13 additions & 1 deletion packages/edit-site/src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,20 @@ export default function registerEditSiteStore( initialState ) {
initialState,
} );

const { showOnFront, pageOnFront } = initialState.home;
const initialPage = {
path: '/',
context:
showOnFront === 'page'
? {
postType: 'page',
postId: pageOnFront,
}
: {},
};

// We set the initial page here to include the template fetch which will
// resolve the correct homepage template.
store.dispatch( actions.setPage( initialState.page ) );
store.dispatch( actions.setupState( initialPage ) );
return store;
}
5 changes: 0 additions & 5 deletions packages/edit-site/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,6 @@ export function home( state = {}, action ) {
...state,
templateId: action.templateId,
};
case 'SET_SHOW_ON_FRONT':
return {
...state,
showOnFront: action.showOnFront,
};
}

return state;
Expand Down
11 changes: 11 additions & 0 deletions packages/edit-site/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,14 @@ export function getPage( state ) {
export function getShowOnFront( state ) {
return state.home.showOnFront;
}

/**
* Returns the site's current `page_on_front` setting.
*
* @param {Object} state Global application state.
*
* @return {number?} The setting.
*/
export function getPageOnFront( state ) {
return state.home.pageOnFront;
}

0 comments on commit c3d21df

Please sign in to comment.