diff --git a/src/Widgets/Concerns/CanManageEvents.php b/src/Widgets/Concerns/CanManageEvents.php index 1c2312f..1a755e1 100644 --- a/src/Widgets/Concerns/CanManageEvents.php +++ b/src/Widgets/Concerns/CanManageEvents.php @@ -50,16 +50,17 @@ public function onEventClick($event): void return; } - $this->editEventForm - ->disabled(! static::canEdit($event)) - ->fill($event); - if (method_exists($this, 'resolveEventRecord')) { $this->event = $this->resolveEventRecord($event); + $this->editEventForm->model($this->event); } else { $this->event_id = $event['id'] ?? null; } + $this->editEventForm + ->disabled(! static::canEdit($event)) + ->fill($this->event?->getAttributes() ?? $event); + $this->dispatchBrowserEvent('open-modal', ['id' => 'fullcalendar--edit-event-modal']); } diff --git a/src/Widgets/Forms/CreateEventForm.php b/src/Widgets/Forms/CreateEventForm.php index 706302b..91fd08b 100644 --- a/src/Widgets/Forms/CreateEventForm.php +++ b/src/Widgets/Forms/CreateEventForm.php @@ -3,6 +3,7 @@ namespace Saade\FilamentFullCalendar\Widgets\Forms; use Filament\Forms; +use Illuminate\Database\Eloquent\Model; trait CreateEventForm { @@ -10,14 +11,19 @@ trait CreateEventForm public function onCreateEventSubmit() { - $this->createEvent($this->createEventForm->getState()); + $eventModel = $this->createEvent($this->createEventForm->getState()); + if ($eventModel) { + $this->createEventForm->model($eventModel); + $this->createEventForm->saveRelationships(); + } $this->dispatchBrowserEvent('close-modal', ['id' => 'fullcalendar--create-event-modal']); } - public function createEvent(array $data): void + public function createEvent(array $data): ?Model { // Override this function and do whatever you want with $data + return null; } protected static function getCreateEventFormSchema(): array @@ -36,6 +42,7 @@ protected function getCreateEventForm(): array { return [ 'createEventForm' => $this->makeForm() + ->model($this->getFormModel()::getModel()) ->schema(static::getCreateEventFormSchema()) ->statePath('createEventFormState'), ]; diff --git a/src/Widgets/Forms/EditEventForm.php b/src/Widgets/Forms/EditEventForm.php index 90b808c..ca47b7b 100644 --- a/src/Widgets/Forms/EditEventForm.php +++ b/src/Widgets/Forms/EditEventForm.php @@ -36,6 +36,7 @@ protected function getEditEventForm(): array { return [ 'editEventForm' => $this->makeForm() + ->model($this->getFormModel()) ->schema(static::getEditEventFormSchema()) ->statePath('editEventFormState'), ];