From dc662f40d1b61522c5468f4591dfe17e1554eaed Mon Sep 17 00:00:00 2001 From: Jean-Philippe THEVENOUX Date: Fri, 8 Mar 2024 21:27:41 +0100 Subject: [PATCH] Install Filament/actions Replace wire-element/modal by filamentphp/actions --- app/Livewire/Dashboard.php | 59 ++++++++++- app/Livewire/EditNote.php | 72 ------------- app/Models/Category.php | 2 +- app/Models/Note.php | 4 +- app/Models/Poste.php | 2 +- composer.json | 7 +- composer.lock | 102 ++++--------------- resources/views/components/section.blade.php | 2 +- resources/views/layouts/app.blade.php | 1 - resources/views/livewire/dashboard.blade.php | 9 +- resources/views/livewire/edit-note.blade.php | 51 ---------- 11 files changed, 90 insertions(+), 221 deletions(-) delete mode 100644 app/Livewire/EditNote.php delete mode 100644 resources/views/livewire/edit-note.blade.php diff --git a/app/Livewire/Dashboard.php b/app/Livewire/Dashboard.php index f0ffbd0..532a1c1 100644 --- a/app/Livewire/Dashboard.php +++ b/app/Livewire/Dashboard.php @@ -3,13 +3,26 @@ namespace App\Livewire; use App\Models\Category; +use App\Models\Note; +use Filament\Actions\Action; +use Filament\Actions\Concerns\InteractsWithActions; +use Filament\Actions\Contracts\HasActions; +use Filament\Actions\CreateAction; +use Filament\Forms\Components\Select; +use Filament\Forms\Components\TextInput; +use Filament\Forms\Concerns\InteractsWithForms; +use Filament\Forms\Contracts\HasForms; +use Filament\Support\Enums\ActionSize; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Relations\HasMany; use Livewire\Attributes\Url; use Livewire\Component; -class Dashboard extends Component +class Dashboard extends Component implements HasActions, HasForms { + use InteractsWithActions; + use InteractsWithForms; + public $years; public $months; @@ -20,9 +33,47 @@ class Dashboard extends Component #[Url(keep: true)] public int $month; - public $listeners = [ - 'noteCreated' => 'render', - ]; + public function addToCategoryAction(): Action + { + return CreateAction::make('addToCategory') + ->model(Note::class) + ->form(function (array $arguments) { + $category = Category::with('postes')->find($arguments['category']); + $fields = [ + TextInput::make('price') + ->numeric() + ->inputMode('decimal') + ->required(), + TextInput::make('label') + ->required($category->postes->count() == 0) + ->maxLength(255), + ]; + // TODO : insert in 2nd place + if ($category->postes->count() > 0) { + $select = Select::make('poste_id') + ->required() + ->label('Poste') + ->options($category->postes->pluck('label', 'id')); + array_splice($fields, 1, 0, [$select]); + } + + return $fields; + }) + ->label('Add note') + ->labeledFrom('md') + ->button() + ->outlined() + ->size(ActionSize::Small) + ->icon('heroicon-m-plus-circle') + ->mutateFormDataUsing(function (array $data, array $arguments): array { + $data['user_id'] = auth()->id(); + $data['year'] = $this->year; + $data['month'] = $this->month; + $data['category_id'] = $arguments['category']; + + return $data; + }); + } public function mount() { diff --git a/app/Livewire/EditNote.php b/app/Livewire/EditNote.php deleted file mode 100644 index d4009ee..0000000 --- a/app/Livewire/EditNote.php +++ /dev/null @@ -1,72 +0,0 @@ - 'required|decimal:0,2', - 'poste' => Rule::requiredIf($this->category->postes->isNotEmpty()), - 'label' => Rule::requiredIf($this->category->postes->isEmpty()), - ]; - } - - public function render() - { - return view('livewire.edit-note'); - } - - public function save() - { - $this->validate(); - - $note = new Note(); - $note->year = $this->year; - $note->month = $this->month; - $note->category_id = $this->category->id; - $note->price = $this->price; - - $note->poste_id = $this->poste; - $note->label = $this->label; - $note->user_id = Auth()->id(); - - $note->save(); - $this->closeModalWithEvents([Dashboard::class => 'noteCreated']); - - Notification::make() - ->title('Note ajoutée') - ->success() - ->send(); - } - - public static function closeModalOnClickAway(): bool - { - return false; - } -} diff --git a/app/Models/Category.php b/app/Models/Category.php index f4c6eb2..cbcc694 100644 --- a/app/Models/Category.php +++ b/app/Models/Category.php @@ -13,7 +13,7 @@ class Category extends Model /** * The attributes that are mass assignable. * - * @var array + * @var array */ protected $fillable = ['label', 'credit']; diff --git a/app/Models/Note.php b/app/Models/Note.php index a04aef3..7b07aab 100644 --- a/app/Models/Note.php +++ b/app/Models/Note.php @@ -14,9 +14,9 @@ class Note extends Model /** * The attributes that are mass assignable. * - * @var array + * @var array */ - protected $fillable = ['label', 'price', 'year', 'month', 'category_id', 'poste_id']; + protected $fillable = ['label', 'price', 'year', 'month', 'category_id', 'poste_id', 'user_id']; protected $casts = [ 'price' => MoneyCast::class, diff --git a/app/Models/Poste.php b/app/Models/Poste.php index 74901b8..fae8bcd 100644 --- a/app/Models/Poste.php +++ b/app/Models/Poste.php @@ -14,7 +14,7 @@ class Poste extends Model /** * The attributes that are mass assignable. * - * @var array + * @var array */ protected $fillable = ['label', 'category_id']; diff --git a/composer.json b/composer.json index 7559e2b..36417ae 100644 --- a/composer.json +++ b/composer.json @@ -9,14 +9,14 @@ "license": "MIT", "require": { "php": "^8.2", + "filament/actions": "^3.2", "filament/notifications": "^v3.2.44", "guzzlehttp/guzzle": "^7.8.1", "laravel/framework": "v10.47.0", "laravel/sanctum": "^v3.3.3", "laravel/tinker": "^v2.9.0", "livewire/livewire": "v3.4.7", - "livewire/volt": "^v1.6.2", - "wire-elements/modal": "^2.0.9" + "livewire/volt": "^v1.6.2" }, "require-dev": { "barryvdh/laravel-debugbar": "v3.10.6", @@ -46,7 +46,8 @@ "scripts": { "post-autoload-dump": [ "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump", - "@php artisan package:discover --ansi" + "@php artisan package:discover --ansi", + "@php artisan filament:upgrade" ], "post-update-cmd": [ "@php artisan vendor:publish --tag=laravel-assets --ansi --force" diff --git a/composer.lock b/composer.lock index 3936266..2b29a53 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "5528db445933c94a56c0dc87cb0efbbe", + "content-hash": "dfb3a24c779c2a867b466efb66066970", "packages": [ { "name": "anourvalar/eloquent-serialize", @@ -1114,16 +1114,16 @@ }, { "name": "filament/actions", - "version": "v3.2.44", + "version": "v3.2.45", "source": { "type": "git", "url": "https://github.com/filamentphp/actions.git", - "reference": "e64667172371b0993c28032eeb34dd6fc63cc603" + "reference": "3a859410aaf76fb5bf1c549da9cca2ac652f26f5" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/filamentphp/actions/zipball/e64667172371b0993c28032eeb34dd6fc63cc603", - "reference": "e64667172371b0993c28032eeb34dd6fc63cc603", + "url": "https://api.github.com/repos/filamentphp/actions/zipball/3a859410aaf76fb5bf1c549da9cca2ac652f26f5", + "reference": "3a859410aaf76fb5bf1c549da9cca2ac652f26f5", "shasum": "" }, "require": { @@ -1163,20 +1163,20 @@ "issues": "https://github.com/filamentphp/filament/issues", "source": "https://github.com/filamentphp/filament" }, - "time": "2024-03-05T14:33:19+00:00" + "time": "2024-03-07T13:17:38+00:00" }, { "name": "filament/forms", - "version": "v3.2.44", + "version": "v3.2.45", "source": { "type": "git", "url": "https://github.com/filamentphp/forms.git", - "reference": "de893403d9fd18c5b34dbec1579e387721477e3c" + "reference": "c379e41ecfdd66fc2b50fa7db591ab118c4f6de7" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/filamentphp/forms/zipball/de893403d9fd18c5b34dbec1579e387721477e3c", - "reference": "de893403d9fd18c5b34dbec1579e387721477e3c", + "url": "https://api.github.com/repos/filamentphp/forms/zipball/c379e41ecfdd66fc2b50fa7db591ab118c4f6de7", + "reference": "c379e41ecfdd66fc2b50fa7db591ab118c4f6de7", "shasum": "" }, "require": { @@ -1219,20 +1219,20 @@ "issues": "https://github.com/filamentphp/filament/issues", "source": "https://github.com/filamentphp/filament" }, - "time": "2024-03-05T14:33:15+00:00" + "time": "2024-03-07T13:17:40+00:00" }, { "name": "filament/infolists", - "version": "v3.2.44", + "version": "v3.2.45", "source": { "type": "git", "url": "https://github.com/filamentphp/infolists.git", - "reference": "9d13f101531ef920961967f1e4501540a22e1659" + "reference": "cca6545710362cdd42e349defb7d7bb4b21a7de3" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/filamentphp/infolists/zipball/9d13f101531ef920961967f1e4501540a22e1659", - "reference": "9d13f101531ef920961967f1e4501540a22e1659", + "url": "https://api.github.com/repos/filamentphp/infolists/zipball/cca6545710362cdd42e349defb7d7bb4b21a7de3", + "reference": "cca6545710362cdd42e349defb7d7bb4b21a7de3", "shasum": "" }, "require": { @@ -1270,11 +1270,11 @@ "issues": "https://github.com/filamentphp/filament/issues", "source": "https://github.com/filamentphp/filament" }, - "time": "2024-03-04T12:09:37+00:00" + "time": "2024-03-07T13:17:47+00:00" }, { "name": "filament/notifications", - "version": "v3.2.44", + "version": "v3.2.45", "source": { "type": "git", "url": "https://github.com/filamentphp/notifications.git", @@ -1326,16 +1326,16 @@ }, { "name": "filament/support", - "version": "v3.2.44", + "version": "v3.2.45", "source": { "type": "git", "url": "https://github.com/filamentphp/support.git", - "reference": "1f5ba74e8755f5a11c0bf53907d8d9ed1f8d868d" + "reference": "f60a06fc11ce0520bf8296c350fa6e3ba2556632" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/filamentphp/support/zipball/1f5ba74e8755f5a11c0bf53907d8d9ed1f8d868d", - "reference": "1f5ba74e8755f5a11c0bf53907d8d9ed1f8d868d", + "url": "https://api.github.com/repos/filamentphp/support/zipball/f60a06fc11ce0520bf8296c350fa6e3ba2556632", + "reference": "f60a06fc11ce0520bf8296c350fa6e3ba2556632", "shasum": "" }, "require": { @@ -1379,7 +1379,7 @@ "issues": "https://github.com/filamentphp/filament/issues", "source": "https://github.com/filamentphp/filament" }, - "time": "2024-03-05T14:33:52+00:00" + "time": "2024-03-07T13:18:04+00:00" }, { "name": "fruitcake/php-cors", @@ -7507,64 +7507,6 @@ "source": "https://github.com/webmozarts/assert/tree/1.11.0" }, "time": "2022-06-03T18:03:27+00:00" - }, - { - "name": "wire-elements/modal", - "version": "2.0.9", - "source": { - "type": "git", - "url": "https://github.com/wire-elements/modal.git", - "reference": "899b05e313403669aa8a359db71a066246184355" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/wire-elements/modal/zipball/899b05e313403669aa8a359db71a066246184355", - "reference": "899b05e313403669aa8a359db71a066246184355", - "shasum": "" - }, - "require": { - "livewire/livewire": "^3.2.3", - "php": "^8.1", - "spatie/laravel-package-tools": "^1.9" - }, - "require-dev": { - "orchestra/testbench": "^8.5", - "phpunit/phpunit": "^9.5" - }, - "type": "library", - "extra": { - "laravel": { - "providers": [ - "LivewireUI\\Modal\\LivewireModalServiceProvider" - ] - } - }, - "autoload": { - "psr-4": { - "LivewireUI\\Modal\\": "src" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Philo Hermans", - "email": "me@philohermans.com" - } - ], - "description": "Laravel Livewire modal component", - "keywords": [ - "laravel", - "livewire", - "modal" - ], - "support": { - "issues": "https://github.com/wire-elements/modal/issues", - "source": "https://github.com/wire-elements/modal/tree/2.0.9" - }, - "time": "2023-12-08T09:31:14+00:00" } ], "packages-dev": [ diff --git a/resources/views/components/section.blade.php b/resources/views/components/section.blade.php index d0811e8..f54ad3a 100644 --- a/resources/views/components/section.blade.php +++ b/resources/views/components/section.blade.php @@ -1,6 +1,6 @@
@if (isset($header)) -
+
{{ $header }}
@endif diff --git a/resources/views/layouts/app.blade.php b/resources/views/layouts/app.blade.php index fb2df8c..7547bd3 100644 --- a/resources/views/layouts/app.blade.php +++ b/resources/views/layouts/app.blade.php @@ -42,7 +42,6 @@ @livewire('notifications') - @livewire('livewire-ui-modal') @filamentScripts diff --git a/resources/views/livewire/dashboard.blade.php b/resources/views/livewire/dashboard.blade.php index 53da6fd..08bdedb 100644 --- a/resources/views/livewire/dashboard.blade.php +++ b/resources/views/livewire/dashboard.blade.php @@ -24,15 +24,12 @@ class="bg-gray-50 border border-l-0 border-gray-300 text-gray-900 text-sm rounde
-
+
@foreach ($categories as $category)

{{ $category->label }}

- {{ __('Add note') }} + {{ ($this->addToCategoryAction)(['category' => $category->id]) }} {{ Number::currency(($category->notes_sum_price ?? 0) / 100, 'EUR', 'fr') }} @@ -52,4 +49,6 @@ class="bg-gray-50 border border-l-0 border-gray-300 text-gray-900 text-sm rounde
@endforeach
+ +
diff --git a/resources/views/livewire/edit-note.blade.php b/resources/views/livewire/edit-note.blade.php deleted file mode 100644 index eccfae7..0000000 --- a/resources/views/livewire/edit-note.blade.php +++ /dev/null @@ -1,51 +0,0 @@ -
- - -

Add Note to {{ $category->label }}

-
- -
- - @error('price') - {{ $message }} - @enderror - - - - - @if ($category->postes->count() > 0) - @error('poste') - {{ $message }} - @enderror - - - @endif - - @error('label') - {{ $message }} - @enderror - - -
- - - - {{ __('Cancel') }} - - - - {{ __('Add note') }} - - -
-