javascriptjqueryeventscalendar

EventCalander - Stop overlapping event on load


I'm using this script https://github.com/vkurko/calendar to create an event calendar. It seems to be working well, but I'm trying to work out how to stop an event being added if will overlap another event.

I've created a simple codepen, https://codepen.io/Tom_T/pen/emYjbJm

When you go to Week view there is one existing event. If you click load at the bottom of the page it adds a new event which overlaps the existing one. I want to stop this happening, if there is already an event then don't add the new one.

The code I'm using to check if there is an existing event is:

newevent = data[i]

    var start = new Date(newevent.start);
    var end = new Date(newevent.end);
    console.log ( 'TEST: ' + start + ' --- ' + end );
    console.log ( ec.getEvents().filter(e => e.start < newevent.end && newevent.start < e.end ) )

If the result of the filter is > 0 then don't add the event. At the moment it is only returning 0 so the event is added.

Does anyone know how I can filter the existing events to see if there are any clashing/overlapping entries ?

Thanks


Solution

  • I've got this working using the following:

    newevent = data[i]
    var start = new Date(newevent.start);
    var end = new Date(newevent.end);
    result = ec.getEvents().filter(e => e.start < end && start < e.end )
    if ( result.length > 0 ) { 
        continue
    }