Skip to content

Commit

Permalink
Extract date to fields package
Browse files Browse the repository at this point in the history
  • Loading branch information
oandregal committed Nov 12, 2024
1 parent d2bc9ea commit 9cbcd77
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 91 deletions.
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

94 changes: 4 additions & 90 deletions packages/edit-site/src/components/post-fields/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,16 @@ import clsx from 'clsx';
/**
* WordPress dependencies
*/
import { __, sprintf } from '@wordpress/i18n';
import { __ } from '@wordpress/i18n';
import { decodeEntities } from '@wordpress/html-entities';
import {
featuredImageField,
slugField,
parentField,
passwordField,
dateField,
} from '@wordpress/fields';
import {
createInterpolateElement,
useMemo,
useState,
} from '@wordpress/element';
import { dateI18n, getDate, getSettings } from '@wordpress/date';
import { useMemo, useState } from '@wordpress/element';
import {
trash,
drafts,
Expand Down Expand Up @@ -74,12 +70,6 @@ const STATUSES = [
{ 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;
Expand Down Expand Up @@ -226,83 +216,7 @@ function usePostFields() {
operators: [ OPERATOR_IS_ANY ],
},
},
{
label: __( 'Date' ),
id: 'date',
type: 'datetime',
render: ( { item } ) => {
const isDraftOrPrivate = [ 'draft', 'private' ].includes(
item.status
);
if ( isDraftOrPrivate ) {
return createInterpolateElement(
sprintf(
/* translators: %s: page creation or modification date. */
__( '<span>Modified: <time>%s</time></span>' ),
getFormattedDate( item.date )
),
{
span: <span />,
time: <time />,
}
);
}

const isScheduled = item.status === 'future';
if ( isScheduled ) {
return createInterpolateElement(
sprintf(
/* translators: %s: page creation date */
__( '<span>Scheduled: <time>%s</time></span>' ),
getFormattedDate( item.date )
),
{
span: <span />,
time: <time />,
}
);
}

const isPublished = item.status === 'publish';
if ( isPublished ) {
return createInterpolateElement(
sprintf(
/* translators: %s: page creation time */
__( '<span>Published: <time>%s</time></span>' ),
getFormattedDate( item.date )
),
{
span: <span />,
time: <time />,
}
);
}

// Pending posts show the modified date if it's newer.
const dateToDisplay =
getDate( item.modified ) > getDate( item.date )
? item.modified
: item.date;

const isPending = item.status === 'pending';
if ( isPending ) {
return createInterpolateElement(
sprintf(
/* translators: %s: page creation or modification date. */
__( '<span>Modified: <time>%s</time></span>' ),
getFormattedDate( dateToDisplay )
),
{
span: <span />,
time: <time />,
}
);
}

// Unknow status.
return <time>{ getFormattedDate( item.date ) }</time>;
},
},
dateField,
slugField,
parentField,
{
Expand Down
4 changes: 4 additions & 0 deletions packages/fields/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ npm install @wordpress/fields --save

<!-- START TOKEN(Autogenerated API docs) -->

### dateField

Date field for BasePost.

### deletePost

Undocumented declaration.
Expand Down
1 change: 1 addition & 0 deletions packages/fields/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"@wordpress/core-data": "*",
"@wordpress/data": "*",
"@wordpress/dataviews": "*",
"@wordpress/date": "5.11.0",
"@wordpress/element": "*",
"@wordpress/hooks": "*",
"@wordpress/html-entities": "*",
Expand Down
93 changes: 93 additions & 0 deletions packages/fields/src/fields/date/date-view.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/**
* WordPress dependencies
*/
import { __, sprintf } from '@wordpress/i18n';
import { createInterpolateElement } from '@wordpress/element';
import { dateI18n, getDate, getSettings } from '@wordpress/date';

/**
* Internal dependencies
*/
import type { BasePost } from '../../types';

const getFormattedDate = ( dateToDisplay: string | null ) =>
dateI18n(
getSettings().formats.datetimeAbbreviated,
getDate( dateToDisplay ),
undefined
);

const DateView = ( { item }: { item: BasePost } ) => {
const isDraftOrPrivate = [ 'draft', 'private' ].includes(
item.status ?? ''
);
if ( isDraftOrPrivate ) {
return createInterpolateElement(
sprintf(
/* translators: %s: page creation or modification date. */
__( '<span>Modified: <time>%s</time></span>' ),
getFormattedDate( item.date ?? null )
),
{
span: <span />,
time: <time />,
}
);
}

const isScheduled = item.status === 'future';
if ( isScheduled ) {
return createInterpolateElement(
sprintf(
/* translators: %s: page creation date */
__( '<span>Scheduled: <time>%s</time></span>' ),
getFormattedDate( item.date ?? null )
),
{
span: <span />,
time: <time />,
}
);
}

const isPublished = item.status === 'publish';
if ( isPublished ) {
return createInterpolateElement(
sprintf(
/* translators: %s: page creation time */
__( '<span>Published: <time>%s</time></span>' ),
getFormattedDate( item.date ?? null )
),
{
span: <span />,
time: <time />,
}
);
}

// Pending posts show the modified date if it's newer.
const dateToDisplay =
getDate( item.modified ?? null ) > getDate( item.date ?? null )
? item.modified
: item.date;

const isPending = item.status === 'pending';
if ( isPending ) {
return createInterpolateElement(
sprintf(
/* translators: %s: page creation or modification date. */
__( '<span>Modified: <time>%s</time></span>' ),
getFormattedDate( dateToDisplay ?? null )
),
{
span: <span />,
time: <time />,
}
);
}

// Unknow status.
return <time>{ getFormattedDate( item.date ?? null ) }</time>;
};

export default DateView;
23 changes: 23 additions & 0 deletions packages/fields/src/fields/date/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* WordPress dependencies
*/
import type { Field } from '@wordpress/dataviews';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import type { BasePost } from '../../types';
import DateView from './date-view';

const dateField: Field< BasePost > = {
id: 'date',
type: 'datetime',
label: __( 'Date' ),
render: DateView,
};

/**
* Date field for BasePost.
*/
export default dateField;
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 dateField } from './date';
4 changes: 3 additions & 1 deletion packages/fields/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
type PostStatus =
| 'published'
| 'publish'
| 'draft'
| 'pending'
| 'private'
Expand Down Expand Up @@ -37,6 +37,8 @@ export interface BasePost extends CommonPost {
link?: string;
slug?: string;
permalink_template?: string;
date?: string;
modified?: string;
}

export interface Template extends CommonPost {
Expand Down

0 comments on commit 9cbcd77

Please sign in to comment.