MOBILE-1927 calendar: Invalidate day when creating/editing an event

main
Albert Gasset 2019-08-08 10:49:35 +02:00
parent 184faa4a6d
commit 2b72b65ba5
1 changed files with 58 additions and 46 deletions

View File

@ -18,6 +18,7 @@ import { CoreSitesProvider } from '@providers/sites';
import { CoreCourseProvider } from '@core/course/providers/course';
import { AddonCalendarProvider } from './calendar';
import { CoreConstants } from '@core/constants';
import { CoreUtilsProvider } from '@providers/utils/utils';
import * as moment from 'moment';
/**
@ -38,7 +39,8 @@ export class AddonCalendarHelperProvider {
constructor(logger: CoreLoggerProvider,
private courseProvider: CoreCourseProvider,
private sitesProvider: CoreSitesProvider,
private calendarProvider: AddonCalendarProvider) {
private calendarProvider: AddonCalendarProvider,
private utils: CoreUtilsProvider) {
this.logger = logger.getInstance('AddonCalendarHelperProvider');
}
@ -286,20 +288,20 @@ export class AddonCalendarHelperProvider {
* @return {Promise<any>} REsolved when done.
*/
invalidateRepeatedEventsOnCalendar(event: any, repeated: number, siteId?: string): Promise<any> {
return this.sitesProvider.getSite(siteId).then((site) => {
let invalidatePromise;
const timestarts = [];
if (repeated > 1) {
if (event.repeatid) {
// Being edited or deleted.
invalidatePromise = this.calendarProvider.getLocalEventsByRepeatIdFromLocalDb(event.repeatid, siteId)
invalidatePromise = this.calendarProvider.getLocalEventsByRepeatIdFromLocalDb(event.repeatid, site.id)
.then((events) => {
return events.map((event) => {
return this.utils.allPromises(events.map((event) => {
timestarts.push(event.timestart);
return this.calendarProvider.invalidateEvent(event.id);
});
}));
});
} else {
// Being added.
@ -318,12 +320,14 @@ export class AddonCalendarHelperProvider {
invalidatePromise = this.calendarProvider.invalidateEvent(event.id);
}
return invalidatePromise.then(() => {
return invalidatePromise.finally(() => {
let lastMonth, lastYear;
return Promise.all([
return this.utils.allPromises([
this.calendarProvider.invalidateAllUpcomingEvents(),
timestarts.map((time) => {
// Invalidate months.
this.utils.allPromises(timestarts.map((time) => {
const day = moment(new Date(time * 1000));
if (lastMonth && (lastMonth == day.month() + 1 && lastYear == day.year())) {
@ -334,9 +338,17 @@ export class AddonCalendarHelperProvider {
lastMonth = day.month() + 1;
lastYear = day.year();
return this.calendarProvider.invalidateMonthlyEvents(lastYear, lastMonth, siteId);
})
return this.calendarProvider.invalidateMonthlyEvents(lastYear, lastMonth, site.id);
})),
// Invalidate days.
this.utils.allPromises(timestarts.map((time) => {
const day = moment(new Date(time * 1000));
return this.calendarProvider.invalidateDayEvents(day.year(), day.month() + 1, day.date(), site.id);
})),
]);
});
});
}
}