Merge pull request #2042 from crazyserver/MOBILE-3090
MOBILE-3090 calendar: Invalidate events when adding, editing or deletingmain
commit
0ac4ce4122
|
@ -491,7 +491,13 @@ export class AddonCalendarEditEventPage implements OnInit, OnDestroy {
|
|||
const modal = this.domUtils.showModalLoading('core.sending', true);
|
||||
|
||||
this.calendarProvider.submitEvent(this.eventId, data).then((result) => {
|
||||
const numberOfRepetitions = formData.repeat ? formData.repeats :
|
||||
(data.repeateditall && this.event.othereventscount ? this.event.othereventscount + 1 : 1);
|
||||
this.calendarHelper.invalidateRepeatedEventsOnCalendar(result.event, numberOfRepetitions).catch(() => {
|
||||
// Ignore errors.
|
||||
}).then(() => {
|
||||
this.returnToList(result.event);
|
||||
});
|
||||
}).catch((error) => {
|
||||
this.domUtils.showErrorModalDefault(error, 'Error sending data.');
|
||||
}).finally(() => {
|
||||
|
|
|
@ -445,7 +445,10 @@ export class AddonCalendarEventPage implements OnDestroy {
|
|||
const modal = this.domUtils.showModalLoading('core.sending', true);
|
||||
|
||||
this.calendarProvider.deleteEvent(this.event.id, this.event.name, deleteAll).then((sent) => {
|
||||
|
||||
this.calendarHelper.invalidateRepeatedEventsOnCalendar(this.event, deleteAll ? this.event.eventcount : 1)
|
||||
.catch(() => {
|
||||
// Ignore errors.
|
||||
}).then(() => {
|
||||
// Trigger an event.
|
||||
this.eventsProvider.trigger(AddonCalendarProvider.DELETED_EVENT_EVENT, {
|
||||
eventId: this.eventId,
|
||||
|
@ -463,7 +466,7 @@ export class AddonCalendarEventPage implements OnDestroy {
|
|||
// Event deleted in offline, just mark it as deleted.
|
||||
this.event.deleted = true;
|
||||
}
|
||||
|
||||
});
|
||||
}).catch((error) => {
|
||||
this.domUtils.showErrorModalDefault(error, 'Error deleting event.');
|
||||
}).finally(() => {
|
||||
|
|
|
@ -1146,6 +1146,19 @@ export class AddonCalendarProvider {
|
|||
return this.getEventsListPrefixCacheKey() + daysToStart + ':' + daysInterval;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get calendar events from local Db that have the same repeatid.
|
||||
*
|
||||
* @param {number} [repeatId] Repeat Id of the event.
|
||||
* @param {string} [siteId] ID of the site the event belongs to. If not defined, use current site.
|
||||
* @return {Promise<any[]>} Promise resolved with all the events.
|
||||
*/
|
||||
getLocalEventsByRepeatIdFromLocalDb(repeatId: number, siteId?: string): Promise<any[]> {
|
||||
return this.sitesProvider.getSite(siteId).then((site) => {
|
||||
return site.getDb().getRecords(AddonCalendarProvider.EVENTS_TABLE, {repeatid: repeatId});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get monthly calendar events.
|
||||
*
|
||||
|
|
|
@ -276,4 +276,67 @@ export class AddonCalendarHelperProvider {
|
|||
// Show the event if it is from site home or if it matches the selected course.
|
||||
return event.courseid === this.sitesProvider.getSiteHomeId() || event.courseid == courseId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Invalidate all calls from calendar WS calls.
|
||||
*
|
||||
* @param {any} event Event that has been touched.
|
||||
* @param {number} repeated Number of times the event is repeated.
|
||||
* @param {string} [siteId] Site ID. If not defined, current site.
|
||||
* @return {Promise<any>} REsolved when done.
|
||||
*/
|
||||
invalidateRepeatedEventsOnCalendar(event: any, repeated: number, siteId?: string): Promise<any> {
|
||||
let invalidatePromise;
|
||||
const timestarts = [];
|
||||
|
||||
if (repeated > 1) {
|
||||
if (event.repeatid) {
|
||||
// Being edited or deleted.
|
||||
invalidatePromise = this.calendarProvider.getLocalEventsByRepeatIdFromLocalDb(event.repeatid, siteId)
|
||||
.then((events) => {
|
||||
return events.map((event) => {
|
||||
timestarts.push(event.timestart);
|
||||
|
||||
return this.calendarProvider.invalidateEvent(event.id);
|
||||
});
|
||||
|
||||
});
|
||||
} else {
|
||||
// Being added.
|
||||
let time = event.timestart;
|
||||
while (repeated > 0) {
|
||||
timestarts.push(time);
|
||||
time += CoreConstants.SECONDS_DAY * 7;
|
||||
repeated--;
|
||||
}
|
||||
|
||||
invalidatePromise = Promise.resolve();
|
||||
}
|
||||
} else {
|
||||
// Not repeated.
|
||||
timestarts.push(event.timestart);
|
||||
invalidatePromise = this.calendarProvider.invalidateEvent(event.id);
|
||||
}
|
||||
|
||||
return invalidatePromise.then(() => {
|
||||
let lastMonth, lastYear;
|
||||
|
||||
return Promise.all([
|
||||
this.calendarProvider.invalidateAllUpcomingEvents(),
|
||||
timestarts.map((time) => {
|
||||
const day = moment(new Date(time * 1000));
|
||||
|
||||
if (lastMonth && (lastMonth == day.month() + 1 && lastYear == day.year())) {
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
// Invalidate once.
|
||||
lastMonth = day.month() + 1;
|
||||
lastYear = day.year();
|
||||
|
||||
return this.calendarProvider.invalidateMonthlyEvents(lastYear, lastMonth, siteId);
|
||||
})
|
||||
]);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue