Skip to content

Commit

Permalink
Add defaultPage attribute to Comments Query Loop
Browse files Browse the repository at this point in the history
  • Loading branch information
DAreRodz committed Jan 31, 2022
1 parent aa4cd79 commit 39a9318
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 9 deletions.
2 changes: 1 addition & 1 deletion docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ An advanced block that allows displaying post comments based on different query
- **Name:** core/comments-query-loop
- **Category:** theme
- **Supports:** align (full, wide), color (background, gradients, link, text), ~~html~~
- **Attributes:** inherit, order, perPage, tagName
- **Attributes:** defaultPage, inherit, order, perPage, tagName

## Cover

Expand Down
7 changes: 6 additions & 1 deletion packages/block-library/src/comment-template/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
"parent": [ "core/comments-query-loop" ],
"description": "Contains the block elements used to render a comment, like the title, date, author, avatar and more.",
"textdomain": "default",
"usesContext": [ "comments/perPage", "postId", "comments/order" ],
"usesContext": [
"comments/defaultPage",
"comments/order",
"comments/perPage",
"postId"
],
"supports": {
"reusable": false,
"html": false,
Expand Down
5 changes: 5 additions & 0 deletions packages/block-library/src/comments-query-loop/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,16 @@
"tagName": {
"type": "string",
"default": "div"
},
"defaultPage": {
"type": "string",
"default": "oldest"
}
},
"providesContext": {
"comments/perPage": "perPage",
"comments/order": "order",
"comments/defaultPage": "defaultPage",
"comments/inherit": "inherit"
},
"supports": {
Expand Down
13 changes: 8 additions & 5 deletions packages/block-library/src/comments-query-loop/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ export default function CommentsQueryLoopEdit( { attributes, setAttributes } ) {
template: TEMPLATE,
} );

const { commentOrder, commentsPerPage } = useSelect( ( select ) => {
const { getSettings } = select( blockEditorStore );
const { __experimentalDiscussionSettings } = getSettings();
return __experimentalDiscussionSettings;
} );
const { commentOrder, commentsPerPage, defaultPage } = useSelect(
( select ) => {
const { getSettings } = select( blockEditorStore );
const { __experimentalDiscussionSettings } = getSettings();
return __experimentalDiscussionSettings;
}
);

return (
<>
Expand All @@ -40,6 +42,7 @@ export default function CommentsQueryLoopEdit( { attributes, setAttributes } ) {
defaultSettings={ {
defaultOrder: commentOrder,
defaultPerPage: commentsPerPage,
defaultDefaultPage: defaultPage,
} }
/>
<TagName { ...innerBlocksProps } />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,21 @@ const orderOptions = [
},
];

const defaultPageOptions = [
{
label: __( 'Newest' ),
value: 'newest',
},
{
label: __( 'Oldest' ),
value: 'oldest',
},
];

export default function CommentsInspectorControls( {
attributes: { TagName, perPage, order, inherit },
attributes: { TagName, perPage, order, inherit, defaultPage },
setAttributes,
defaultSettings: { defaultPerPage, defaultOrder },
defaultSettings: { defaultPerPage, defaultOrder, defaultDefaultPage },
} ) {
return (
<InspectorControls>
Expand All @@ -37,6 +48,7 @@ export default function CommentsInspectorControls( {
inherit: ! inherit,
order: inherit ? defaultOrder : null,
perPage: inherit ? defaultPerPage : null,
defaultPage: inherit ? defaultDefaultPage : null,
} );
} }
/>
Expand All @@ -52,6 +64,16 @@ export default function CommentsInspectorControls( {
} );
} }
/>
<SelectControl
label={ __( 'Default page' ) }
value={ defaultPage }
options={ defaultPageOptions }
onChange={ ( value ) => {
setAttributes( {
defaultPage: value,
} );
} }
/>
<NumberControl
__unstableInputWidth="60px"
label={ __( 'Items per Page' ) }
Expand Down

0 comments on commit 39a9318

Please sign in to comment.