GuavaCZ / calendar

MIT License
140 stars 13 forks source link

[Bug]: Context Menu Actions not working #30

Closed PrasadChinwal closed 2 months ago

PrasadChinwal commented 2 months ago

What happened?

I am trying to implement the edit and delete actions when clicked on an event. I see the context menu with my view, edit and delete actions, unfortunately when I click them nothing happens (I briefly see the button disable).

Weirdly I have a create action on the getDateClickContextMenuActions which works perfectly.

How to reproduce the bug

<?php

namespace App\Livewire;

use App\Models\Appointment;
use Carbon\Carbon;
use Carbon\CarbonPeriod;
use Filament\Forms\Components\DateTimePicker;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Guava\Calendar\Actions\CreateAction;
use Guava\Calendar\Actions\EditAction;
use Guava\Calendar\Actions\ViewAction;
use Guava\Calendar\ValueObjects\Event;
use Guava\Calendar\Widgets\CalendarWidget;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;

class InstructorSchedule extends CalendarWidget
{
    protected string $calendarView = 'timeGridWeek';

    protected bool $dateClickEnabled = true;

    protected bool $eventClickEnabled = true;

    protected ?string $defaultEventClickAction = 'edit';

    public ?Model $record = null;

    public function getEvents(array $fetchInfo = []): Collection|array
    {
        return [
            // Your events
        ];
    }

    public function getHeaderActions(): array
    {
        return [
            CreateAction::make('createAppointment')
                ->icon('heroicon-o-plus-circle')
                ->model(Appointment::class),
        ];
    }

    public function getDateClickContextMenuActions(): array
    {
        return [
            CreateAction::make()
                ->model(Appointment::class)
                ->mountUsing(function ($arguments, $form) {
                    return $form->fill([
                        'start_at' => data_get($arguments, 'dateStr'),
                        'end_at' => data_get($arguments, 'dateStr'),
                    ]);
                }),
        ];
    }

    // NOT WORKING
    public function getEventClickContextMenuActions(): array
    {
        return [
            ViewAction::make('view_event')
                ->label('My View')
                ->model(Appointment::class)
                ->form($this->getSchema()),
            EditAction::make('editAppointment')
                ->record($this->eventRecord)
                ->model(Appointment::class),
            $this->deleteAction()
                ->model(Appointment::class)
                ->requiresConfirmation(),
        ];
    }

    public function getSchema(?string $model = null): ?array
    {
        return [
            TextInput::make('title'),
            Select::make('appointment_type_id')
                ->relationship('appointmentType', 'name')
                ->searchable()
                ->preload()
                ->required(),
            Select::make('instructor_id')
                ->relationship('instructor', 'netid')
                ->searchable()
                ->preload()
                ->required(),
            Select::make('course_id')
                ->relationship('course', 'name')
                ->searchable()
                ->preload()
                ->required(),
            DateTimePicker::make('start_at')
                ->required(),
            DateTimePicker::make('end_at')
                ->required(),
        ];
    }
}

Package Version

1.6.0

PHP Version

8.2

Laravel Version

11.20

Which operating systems does with happen with?

macOS, Windows

Notes

Screenshot 2024-09-03 at 8 16 57 PM
lukas-frey commented 2 months ago

https://github.com/GuavaCZ/calendar?tab=readme-ov-file#authorization

Check this and follow the instructions there

hrodrigues-belodigital commented 2 months ago

Had the same issue here. Overwriting the authorize method solved it. Thank you @lukas-frey .

PrasadChinwal commented 2 months ago

@lukas-frey @hrodrigues-belodigital Yes the solution works for some events in the calendar. It works for the events I retrieve from Model (Blue and Green events in screenshots) but doesn't work for events of type Guava\Calendar\ValueObjects\Event (Orange events in screenshots).

Here is my code for the events:

public function getEvents(array $fetchInfo = []): Collection|array
    {
        $availability = collect();
        $schedule = \App\Models\InstructorSchedule::query()
            ->where('instructor_id', 5)
            ->get();
        $start = Carbon::parse($fetchInfo['startStr']);
        $end = Carbon::parse($fetchInfo['endStr']);
        $week = CarbonPeriod::between($start, $end);
        $week->forEach(function (Carbon $date) use ($schedule, $availability) {
            foreach ($schedule as $scheduleItem) {
                if ((int) $scheduleItem->day === $date->dayOfWeek()) {
                    $availability->push(
                        Event::make()
                            ->title('Available')
                            ->backgroundColor('#fbbf24')
                            ->textColor('#78350f')
                            ->start(Carbon::parse($date)->setTimeFromTimeString($scheduleItem->start_at))
                            ->end(Carbon::parse($date)->setTimeFromTimeString($scheduleItem->end_at))
                    );
                }
            }
        });

        return collect()
            ->push(...$availability) // menu doesn't work for these events
            ->push(...Appointment::whereRelation('instructor', 'id', 5) // menu works for these events
                ->where('start_at', '>=', $start)
                ->where('end_at', '<=', $end)
                ->get()
            );
    }
lukas-frey commented 2 months ago

Yeah, it can't work for that. It's not a model so there's nothing to edit.

PrasadChinwal commented 2 months ago

@lukas-frey that totally makes sense. Thanks for the help.