Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: lazy loading the events data #17

Merged
merged 6 commits into from
Jul 1, 2022

Conversation

wychoong
Copy link
Contributor

this PR allows lazy loading of calendar events data by implementing LazyLoading interface to calendar widget

use Saade\FilamentFullCalendar\Widgets\Contracts\LazyLoading;

class SchedulingCalendarWidget extends FullCalendarWidget implements LazyLoading
{

    // implement `lazyLoadViewData` instead of `getViewData`
    public function lazyLoadViewData($fetchInfo = null): array
    {
        $schedules = Appointment::query()
            ->when($fetchInfo, function (Builder $query) use ($fetchInfo) {
                $startDate = Carbon::parse($fetchInfo['start']);
                $endDate = Carbon::parse($fetchInfo['end']);

                $query->where([
                    ['start_at', '>=', $startDate],
                    ['end_at', '<', $endDate],
                ]);
            }, function (Builder $query) {
                $query->where( .... );
            })
            ->get();

        $data = $schedules->map( ... );

        return $data;
    }
 }

@wychoong
Copy link
Contributor Author

@saade I'm not sure on the initial load, should the calendar data being loaded or let the ajax handle fully

data loaded on initial
pros: one less ajax and events can show immediately
cons: unable to scope the query to only fetch days in current view

data load after calendar render
pros: can scope query to fetch only days in current view
cons: calendar will be empty on first load

@saade
Copy link
Owner

saade commented Jul 1, 2022

thanks! couple of changes:

1 - lazyLoadViewData is now fetchEvents, it receives $ignorableIds that you should filter on your query instead of caching it on the client side (this is better for the database as you avoid querying already rendered events). I've added an example to the docs.

2 - getViewData remains and its used to render static events on page render, after the page load, it will call fetchEvents where you can filter events based on the calendar view.

both getViewData and fetchEvents are optional, you can use one or another or both to better fit your needs

3 - refreshEvents does not keep old events on the calendar. as the name implies, it should refresh events with a new set of events.

let me know if you have any questions or found some bugs. made my best to keep all working properly!

@saade saade merged commit 6e1d120 into saade:main Jul 1, 2022
@wychoong
Copy link
Contributor Author

wychoong commented Jul 1, 2022

LazyLoading interface can be removed I believe

@wychoong wychoong deleted the feat/paginate-calendar-events-data branch July 3, 2022 16:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants