Skip to content

Commit f7a13c1

Browse files
authored
Merge pull request #113 from AlaaN-Smadi/master
filter updated events
2 parents 1d87b61 + 01dc730 commit f7a13c1

5 files changed

Lines changed: 66 additions & 4 deletions

File tree

widget/assets/js/rrule.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1884,7 +1884,8 @@
18841884
rrule._iter(iterResult)
18851885
})
18861886

1887-
var res = iterResult._result
1887+
// Filtering excluded and deleted dates to get the final updated events result
1888+
var res = iterResult._result.filter(date => !this._exdate.find(exdate => new Date(exdate).setHours(0,0,0) === new Date(date).setHours(0,0,0)))
18881889
dateutil.sort(res)
18891890
switch (iterResult.method) {
18901891
case 'all':

widget/controllers/widget.feed.controller.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,6 @@
285285
temp_result.tmpStartDate = temp_result.startDate;
286286
temp_result.startDate = Date.parse(dates[j]);
287287
temp_result.endDate = temp_result.startDate + eventDuration;
288-
temp_result.UID = temp_result.UID + String(temp_result.startDate) + String(temp_result.endDate)
289288
if (temp_result.startDate >= +new Date(eventStartDate) && temp_result.startDate <= +new Date(eventRecEndDate))
290289
if (AllEvent)
291290
repeat_results.push(temp_result);
@@ -314,13 +313,15 @@
314313
startDate += 86400000;
315314
while (startDate < endDate) {
316315
if (startDate >= +new Date(eventStartDate) && startDate <= +new Date(eventRecEndDate) && (AllEvent || startDate >= timeStampInMiliSec)) {
317-
repeat_results.push({...result.events[i], startDate: new Date(startDate), UID: "_id" + Date.now() + Math.random()});
316+
repeat_results.push({...result.events[i], startDate: new Date(startDate)});
318317
}
319318
startDate += 86400000;
320319
}
321320
}
322321

323322
}}
323+
// filter out the repeating events that have been updated
324+
repeat_results = utils.filterUpdatedEvents(repeat_results);
324325
//sort the list by start date
325326
repeat_results.sort(function (a, b) {
326327
if (a.startDate > b.startDate) {

widget/index.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
<!-- App -->
2626
<script src="../../../scripts/angular/ng-infinite-scroll.custom.js"
2727
type="application/javascript"></script>
28+
<script src="utils/nanoid.js"></script>
29+
<script src="utils/index.js"></script>
2830
<script src="assets/js/moment.min.js"></script>
2931
<script src="assets/js/jstz.min.js" type="application/javascript"></script>
3032
<script src="assets/js/moment-timezone.min.js" type="application/javascript"></script>
@@ -68,7 +70,7 @@
6870
ng-swipe-left="WidgetFeed.addEvents($event, $index, false)"
6971
custom-class="getDayClass(date, mode)"
7072
ng-class="{'active' : WidgetFeed.swiped[$index]}"
71-
ng-repeat="event in WidgetFeed.events track by event.UID">
73+
ng-repeat="event in WidgetFeed.events track by event.id">
7274
<div class="calendar-icon text-center bg-primary"
7375
ng-click="WidgetFeed.addEventsToCalendar(event, $index)">
7476
<span class="icon icon-calendar-check"></span>

widget/utils/index.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
const utils = {
3+
// this function will filter out the repeating events that have been updated
4+
// it will skip all previous versions, and only show the latest version of the event
5+
filterUpdatedEvents(events) {
6+
let latestEvents = {};
7+
events.forEach(event => {
8+
let uid = event.UID;
9+
// id is a unique identifier key to separate event and avoid duplicate keys in the DOM
10+
event.id = nanoid();
11+
let newStartDate = new Date(event.startDate);
12+
let recurrenceId = 0;
13+
let oldRecurrenceId = 0;
14+
// check if this event has been seen before
15+
// the key is the UID + the date of the event
16+
let storedRecored = latestEvents[uid + `${newStartDate.getDate()}-${newStartDate.getMonth()}-${newStartDate.getFullYear()}`];
17+
18+
for (const key in event) {
19+
if (key.startsWith('RECURRENCE-ID')) {
20+
recurrenceId = event[key];
21+
}
22+
}
23+
if (storedRecored) {
24+
for (const key in storedRecored) {
25+
if (key.startsWith('RECURRENCE-ID')) {
26+
oldRecurrenceId = event[key];
27+
}
28+
}
29+
}
30+
31+
// to add the event to the list, it must be the first time we see it
32+
// or it must be a newer version of the event
33+
if (!storedRecored
34+
|| (recurrenceId && !oldRecurrenceId)
35+
|| (recurrenceId && oldRecurrenceId && Number(recurrenceId.substr(0, 8)) > Number(oldRecurrenceId.substr(0, 8)) && Number(recurrenceId.substr(9, 15)) > Number(oldRecurrenceId.substr(9, 15)))
36+
) {
37+
latestEvents[uid + `${newStartDate.getDate()}-${newStartDate.getMonth()}-${newStartDate.getFullYear()}`] = event;
38+
}
39+
});
40+
// return the values of the object
41+
return Object.values(latestEvents);
42+
},
43+
};

widget/utils/nanoid.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
let nanoid = (t = 21) =>
2+
crypto
3+
.getRandomValues(new Uint8Array(t))
4+
.reduce(
5+
(t, e) =>
6+
(t +=
7+
(e &= 63) < 36
8+
? e.toString(36)
9+
: e < 62
10+
? (e - 26).toString(36).toUpperCase()
11+
: e > 62
12+
? '-'
13+
: '_'),
14+
''
15+
);

0 commit comments

Comments
 (0)