Skip to content

Commit 6d0b53f

Browse files
committed
MOBILE-3881 timeline: Fix minimim time to get events from
1 parent c644eeb commit 6d0b53f

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

src/addons/block/timeline/components/events/events.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ import { CoreDomUtils } from '@services/utils/dom';
1818
import { CoreTextUtils } from '@services/utils/text';
1919
import { CoreTimeUtils } from '@services/utils/time';
2020
import { CoreCourse } from '@features/course/services/course';
21-
import moment from 'moment';
2221
import { CoreContentLinksHelper } from '@features/contentlinks/services/contentlinks-helper';
2322
import { AddonCalendarEvent } from '@addons/calendar/services/calendar';
2423
import { CoreEnrolledCourseDataWithOptions } from '@features/courses/services/courses-helper';
24+
import { AddonBlockTimeline } from '../../services/timeline';
2525

2626
/**
2727
* Directive to render a list of events in course overview.
@@ -94,8 +94,8 @@ export class AddonBlockTimelineEventsComponent implements OnChanges {
9494
* @return Filtered events.
9595
*/
9696
protected async filterEventsByTime(start: number, end?: number): Promise<AddonBlockTimelineEvent[]> {
97-
start = moment().add(start, 'days').startOf('day').unix();
98-
end = end !== undefined ? moment().add(end, 'days').startOf('day').unix() : end;
97+
start = AddonBlockTimeline.getDayStart(start);
98+
end = end !== undefined ? AddonBlockTimeline.getDayStart(end) : end;
9999

100100
return await Promise.all(this.events.filter((event) => {
101101
if (end) {

src/addons/block/timeline/services/timeline.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export class AddonBlockTimelineProvider {
5555
): Promise<{ events: AddonCalendarEvent[]; canLoadMore?: number }> {
5656
const site = await CoreSites.getSite(siteId);
5757

58-
const time = moment().subtract(14, 'days').unix(); // Check two weeks ago.
58+
const time = this.getDayStart(-14); // Check two weeks ago.
5959

6060
const data: AddonCalendarGetActionEventsByCourseWSParams = {
6161
timesortfrom: time,
@@ -109,7 +109,7 @@ export class AddonBlockTimelineProvider {
109109
): Promise<{[courseId: string]: { events: AddonCalendarEvent[]; canLoadMore?: number } }> {
110110
const site = await CoreSites.getSite(siteId);
111111

112-
const time = moment().subtract(14, 'days').unix(); // Check two weeks ago.
112+
const time = this.getDayStart(-14); // Check two weeks ago.
113113

114114
const data: AddonCalendarGetActionEventsByCoursesWSParams = {
115115
timesortfrom: time,
@@ -164,7 +164,7 @@ export class AddonBlockTimelineProvider {
164164
): Promise<{ events: AddonCalendarEvent[]; canLoadMore?: number }> {
165165
const site = await CoreSites.getSite(siteId);
166166

167-
const timesortfrom = moment().subtract(14, 'days').unix(); // Check two weeks ago.
167+
const timesortfrom = this.getDayStart(-14); // Check two weeks ago.
168168
const limitnum = AddonBlockTimelineProvider.EVENTS_LIMIT;
169169

170170
const data: AddonCalendarGetActionEventsByTimesortWSParams = {
@@ -275,6 +275,16 @@ export class AddonBlockTimelineProvider {
275275
};
276276
}
277277

278+
/**
279+
* Returns the timestamp at the start of the day with an optional offset.
280+
*
281+
* @param daysOffset Offset days to add or substract.
282+
* @return timestamp.
283+
*/
284+
getDayStart(daysOffset = 0): number {
285+
return moment().startOf('day').add(daysOffset, 'days').unix();
286+
}
287+
278288
}
279289

280290
export const AddonBlockTimeline = makeSingleton(AddonBlockTimelineProvider);

src/core/singletons/time.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export class CoreTime {
8181
/**
8282
* Converts a number of seconds into a short human readable format: minutes and seconds, in fromat: 3' 27''.
8383
*
84-
* @param seconds Seconds
84+
* @param duration Duration in seconds.
8585
* @return Short human readable text.
8686
*/
8787
static formatTimeShort(duration: number): string {

0 commit comments

Comments
 (0)