nova-calendar icon indicating copy to clipboard operation
nova-calendar copied to clipboard

Cannot change url by method `withUrl()`, when events are created by generator

Open webard opened this issue 10 months ago • 3 comments

Hello,

In my app I have calendar_events table with all calendar events morphed to many other models.

My generator look like this:

class CrmCalendarEventGenerator extends CustomEventGenerator
{
    protected function modelQuery(EloquentBuilder $queryBuilder, Carbon $startOfCalendar, Carbon $endOfCalendar): EloquentBuilder
    {
        return $queryBuilder->with(['eventable'])->where('start', '>=', $startOfCalendar)
            ->where(function ($q) use ($endOfCalendar) {
                $q->where('end', '<=', $endOfCalendar)
                    ->orWhereNull('end');
            })->where('type', 'recall');
    }

    /**
     * @return array<Event>
     */
    protected function resourceToEvents(NovaResource $resource, Carbon $startOfCalendar, Carbon $endOfCalendar): array
    {
        $model = $resource->model();
        assert($model instanceof CalendarEvent);
        assert($model->eventable instanceof HasCalendarEvents);
        $event = new Event($model->eventable->getCalendarTitle(), $model->start);

        $event->notes($model->description);
        $event->addStyle($model->style ?? 'default');

        // TODO: not working
        $event->withUrl('/resources/' . Str::plural($model->eventable_type) . '/' . $model->eventable_id);

        return [$event];

    }
}

And like you see, there is no option to change url by method withUrl(), when events are created by generator. Calendar Event always redirects to CalendarEvent model, instead of my customized URL.

webard avatar Mar 28 '24 14:03 webard

Mhm, I see what you mean.

As you can see in the loadAllEvents method of the AbstractCalendarDataProvider of this package, the event url is being overwritten after the event generator has generated the event, so your custom URL is being overwritten.

I understand that this is suboptimal and I agree that what you expect to work, should work like that, but currently it doesn't.

A fix for now, would be to implement the urlForResource method in your CalendarDataProvider, and move your URL calculation logic to there. I think it will then work as you want.

Let me know! :)

wdelfuego avatar Mar 28 '24 14:03 wdelfuego

Hi,

yes, urlForResource() works great:

protected function urlForResource(NovaResource $resource): string
    {
        $model = $resource->model();
        assert($model instanceof CalendarEvent);

        return '/resources/' . Str::plural($model->eventable_type) . '/' . $model->eventable_id;
    }

but I think it is little unintuitive, when all other data is set in resourceToEvents() method.

https://github.com/wdelfuego/nova-calendar/blob/efcafb0bd3c5e5249ddb5b68d2e255713abe29a2/src/DataProvider/AbstractCalendarDataProvider.php#L293-L299

Can we move this logic to Event, and use urlForResource as default url until user set own url?

webard avatar Mar 30 '24 08:03 webard

Hi, yes, I agree, this needs a little refactoring. I'll leave this issue open until I get around to releasing the improvement so others can find the work-around easily.

wdelfuego avatar Apr 18 '24 16:04 wdelfuego