-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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 date
fields from edit-site
to fields
package
#66938
Changes from all commits
ae424e1
b2a5e69
0795efe
e92d04e
2ed038c
544bf3e
2686139
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/** | ||
* 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 ) | ||
); | ||
|
||
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; |
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; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
type PostStatus = | ||
| 'published' | ||
| 'publish' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was the wrong value. |
||
| 'draft' | ||
| 'pending' | ||
| 'private' | ||
|
@@ -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 { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,24 +8,25 @@ | |
}, | ||
"references": [ | ||
{ "path": "../api-fetch" }, | ||
{ "path": "../blob" }, | ||
{ "path": "../components" }, | ||
{ "path": "../compose" }, | ||
{ "path": "../core-data" }, | ||
{ "path": "../data" }, | ||
{ "path": "../dataviews" }, | ||
{ "path": "../date" }, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorted alphabetically, like the
|
||
{ "path": "../element" }, | ||
{ "path": "../hooks" }, | ||
{ "path": "../html-entities" }, | ||
{ "path": "../i18n" }, | ||
{ "path": "../icons" }, | ||
{ "path": "../media-utils" }, | ||
{ "path": "../notices" }, | ||
{ "path": "../primitives" }, | ||
{ "path": "../private-apis" }, | ||
{ "path": "../warning" }, | ||
{ "path": "../router" }, | ||
{ "path": "../url" }, | ||
{ "path": "../notices" }, | ||
{ "path": "../dataviews" }, | ||
{ "path": "../blob" }, | ||
{ "path": "../core-data" }, | ||
{ "path": "../hooks" }, | ||
{ "path": "../html-entities" }, | ||
{ "path": "../media-utils" }, | ||
{ "path": "../router" } | ||
{ "path": "../warning" } | ||
], | ||
"include": [ "src" ] | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One interesting thing from migrating this to TypeScript is that it forces you to pass the last parameter. Despite what the docs say, there's no default value and cannot be undefined.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this can be solved by slightly changing the type definition of the function and making the argument optional
=something
rather thansomething | undefined
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I pushed 9e44b6f and 012a507
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reverted the default parameter and modified the JSDoc instead at 544bf3e