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

Post fields: move status from edit-site to fields #66937

Merged
merged 1 commit into from
Nov 12, 2024
Merged
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
81 changes: 3 additions & 78 deletions packages/edit-site/src/components/post-fields/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,89 +13,25 @@ import {
slugField,
parentField,
passwordField,
statusField,
} from '@wordpress/fields';
import {
createInterpolateElement,
useMemo,
useState,
} from '@wordpress/element';
import { dateI18n, getDate, getSettings } from '@wordpress/date';
import {
trash,
drafts,
published,
scheduled,
pending,
notAllowed,
commentAuthorAvatar as authorIcon,
} from '@wordpress/icons';
import { commentAuthorAvatar as authorIcon } from '@wordpress/icons';
import { __experimentalHStack as HStack, Icon } from '@wordpress/components';
import { useSelect } from '@wordpress/data';
import { useEntityRecords, store as coreStore } from '@wordpress/core-data';

/**
* Internal dependencies
*/
import { OPERATOR_IS_ANY } from '../../utils/constants';

// See https://github.com/WordPress/gutenberg/issues/55886
// We do not support custom statutes at the moment.
const STATUSES = [
{
value: 'draft',
label: __( 'Draft' ),
icon: drafts,
description: __( 'Not ready to publish.' ),
},
{
value: 'future',
label: __( 'Scheduled' ),
icon: scheduled,
description: __( 'Publish automatically on a chosen date.' ),
},
{
value: 'pending',
label: __( 'Pending Review' ),
icon: pending,
description: __( 'Waiting for review before publishing.' ),
},
{
value: 'private',
label: __( 'Private' ),
icon: notAllowed,
description: __( 'Only visible to site admins and editors.' ),
},
{
value: 'publish',
label: __( 'Published' ),
icon: published,
description: __( 'Visible to everyone.' ),
},
{ value: 'trash', label: __( 'Trash' ), icon: trash },
];

const getFormattedDate = ( dateToDisplay ) =>
dateI18n(
getSettings().formats.datetimeAbbreviated,
getDate( dateToDisplay )
);

function PostStatusField( { item } ) {
const status = STATUSES.find( ( { value } ) => value === item.status );
const label = status?.label || item.status;
const icon = status?.icon;
return (
<HStack alignment="left" spacing={ 0 }>
{ icon && (
<div className="edit-site-post-list__status-icon">
<Icon icon={ icon } />
</div>
) }
<span>{ label }</span>
</HStack>
);
}

function PostAuthorField( { item } ) {
const { text, imageUrl } = useSelect(
( select ) => {
Expand Down Expand Up @@ -214,18 +150,7 @@ function usePostFields() {
: nameB.localeCompare( nameA );
},
},
{
label: __( 'Status' ),
id: 'status',
type: 'text',
elements: STATUSES,
render: PostStatusField,
Edit: 'radio',
enableSorting: false,
filterBy: {
operators: [ OPERATOR_IS_ANY ],
},
},
statusField,
{
label: __( 'Date' ),
id: 'date',
Expand Down
4 changes: 4 additions & 0 deletions packages/fields/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ Undocumented declaration.

Undocumented declaration.

### statusField

Status field for BasePost.

### titleField

Undocumented declaration.
Expand Down
1 change: 1 addition & 0 deletions packages/fields/src/fields/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export { default as orderField } from './order';
export { default as featuredImageField } from './featured-image';
export { default as parentField } from './parent';
export { default as passwordField } from './password';
export { default as statusField } from './status';
32 changes: 32 additions & 0 deletions packages/fields/src/fields/status/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* WordPress dependencies
*/
import type { Field } from '@wordpress/dataviews';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import type { BasePost } from '../../types';
import StatusView from './status-view';
import STATUSES from './status-elements';

const OPERATOR_IS_ANY = 'isAny';

const statusField: Field< BasePost > = {
label: __( 'Status' ),
id: 'status',
type: 'text',
elements: STATUSES,
render: StatusView,
Edit: 'radio',
enableSorting: false,
filterBy: {
operators: [ OPERATOR_IS_ANY ],
},
};

/**
* Status field for BasePost.
*/
export default statusField;
50 changes: 50 additions & 0 deletions packages/fields/src/fields/status/status-elements.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* WordPress dependencies
*/
import {
trash,
drafts,
published,
scheduled,
pending,
notAllowed,
} from '@wordpress/icons';
import { __ } from '@wordpress/i18n';

// See https://github.com/WordPress/gutenberg/issues/55886
// We do not support custom statutes at the moment.
const STATUSES = [
{
value: 'draft',
label: __( 'Draft' ),
icon: drafts,
description: __( 'Not ready to publish.' ),
},
{
value: 'future',
label: __( 'Scheduled' ),
icon: scheduled,
description: __( 'Publish automatically on a chosen date.' ),
},
{
value: 'pending',
label: __( 'Pending Review' ),
icon: pending,
description: __( 'Waiting for review before publishing.' ),
},
{
value: 'private',
label: __( 'Private' ),
icon: notAllowed,
description: __( 'Only visible to site admins and editors.' ),
},
{
value: 'publish',
label: __( 'Published' ),
icon: published,
description: __( 'Visible to everyone.' ),
},
{ value: 'trash', label: __( 'Trash' ), icon: trash },
];

export default STATUSES;
28 changes: 28 additions & 0 deletions packages/fields/src/fields/status/status-view.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* WordPress dependencies
*/
import { __experimentalHStack as HStack, Icon } from '@wordpress/components';

/**
* Internal dependencies
*/
import type { BasePost } from '../../types';
import STATUSES from './status-elements';

function StatusView( { item }: { item: BasePost } ) {
const status = STATUSES.find( ( { value } ) => value === item.status );
const label = status?.label || item.status;
const icon = status?.icon;
return (
<HStack alignment="left" spacing={ 0 }>
{ icon && (
<div className="edit-site-post-list__status-icon">
<Icon icon={ icon } />
</div>
) }
<span>{ label }</span>
</HStack>
);
}

export default StatusView;
Loading