Skip to content

Commit

Permalink
set templateSlug in page context on template set
Browse files Browse the repository at this point in the history
  • Loading branch information
ntsekouras committed Jan 13, 2021
1 parent 09e9bb0 commit c20bee8
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 35 deletions.
2 changes: 0 additions & 2 deletions packages/block-library/src/query-loop/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ export default function QueryLoopEdit( {
if ( sticky ) {
query.sticky = sticky === 'only';
}

// If `inherit` is truthy, adjust conditionally the query to create a better preview.
if ( inherit ) {
// Change the post-type if needed.
Expand All @@ -89,7 +88,6 @@ export default function QueryLoopEdit( {
postType = query.postType;
}
}

return {
posts: getEntityRecords( 'postType', postType, query ),
blocks: getBlocks( clientId ),
Expand Down
3 changes: 1 addition & 2 deletions packages/block-library/src/query/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@
"layout": "layout"
},
"usesContext": [
"postId",
"templateSlug"
"postId"
],
"supports": {
"html": false
Expand Down
10 changes: 2 additions & 8 deletions packages/block-library/src/query/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { DEFAULTS_POSTS_PER_PAGE } from '../constants';
const TEMPLATE = [ [ 'core/query-loop' ] ];
export function QueryContent( {
attributes,
context: { postId, templateSlug },
context: { postId },
setAttributes,
} ) {
const { queryId, query, layout } = attributes;
Expand All @@ -47,15 +47,10 @@ export function QueryContent( {
if ( ! query.perPage && postsPerPage ) {
newQuery.perPage = postsPerPage;
}
// Show and allow inherit Query options only when
// in site-editing context.
if ( ! templateSlug && query.inherit ) {
newQuery.inherit = false;
}
if ( !! Object.keys( newQuery ).length ) {
updateQuery( newQuery );
}
}, [ query.perPage, query.exclude, query.inherit, postId, templateSlug ] );
}, [ query.perPage, query.exclude, query.inherit, postId ] );
// We need this for multi-query block pagination.
// Query parameters for each block are scoped to their ID.
useEffect( () => {
Expand All @@ -73,7 +68,6 @@ export function QueryContent( {
attributes={ attributes }
setQuery={ updateQuery }
setLayout={ updateLayout }
allowInheritQuery={ !! templateSlug }
/>
<BlockControls>
<QueryToolbar
Expand Down
21 changes: 8 additions & 13 deletions packages/block-library/src/query/edit/query-inspector-controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ export default function QueryInspectorControls( {
attributes: { query, layout },
setQuery,
setLayout,
allowInheritQuery,
} ) {
const {
order,
Expand Down Expand Up @@ -171,18 +170,14 @@ export default function QueryInspectorControls( {
<InspectorControls>
<CreateNewPostLink type={ postType } />
<PanelBody title={ __( 'Settings' ) }>
{ allowInheritQuery && (
<ToggleControl
label={ __( 'Inherit query from URL' ) }
help={ __(
'Disable the option to customize the query arguments. Leave enabled to inherit the global query depending on the URL.'
) }
checked={ !! inherit }
onChange={ ( value ) =>
setQuery( { inherit: !! value } )
}
/>
) }
<ToggleControl
label={ __( 'Inherit query from URL' ) }
help={ __(
'Disable the option to customize the query arguments. Leave enabled to inherit the global query depending on the URL.'
) }
checked={ !! inherit }
onChange={ ( value ) => setQuery( { inherit: !! value } ) }
/>
{ ! inherit && (
<SelectControl
options={ postTypesSelectOptions }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default function TemplateNavigationItem( { item } ) {

const onActivateItem = () =>
'wp_template' === item.type
? setTemplate( item.id )
? setTemplate( item.id, item.slug )
: setTemplatePart( item.id );

return (
Expand Down
33 changes: 27 additions & 6 deletions packages/edit-site/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,25 @@ export function __experimentalSetPreviewDeviceType( deviceType ) {
* Returns an action object used to set a template.
*
* @param {number} templateId The template ID.
*
* @param {string} templateSlug The template slug.
* @return {Object} Action object.
*/
export function setTemplate( templateId ) {
export function* setTemplate( templateId, templateSlug ) {
const pageContext = { templateSlug };
if ( ! templateSlug ) {
const template = yield controls.resolveSelect(
'core',
'getEntityRecord',
'postType',
'wp_template',
templateId
);
pageContext.templateSlug = template?.slug;
}
return {
type: 'SET_TEMPLATE',
templateId,
page: { context: pageContext },
};
}

Expand All @@ -64,6 +76,7 @@ export function* addTemplate( template ) {
return {
type: 'SET_TEMPLATE',
templateId: newTemplate.id,
page: { context: { templateSlug: newTemplate.slug } },
};
}

Expand Down Expand Up @@ -124,17 +137,25 @@ export function* setPage( page ) {
if ( ! page.path && page.context?.postId ) {
page.path = `?p=${ page.context.postId }`;
}
const template = yield controls.resolveSelect(
const { id: templateId, slug: templateSlug } = yield controls.resolveSelect(
'core',
'__experimentalGetTemplateForLink',
page.path
);
yield {
type: 'SET_PAGE',
page,
templateId: template.id,
page: ! templateSlug
? page
: {
...page,
context: {
...page.context,
templateSlug,
},
},
templateId,
};
return template.id;
return templateId;
}

/**
Expand Down
26 changes: 23 additions & 3 deletions packages/edit-site/src/store/test/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,38 @@ describe( 'actions', () => {
} );

describe( 'setTemplate', () => {
it( 'should return the SET_TEMPLATE action', () => {
it( 'should return the SET_TEMPLATE action when slug is provided', () => {
const templateId = 1;
expect( setTemplate( templateId ) ).toEqual( {
const templateSlug = 'archive';
const it = setTemplate( templateId, templateSlug );
expect( it.next().value ).toEqual( {
type: 'SET_TEMPLATE',
templateId,
page: { context: { templateSlug } },
} );
} );
it( 'should return the SET_TEMPLATE by getting the template slug', () => {
const templateId = 1;
const template = { slug: 'index' };
const it = setTemplate( templateId );
expect( it.next().value ).toEqual( {
type: '@@data/RESOLVE_SELECT',
storeKey: 'core',
selectorName: 'getEntityRecord',
args: [ 'postType', 'wp_template', templateId ],
} );
expect( it.next( template ).value ).toEqual( {
type: 'SET_TEMPLATE',
templateId,
page: { context: { templateSlug: template.slug } },
} );
} );
} );

describe( 'addTemplate', () => {
it( 'should yield the DISPATCH control to create the template and return the SET_TEMPLATE action', () => {
const template = { slug: 'index' };
const newTemplate = { id: 1 };
const newTemplate = { id: 1, slug: 'index' };

const it = addTemplate( template );
expect( it.next().value ).toEqual( {
Expand All @@ -49,6 +68,7 @@ describe( 'actions', () => {
value: {
type: 'SET_TEMPLATE',
templateId: newTemplate.id,
page: { context: { templateSlug: newTemplate.slug } },
},
done: true,
} );
Expand Down

0 comments on commit c20bee8

Please sign in to comment.