1
0
mirror of https://github.com/trambarhq/relaks-wordpress-example.git synced 2025-09-25 23:09:08 +02:00

Fixed date range calculation.

This commit is contained in:
Chung Leong
2018-12-23 18:33:06 +01:00
parent 861a89bc8e
commit a73e2f74b7

View File

@@ -44,36 +44,35 @@ class SideNav extends AsyncComponent {
// build the archive tree // build the archive tree
props.archive = []; props.archive = [];
if (latestPost && earliestPost) { if (latestPost && earliestPost) {
let lastMonthEnd = Moment(latestPost.date_gmt).endOf('month'); let lastPostDate = Moment(latestPost.date_gmt);
let firstMonthStart = Moment(earliestPost.date_gmt).startOf('month'); let firstPostDate = Moment(earliestPost.date_gmt);
let currentYearEntry; // loop through the years
// loop from the last month to the first let lastYear = lastPostDate.year();
let e = lastMonthEnd.clone(); let firstYear = firstPostDate.year();
let s = e.clone().startOf('month'); for (let y = lastYear; y >= firstYear; y--) {
while (s >= firstMonthStart) { let yearEntry = {
let year = s.year(); year: y,
let month = s.month() + 1; label: Moment(`${y}-01-01`).format('YYYY'),
if (!currentYearEntry || currentYearEntry.year !== year) { months: []
// start a new year };
currentYearEntry = { props.archive.push(yearEntry);
year,
label: s.format('YYYY'),
months: []
};
props.archive.push(currentYearEntry);
}
let monthEntry = {
month,
label: s.format('MMMM'),
slug: s.format('YYYY-MM'),
post: undefined,
start: s.clone(),
end: e.clone(),
};
currentYearEntry.months.push(monthEntry);
e.subtract(1, 'month'); // loop through the months
s.subtract(1, 'month'); let lastMonth = (y === lastYear) ? lastPostDate.month() : 11;
let firstMonth = (y === firstYear) ? firstPostDate.month() : 0;
for (let m = lastMonth; m >= firstMonth; m--) {
let start = Moment(new Date(y, m, 1));
let end = start.clone().endOf('month');
let monthEntry = {
month: m + 1,
label: start.format('MMMM'),
slug: start.format('YYYY-MM'),
post: undefined,
start,
end,
};
yearEntry.months.push(monthEntry);
}
} }
meanwhile.show(<SideNavSync {...props} />); meanwhile.show(<SideNavSync {...props} />);
@@ -83,7 +82,7 @@ class SideNav extends AsyncComponent {
for (let monthEntry of yearEntry.months) { for (let monthEntry of yearEntry.months) {
let before = monthEntry.end.toISOString(); let before = monthEntry.end.toISOString();
let after = monthEntry.start.toISOString(); let after = monthEntry.start.toISOString();
let url = `/wp/v2/posts/?before=${before}&after=${after}`; let url = `/wp/v2/posts/?after=${after}&before=${before}`;
monthEntry.posts = await wp.fetchList(url); monthEntry.posts = await wp.fetchList(url);
// force prop change // force prop change