Skip to content

Commit

Permalink
working simple resources
Browse files Browse the repository at this point in the history
  • Loading branch information
awcodes committed Nov 4, 2022
1 parent 9830ec0 commit d21bff3
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 72 deletions.
99 changes: 48 additions & 51 deletions resources/views/components/create-menu.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
@svg('heroicon-o-plus', 'w-4 h-4')
</button>
</x-slot>
<ul @class([
'py-1 space-y-1 overflow-hidden bg-white shadow rounded-xl',
'dark:border-gray-600 dark:bg-gray-700' => config('filament.dark_mode'),
])>
<x-filament::dropdown.list>
@foreach($resources as $resource)
<x-filament::dropdown.item
:color="'secondary'"
Expand All @@ -24,64 +21,64 @@
{{ $resource['label'] }}
</x-filament::dropdown.item>
@endforeach
</ul>
</x-filament::dropdown.list>
</x-filament::dropdown>

<form wire:submit.prevent="callMountedAction">
@php
$action = $this->getMountedAction();
@endphp
<form wire:submit.prevent="callMountedAction">
@php
$action = $this->getMountedAction();
@endphp

<x-filament::modal
id="page-action"
:wire:key="$action ? $this->id . '.actions.' . $action->getName() . '.modal' : null"
:visible="filled($action)"
:width="$action?->getModalWidth()"
:slide-over="$action?->isModalSlideOver()"
display-classes="block"
>
@if ($action)
@if ($action->isModalCentered())
<x-slot name="heading">
{{ $action->getModalHeading() }}
<x-filament::modal
id="quick-create-action"
:wire:key="$action ? $this->id . '.actions.' . $action->getName() . '.modal' : null"
:visible="filled($action)"
:width="$action?->getModalWidth()"
:slide-over="$action?->isModalSlideOver()"
display-classes="block"
>
@if ($action)
@if ($action->isModalCentered())
<x-slot name="heading">
{{ $action->getModalHeading() }}
</x-slot>

@if ($subheading = $action->getModalSubheading())
<x-slot name="subheading">
{{ $subheading }}
</x-slot>
@endif
@else
<x-slot name="header">
<x-filament::modal.heading>
{{ $action->getModalHeading() }}
</x-filament::modal.heading>

@if ($subheading = $action->getModalSubheading())
<x-slot name="subheading">
<x-filament::modal.subheading>
{{ $subheading }}
</x-slot>
</x-filament::modal.subheading>
@endif
@else
<x-slot name="header">
<x-filament::modal.heading>
{{ $action->getModalHeading() }}
</x-filament::modal.heading>

@if ($subheading = $action->getModalSubheading())
<x-filament::modal.subheading>
{{ $subheading }}
</x-filament::modal.subheading>
@endif
</x-slot>
@endif
</x-slot>
@endif

{{ $action->getModalContent() }}
{{ $action->getModalContent() }}

@if ($action->hasFormSchema())
{{ $this->getMountedActionForm() }}
@endif
@if ($action->hasFormSchema())
{{ $this->getMountedActionForm() }}
@endif

@if (count($action->getModalActions()))
<x-slot name="footer">
<x-filament::modal.actions :full-width="$action->isModalCentered()">
@foreach ($action->getModalActions() as $modalAction)
{{ $modalAction }}
@endforeach
</x-filament::modal.actions>
</x-slot>
@endif
@if (count($action->getModalActions()))
<x-slot name="footer">
<x-filament::modal.actions :full-width="$action->isModalCentered()">
@foreach ($action->getModalActions() as $modalAction)
{{ $modalAction }}
@endforeach
</x-filament::modal.actions>
</x-slot>
@endif
</x-filament::modal>
</form>
@endif
</x-filament::modal>
</form>
@endif
</div>
1 change: 0 additions & 1 deletion resources/views/components/quick-create.blade.php

This file was deleted.

12 changes: 4 additions & 8 deletions src/FilamentQuickCreateServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@
namespace FilamentQuickCreate;

use Filament\Facades\Filament;
use Filament\Pages\Actions\Action;
use Filament\Pages\Actions\CreateAction;
use Filament\PluginServiceProvider;
use FilamentQuickCreate\Facades\QuickCreate as Facade;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Str;
use Illuminate\View\View;
use FilamentQuickCreate\Facades\QuickCreate as QuickCreateFacade;
use Illuminate\Support\Facades\Blade;
use Livewire\Livewire;
use Spatie\LaravelPackageTools\Package;

Expand All @@ -25,7 +21,7 @@ public function configurePackage(Package $package): void

public function packageRegistered(): void
{
$this->app->scoped(Facade::class, function () {
$this->app->scoped(QuickCreateFacade::class, function () {
return new QuickCreate();
});

Expand All @@ -38,7 +34,7 @@ public function boot()

Filament::registerRenderHook(
'user-menu.start',
fn (): View => view('filament-quick-create::components.quick-create'),
fn (): string => Blade::render('@livewire(\'quick-create-menu\')'),
);

parent::boot();
Expand Down
123 changes: 111 additions & 12 deletions src/Http/Livewire/QuickCreateMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@
namespace FilamentQuickCreate\Http\Livewire;

use Filament\Pages\Actions\CreateAction;
use Filament\Pages\Concerns\HasActions;
use Filament\Pages\Page;
use Filament\Support\Exceptions\Cancel;
use Filament\Support\Exceptions\Halt;
use FilamentQuickCreate\Facades\QuickCreate as QuickCreateFacade;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Str;
use Illuminate\View\View;
use Livewire\Component;

class QuickCreateMenu extends Page
{
use HasActions;

public $resources;

public function mount(): void
Expand All @@ -25,12 +23,11 @@ public function mount(): void
protected function getActions(): array
{
if ($this->resources) {

return collect($this->resources)
->filter(function($item) {
->filter(function ($item) {
return $item['url'] === null;
})
->transform(function($item) {
->transform(function ($item) {
$resource = App::make($item['resource_name']);
$listResource = invade(App::make($resource->getPages()['index']['class']));
$form = $listResource->getCreateFormSchema();
Expand All @@ -46,9 +43,105 @@ protected function getActions(): array
return [];
}

public function render(): View
public function callMountedAction(?string $arguments = null)
{
return view('filament-quick-create::components.create-menu');
$action = $this->getMountedAction();

if (! $action) {
return;
}

if ($action->isDisabled()) {
return;
}

$action->arguments($arguments ? json_decode($arguments, associative: true) : []);

$form = $this->getMountedActionForm();

$result = null;

try {
if ($action->hasForm()) {
$action->callBeforeFormValidated();

$action->formData($form->getState());

$action->callAfterFormValidated();
}

$action->callBefore();

$result = $action->call([
'form' => $form,
]);

$result = $action->callAfter() ?? $result;
} catch (Halt $exception) {
return;
} catch (Cancel $exception) {
}

$this->mountedAction = null;

$action->resetArguments();
$action->resetFormData();

$this->dispatchBrowserEvent('close-modal', [
'id' => 'quick-create-action',
]);

return $result;
}

public function mountAction(string $name)
{
$this->mountedAction = $name;

$action = $this->getMountedAction();

if (! $action) {
return;
}

if ($action->isDisabled()) {
return;
}

$this->cacheForm(
'mountedActionForm',
fn () => $this->getMountedActionForm(),
);

try {
if ($action->hasForm()) {
$action->callBeforeFormFilled();
}

$action->mount([
'form' => $this->getMountedActionForm(),
]);

if ($action->hasForm()) {
$action->callAfterFormFilled();
}
} catch (Halt $exception) {
return;
} catch (Cancel $exception) {
$this->mountedAction = null;

return;
}

if (! $action->shouldOpenModal()) {
return $this->callMountedAction();
}

$this->resetErrorBag();

$this->dispatchBrowserEvent('open-modal', [
'id' => 'quick-create-action',
]);
}

public function getFilamentResources(): array
Expand All @@ -60,13 +153,14 @@ public function getFilamentResources(): array
->map(function ($resourceName) {
$resource = App::make($resourceName);
if ($resource->canCreate()) {
$actionName = 'create' . Str::of($resource->getLabel())->camel();
$actionName = 'create'.Str::of($resource->getLabel())->camel();

return [
'resource_name' => $resourceName,
'label' => Str::ucfirst($resource->getModelLabel()),
'icon' => invade($resource)->getNavigationIcon(),
'action_name' => $actionName,
'action' => ! $resource->hasPage('create') ? 'mountAction(\'' . $actionName . '\')' : null,
'action' => ! $resource->hasPage('create') ? 'mountAction(\''.$actionName.'\')' : null,
'url' => $resource->hasPage('create') ? $resource::getUrl('create') : null,
];
}
Expand All @@ -79,4 +173,9 @@ public function getFilamentResources(): array

return array_filter($resources);
}
}

public function render(): View
{
return view('filament-quick-create::components.create-menu');
}
}

0 comments on commit d21bff3

Please sign in to comment.