Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use tanstack and migrate templates and parts #54006

Draft
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/edit-site/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"react-native": "src/index",
"dependencies": {
"@babel/runtime": "^7.16.0",
"@tanstack/react-table": "^8.9.3",
"@wordpress/a11y": "file:../a11y",
"@wordpress/api-fetch": "file:../api-fetch",
"@wordpress/block-editor": "file:../block-editor",
Expand Down
91 changes: 54 additions & 37 deletions packages/edit-site/src/components/page-template-parts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,26 @@ import AddedBy from '../list/added-by';
import TemplateActions from '../template-actions';
import AddNewTemplatePart from './add-new-template-part';

export default function PageTemplateParts() {
const { records: templateParts } = useEntityRecords(
'postType',
'wp_template_part',
{
per_page: -1,
}
);
/**
* @typedef {Object} RichContent
* @property {string} rendered The rendered content.
* @property {string} raw The raw content.
*
* @typedef {Object} TemplatePart
* @property {string} id The ID of the template.
* @property {'wp_template_part'} type The type of the template.
* @property {RichContent} title The name of the template.
* @property {string} description The description of the template.
* @property {string} slug The slug of the template.
*/

const columns = [
{
header: __( 'Template Part' ),
cell: ( templatePart ) => (
const columns = [
{
accessor: ( row ) => decodeEntities( row.title?.rendered || row.slug ),
id: 'title',
header: __( 'Template Part' ),
cell: ( { value, row: templatePart } ) => {
return (
<VStack>
<Heading as="h3" level={ 5 }>
<Link
Expand All @@ -42,43 +49,53 @@ export default function PageTemplateParts() {
} }
state={ { backPath: '/wp_template_part/all' } }
>
{ decodeEntities(
templatePart.title?.rendered ||
templatePart.slug
) }
{ value }
</Link>
</Heading>
</VStack>
),
maxWidth: 400,
},
{
header: __( 'Added by' ),
cell: ( templatePart ) => (
<AddedBy
postType={ templatePart.type }
postId={ templatePart.id }
/>
),
);
},
maxWidth: 400,
},
{
id: 'added-by',
header: __( 'Added by' ),
cell: ( { row: templatePart } ) => (
<AddedBy
postType={ templatePart.type }
postId={ templatePart.id }
/>
),
},
{
id: 'actions',
header: <VisuallyHidden>{ __( 'Actions' ) }</VisuallyHidden>,
cell: ( { row: templatePart } ) => (
<TemplateActions
postType={ templatePart.type }
postId={ templatePart.id }
/>
),
},
];

export default function PageTemplateParts() {
/** @type {{records: TemplatePart[] | null}} */
const { records: templateParts } = useEntityRecords(
'postType',
'wp_template_part',
{
header: <VisuallyHidden>{ __( 'Actions' ) }</VisuallyHidden>,
cell: ( templatePart ) => (
<TemplateActions
postType={ templatePart.type }
postId={ templatePart.id }
/>
),
},
];
per_page: -1,
}
);

return (
<Page
title={ __( 'Template Parts' ) }
actions={ <AddNewTemplatePart /> }
>
{ templateParts && (
<Table data={ templateParts } columns={ columns } />
<Table data={ templateParts } columns={ columns } rowId="id" />
) }
</Page>
);
Expand Down
91 changes: 58 additions & 33 deletions packages/edit-site/src/components/page-templates/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,26 @@ import AddedBy from '../list/added-by';
import TemplateActions from '../template-actions';
import AddNewTemplate from '../add-new-template';

export default function PageTemplates() {
const { records: templates } = useEntityRecords(
'postType',
'wp_template',
{
per_page: -1,
}
);
/**
* @typedef {Object} RichContent
* @property {string} rendered The rendered content.
* @property {string} raw The raw content.
*
* @typedef {Object} Template
* @property {string} id The ID of the template.
* @property {'wp_template'} type The type of the template.
* @property {RichContent} title The name of the template.
* @property {string} description The description of the template.
* @property {string} slug The slug of the template.
*/

const columns = [
{
header: __( 'Template' ),
cell: ( template ) => (
const columns = [
{
accessor: ( row ) => decodeEntities( row.title?.rendered || row.slug ),
id: 'title',
header: __( 'Template' ),
cell: ( { value, row: template } ) => {
return (
<VStack>
<Heading as="h3" level={ 5 }>
<Link
Expand All @@ -43,9 +50,7 @@ export default function PageTemplates() {
canvas: 'edit',
} }
>
{ decodeEntities(
template.title?.rendered || template.slug
) }
{ value }
</Link>
</Heading>
{ template.description && (
Expand All @@ -54,25 +59,38 @@ export default function PageTemplates() {
</Text>
) }
</VStack>
),
maxWidth: 400,
},
{
header: __( 'Added by' ),
cell: ( template ) => (
<AddedBy postType={ template.type } postId={ template.id } />
),
);
},
maxWidth: 400,
},
{
id: 'added-by',
header: __( 'Added by' ),
cell: ( { row: template } ) => (
<AddedBy postType={ template.type } postId={ template.id } />
),
},
{
id: 'actions',
header: <VisuallyHidden>{ __( 'Actions' ) }</VisuallyHidden>,
cell: ( { row: template } ) => (
<TemplateActions
postType={ template.type }
postId={ template.id }
/>
),
},
];

export default function PageTemplates() {
/** @type {{records: Template[] | null}} */
const { records: templates } = useEntityRecords(
'postType',
'wp_template',
{
header: <VisuallyHidden>{ __( 'Actions' ) }</VisuallyHidden>,
cell: ( template ) => (
<TemplateActions
postType={ template.type }
postId={ template.id }
/>
),
},
];
per_page: -1,
}
);

return (
<Page
Expand All @@ -85,7 +103,14 @@ export default function PageTemplates() {
/>
}
>
{ templates && <Table data={ templates } columns={ columns } /> }
{ templates && (
<Table
data={ templates }
columns={ columns }
rowId="id"
enableSorting={ false }
/>
) }
</Page>
);
}
Loading
Loading