Skip to content

Commit

Permalink
Retrieve all future posts in a given month (#44540)
Browse files Browse the repository at this point in the history
* Retrieve all future posts in a given month

* Retrieve 100 posts and only retrieve ID and date

* Remove type and title from events

* Use date-fns for date calculations

* Fix date parsing errors

* Update package.json

Required for new editor dependency (date-fns)

Co-authored-by: Robert Anderson <robert@noisysocks.com>
  • Loading branch information
GeoJunkie and noisysocks authored Oct 25, 2022
1 parent eefde30 commit 41a3023
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 27 deletions.
22 changes: 15 additions & 7 deletions package-lock.json

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

1 change: 1 addition & 0 deletions packages/editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"@wordpress/url": "file:../url",
"@wordpress/wordcount": "file:../wordcount",
"classnames": "^2.3.1",
"date-fns": "^2.28.0",
"lodash": "^4.17.21",
"memize": "^1.1.0",
"react-autosize-textarea": "^7.1.0",
Expand Down
36 changes: 16 additions & 20 deletions packages/editor/src/components/post-schedule/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* External dependencies
*/
import { parseISO, endOfMonth, startOfMonth } from 'date-fns';

/**
* WordPress dependencies
*/
Expand All @@ -12,15 +17,6 @@ import { store as coreStore } from '@wordpress/core-data';
*/
import { store as editorStore } from '../../store';

function getDayOfTheMonth( date = new Date(), firstDay = true ) {
const d = new Date( date );
return new Date(
d.getFullYear(),
d.getMonth() + ( firstDay ? 0 : 1 ),
firstDay ? 1 : 0
).toISOString();
}

export default function PostSchedule( { onClose } ) {
const { postDate, postType } = useSelect(
( select ) => ( {
Expand All @@ -34,30 +30,28 @@ export default function PostSchedule( { onClose } ) {
const onUpdateDate = ( date ) => editPost( { date } );

const [ previewedMonth, setPreviewedMonth ] = useState(
getDayOfTheMonth( postDate )
startOfMonth( new Date( postDate ) )
);

// Pick up published and schduled site posts.
const eventsByPostType = useSelect(
( select ) =>
select( coreStore ).getEntityRecords( 'postType', postType, {
status: 'publish,future',
after: getDayOfTheMonth( previewedMonth ),
before: getDayOfTheMonth( previewedMonth, false ),
after: startOfMonth( previewedMonth ).toISOString(),
before: endOfMonth( previewedMonth ).toISOString(),
exclude: [ select( editorStore ).getCurrentPostId() ],
per_page: 100,
_fields: 'id,date',
} ),
[ previewedMonth, postType ]
);

const events = useMemo(
() =>
( eventsByPostType || [] ).map(
( { title, type, date: eventDate } ) => ( {
title: title?.rendered,
type,
date: new Date( eventDate ),
} )
),
( eventsByPostType || [] ).map( ( { date: eventDate } ) => ( {
date: new Date( eventDate ),
} ) ),
[ eventsByPostType ]
);

Expand All @@ -80,7 +74,9 @@ export default function PostSchedule( { onClose } ) {
onChange={ onUpdateDate }
is12Hour={ is12HourTime }
events={ events }
onMonthPreviewed={ setPreviewedMonth }
onMonthPreviewed={ ( date ) =>
setPreviewedMonth( parseISO( date ) )
}
onClose={ onClose }
/>
);
Expand Down

0 comments on commit 41a3023

Please sign in to comment.