Skip to content

Commit

Permalink
Merge pull request #113 from AlaaN-Smadi/master
Browse files Browse the repository at this point in the history
filter updated events
  • Loading branch information
mas-iota authored Jan 24, 2024
2 parents 1d87b61 + 01dc730 commit f7a13c1
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 4 deletions.
3 changes: 2 additions & 1 deletion widget/assets/js/rrule.js
Original file line number Diff line number Diff line change
Expand Up @@ -1884,7 +1884,8 @@
rrule._iter(iterResult)
})

var res = iterResult._result
// Filtering excluded and deleted dates to get the final updated events result
var res = iterResult._result.filter(date => !this._exdate.find(exdate => new Date(exdate).setHours(0,0,0) === new Date(date).setHours(0,0,0)))
dateutil.sort(res)
switch (iterResult.method) {
case 'all':
Expand Down
5 changes: 3 additions & 2 deletions widget/controllers/widget.feed.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,6 @@
temp_result.tmpStartDate = temp_result.startDate;
temp_result.startDate = Date.parse(dates[j]);
temp_result.endDate = temp_result.startDate + eventDuration;
temp_result.UID = temp_result.UID + String(temp_result.startDate) + String(temp_result.endDate)
if (temp_result.startDate >= +new Date(eventStartDate) && temp_result.startDate <= +new Date(eventRecEndDate))
if (AllEvent)
repeat_results.push(temp_result);
Expand Down Expand Up @@ -314,13 +313,15 @@
startDate += 86400000;
while (startDate < endDate) {
if (startDate >= +new Date(eventStartDate) && startDate <= +new Date(eventRecEndDate) && (AllEvent || startDate >= timeStampInMiliSec)) {
repeat_results.push({...result.events[i], startDate: new Date(startDate), UID: "_id" + Date.now() + Math.random()});
repeat_results.push({...result.events[i], startDate: new Date(startDate)});
}
startDate += 86400000;
}
}

}}
// filter out the repeating events that have been updated
repeat_results = utils.filterUpdatedEvents(repeat_results);
//sort the list by start date
repeat_results.sort(function (a, b) {
if (a.startDate > b.startDate) {
Expand Down
4 changes: 3 additions & 1 deletion widget/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
<!-- App -->
<script src="../../../scripts/angular/ng-infinite-scroll.custom.js"
type="application/javascript"></script>
<script src="utils/nanoid.js"></script>
<script src="utils/index.js"></script>
<script src="assets/js/moment.min.js"></script>
<script src="assets/js/jstz.min.js" type="application/javascript"></script>
<script src="assets/js/moment-timezone.min.js" type="application/javascript"></script>
Expand Down Expand Up @@ -68,7 +70,7 @@
ng-swipe-left="WidgetFeed.addEvents($event, $index, false)"
custom-class="getDayClass(date, mode)"
ng-class="{'active' : WidgetFeed.swiped[$index]}"
ng-repeat="event in WidgetFeed.events track by event.UID">
ng-repeat="event in WidgetFeed.events track by event.id">
<div class="calendar-icon text-center bg-primary"
ng-click="WidgetFeed.addEventsToCalendar(event, $index)">
<span class="icon icon-calendar-check"></span>
Expand Down
43 changes: 43 additions & 0 deletions widget/utils/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

const utils = {
// this function will filter out the repeating events that have been updated
// it will skip all previous versions, and only show the latest version of the event
filterUpdatedEvents(events) {
let latestEvents = {};
events.forEach(event => {
let uid = event.UID;
// id is a unique identifier key to separate event and avoid duplicate keys in the DOM
event.id = nanoid();
let newStartDate = new Date(event.startDate);
let recurrenceId = 0;
let oldRecurrenceId = 0;
// check if this event has been seen before
// the key is the UID + the date of the event
let storedRecored = latestEvents[uid + `${newStartDate.getDate()}-${newStartDate.getMonth()}-${newStartDate.getFullYear()}`];

for (const key in event) {
if (key.startsWith('RECURRENCE-ID')) {
recurrenceId = event[key];
}
}
if (storedRecored) {
for (const key in storedRecored) {
if (key.startsWith('RECURRENCE-ID')) {
oldRecurrenceId = event[key];
}
}
}

// to add the event to the list, it must be the first time we see it
// or it must be a newer version of the event
if (!storedRecored
|| (recurrenceId && !oldRecurrenceId)
|| (recurrenceId && oldRecurrenceId && Number(recurrenceId.substr(0, 8)) > Number(oldRecurrenceId.substr(0, 8)) && Number(recurrenceId.substr(9, 15)) > Number(oldRecurrenceId.substr(9, 15)))
) {
latestEvents[uid + `${newStartDate.getDate()}-${newStartDate.getMonth()}-${newStartDate.getFullYear()}`] = event;
}
});
// return the values of the object
return Object.values(latestEvents);
},
};
15 changes: 15 additions & 0 deletions widget/utils/nanoid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
let nanoid = (t = 21) =>
crypto
.getRandomValues(new Uint8Array(t))
.reduce(
(t, e) =>
(t +=
(e &= 63) < 36
? e.toString(36)
: e < 62
? (e - 26).toString(36).toUpperCase()
: e > 62
? '-'
: '_'),
''
);

0 comments on commit f7a13c1

Please sign in to comment.