Skip to content

Commit 2a2f9e9

Browse files
Merge pull request #771 from carrythebanner/event-ranges
Event range request improvements
2 parents 820238c + d18c921 commit 2a2f9e9

File tree

4 files changed

+35
-14
lines changed

4 files changed

+35
-14
lines changed

app/endpoints/events.js

+19-8
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ const config = require("../config");
2828
const { fromYMDString, to24HourString, toYMDString } = require("../util/dateTime");
2929
const { CalEvent } = require("../models/calEvent");
3030
const { CalDaily } = require("../models/calDaily");
31+
const { EventsRange } = require("../models/calConst");
32+
3133

3234
// the events endpoint:
3335
exports.get = function(req, res, next) {
@@ -58,11 +60,13 @@ exports.get = function(req, res, next) {
5860
if (!start.isValid() || !end.isValid()) {
5961
res.textError("need valid start and end times");
6062
} else {
61-
const range = end.diff(start, 'day');
62-
if (range < 0) {
63-
res.textError("start date should be before end date");
64-
} else if (range > 100) {
65-
res.textError(`event range too large: ${range} days requested; max 100 days`);
63+
// add 1 so days in range is inclusive
64+
// e.g. (2025-06-10 - 2025-06-01 = 9 days difference) + 1 day = 10 day range
65+
const range = end.diff(start, 'day') + 1;
66+
if (range < 1) {
67+
res.textError("end date cannot be before start date");
68+
} else if (range > EventsRange.MaxDays) {
69+
res.textError(`event range too large: ${range} days requested; max ${EventsRange.MaxDays} days`);
6670
} else {
6771
return CalDaily.getRangeVisible(start, end, includeAllEvents).then((dailies) => {
6872
return getSummaries(dailies).then((events) => {
@@ -113,16 +117,23 @@ function specialSummary(evt) {
113117
// expects days are dayjs objects
114118
// and count is the number of events between the two
115119
function getPagination(firstDay, lastDay, count) {
116-
const range = lastDay.diff(firstDay, 'day');
117-
const nextRangeStart = firstDay.add(range + 1, 'day');
118-
const nextRangeEnd = lastDay.add(range + 1, 'day');
120+
// add 1 so days in range is inclusive
121+
const range = lastDay.diff(firstDay, 'day') + 1;
122+
123+
const prevRangeStart = firstDay.subtract(range, 'day');
124+
const prevRangeEnd = lastDay.subtract(range, 'day');
125+
const prev = getEventRangeUrl(prevRangeStart, prevRangeEnd);
126+
127+
const nextRangeStart = firstDay.add(range, 'day');
128+
const nextRangeEnd = lastDay.add(range, 'day');
119129
const next = getEventRangeUrl(nextRangeStart, nextRangeEnd);
120130

121131
return {
122132
start: toYMDString(firstDay),
123133
end: toYMDString(lastDay),
124134
range, // tbd: do we need to send this? can client determine from start, end?
125135
events: count,
136+
prev,
126137
next,
127138
};
128139
}

app/models/calConst.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,9 @@ const Review = Object.freeze({
4242
Revised : 'R',
4343
});
4444

45+
const EventsRange = Object.freeze({
46+
MaxDays : 100,
47+
});
48+
4549
//
46-
module.exports = { Area, Audience, DatesType, EventStatus, Review };
50+
module.exports = { Area, Audience, DatesType, EventStatus, Review, EventsRange };

site/themes/s2b_hugo_theme/assets/js/cal/helpers.js

+4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ const DEFAULT_AREA = 'P';
3131
const DEFAULT_AUDIENCE = 'G';
3232
const DEFAULT_LENGTH = '--';
3333

34+
// total number of days to fetch, inclusive of start and end dates;
35+
// minimum of 1, maximum set by server
36+
const DEFAULT_DAYS_TO_FETCH = 10;
37+
3438
const SITE_TITLE = "Shift";
3539

3640
const API_VERSION = '3';

site/themes/s2b_hugo_theme/assets/js/cal/main.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,6 @@ $(document).ready(function() {
8282
});
8383
}
8484

85-
// default range of days to show.
86-
const dayRange = 10;
87-
8885
// compute range and details settings from the url options.
8986
// the returned object gets passed to getEventHTML().
9087
function getInitialView(options) {
@@ -103,7 +100,8 @@ $(document).ready(function() {
103100
// 'from' is today; for other PP pages it's options startdate.
104101
startdate: from,
105102
// if there was an enddate, use it; otherwise use a fixed number of days.
106-
enddate: options.enddate ? end : from.add(dayRange, 'day'),
103+
// subtract 1 so range is inclusive
104+
enddate: options.enddate ? end : from.add( (DEFAULT_DAYS_TO_FETCH - 1), 'day'),
107105
// pass this on to the events listing.
108106
show_details: options.show_details,
109107
};
@@ -130,9 +128,13 @@ $(document).ready(function() {
130128
lazyLoadEventImages();
131129
$(document).off('click', '#load-more')
132130
.on('click', '#load-more', function(e) {
131+
// if there is a user-provided enddate, use that to set the day range (and add 1 so date range is inclusive);
132+
// otherwise, use the default range
133+
range = options.enddate ? (view.enddate.diff(view.startdate, 'day') + 1) : DEFAULT_DAYS_TO_FETCH;
134+
133135
// the next day to view is one day after the previous last
134136
view.startdate = view.enddate.add(1, 'day');
135-
view.enddate = view.startdate.add(dayRange, 'day');
137+
view.enddate = view.startdate.add(range - 1, 'day');
136138
// add new events to the end of those we've already added.
137139
getEventHTML(view, function(eventHTML) {
138140
$('#load-more').before(eventHTML);

0 commit comments

Comments
 (0)