From e70bb35bea13875e90cb37afccb96c25d1d004e0 Mon Sep 17 00:00:00 2001 From: ChimpGamer Date: Tue, 17 Dec 2024 20:27:18 +0100 Subject: [PATCH] Updated UltimateTags addon to use powergrid. --- .../Controllers/UltimateTagsController.php | 11 +++ addons/UltimateTags/Livewire/ShowTags.php | 64 +++++++------ addons/UltimateTags/Livewire/TagsTable.php | 95 +++++++++++++++++++ .../views/livewire/show-tags.blade.php | 64 +------------ 4 files changed, 148 insertions(+), 86 deletions(-) create mode 100644 addons/UltimateTags/Livewire/TagsTable.php diff --git a/addons/UltimateTags/App/Http/Controllers/UltimateTagsController.php b/addons/UltimateTags/App/Http/Controllers/UltimateTagsController.php index 3e1613c..25e7741 100644 --- a/addons/UltimateTags/App/Http/Controllers/UltimateTagsController.php +++ b/addons/UltimateTags/App/Http/Controllers/UltimateTagsController.php @@ -8,6 +8,17 @@ class UltimateTagsController extends Controller { + + /** + * Create a new controller instance. + * + * @return void + */ + public function __construct() + { + $this->middleware('auth'); + } + /** * Display a listing of the resource. */ diff --git a/addons/UltimateTags/Livewire/ShowTags.php b/addons/UltimateTags/Livewire/ShowTags.php index 8a727c6..4f8daea 100644 --- a/addons/UltimateTags/Livewire/ShowTags.php +++ b/addons/UltimateTags/Livewire/ShowTags.php @@ -3,15 +3,14 @@ namespace Addons\UltimateTags\Livewire; use Addons\UltimateTags\App\Models\Tag; +use Illuminate\Contracts\View\Factory; +use Illuminate\Contracts\View\View; +use Illuminate\Foundation\Application; +use Livewire\Attributes\On; use Livewire\Component; -use Livewire\WithPagination; class ShowTags extends Component { - use WithPagination; - - protected string $paginationTheme = 'bootstrap'; - public int $tagId; public ?string $name; @@ -24,8 +23,6 @@ class ShowTags extends Component public ?string $server; - public string $search = ''; - protected array $rules = [ 'name' => 'required|string|exists:Addons\UltimateTags\App\Models\Tag,name', 'tag' => 'required|string', @@ -34,19 +31,12 @@ class ShowTags extends Component 'server' => 'string|nullable', ]; - public function updated($name, $value): void - { - if ($name == 'search') { - $this->resetPage(); - } - } - - public function addTag() + public function addTag(): void { $this->resetInput(); } - public function createTag() + public function createTag(): void { // name has to be unique. $validatedData = $this->validate([ @@ -72,10 +62,19 @@ public function createTag() ]); session()->flash('message', 'Successfully Added Tag'); $this->closeModal('addTagModal'); + $this->refreshTable(); } - public function editTag(Tag $tag) + #[On('edit')] + public function editTag($rowId): void { + $tag = Tag::find($rowId); + if ($tag == null) { + session()->flash('error', 'Tag #'.$rowId.' not found'); + + return; + } + $this->tagId = $tag->id; $this->name = $tag->name; $this->tag = $tag->tag; @@ -84,7 +83,7 @@ public function editTag(Tag $tag) $this->server = $tag->server; } - public function updateTag() + public function updateTag(): void { $validatedData = $this->validate(); @@ -103,21 +102,31 @@ public function updateTag() ]); session()->flash('message', 'Tag Updated Successfully'); $this->closeModal('editTagModal'); + $this->refreshTable(); } - public function deleteTag(Tag $tag) + #[On('delete')] + public function deleteTag($rowId): void { + $tag = Tag::find($rowId); + if ($tag == null) { + session()->flash('error', 'Tag #'.$rowId.' not found'); + + return; + } + $this->tagId = $tag->id; $this->name = $tag->name; } - public function delete() + public function delete(): void { Tag::find($this->tagId)->delete(); $this->closeModal('deleteTagModal'); + $this->refreshTable(); } - public function closeModal(?string $modalId = null) + public function closeModal(?string $modalId = null): void { $this->resetInput(); if ($modalId != null) { @@ -125,7 +134,7 @@ public function closeModal(?string $modalId = null) } } - public function resetInput() + public function resetInput(): void { $this->tagId = -1; $this->name = null; @@ -135,12 +144,13 @@ public function resetInput() $this->server = null; } - public function render() + private function refreshTable(): void { - $tags = Tag::where('name', 'like', '%'.$this->search.'%') - ->orWhere('server', 'like', '%'.$this->search.'%') - ->orderBy('id', 'DESC')->paginate(10); + $this->dispatch('pg:eventRefresh-tags-table'); + } - return view('ultimatetags::livewire.show-tags')->with('tags', $tags); + public function render(): View|Factory|Application + { + return view('ultimatetags::livewire.show-tags'); } } diff --git a/addons/UltimateTags/Livewire/TagsTable.php b/addons/UltimateTags/Livewire/TagsTable.php new file mode 100644 index 0000000..27f51b1 --- /dev/null +++ b/addons/UltimateTags/Livewire/TagsTable.php @@ -0,0 +1,95 @@ +showSearchInput(), + PowerGrid::footer() + ->showPerPage() + ->showRecordCount(), + ]; + } + + public function datasource(): Builder + { + return Tag::query(); + } + + public function fields(): PowerGridFields + { + return PowerGrid::fields() + ->add('id') + ->add('name') + ->add('tag_formatted', fn (Tag $model) => $model->tag) + ->add('description') + ->add('permission') + ->add('server'); + } + + public function columns(): array + { + return [ + Column::make('Id', 'id') + ->sortable() + ->searchable(), + + Column::make('Name', 'name') + ->sortable() + ->searchable(), + + Column::make('Tag', 'tag') + ->sortable() + ->searchable(), + + Column::make('Description', 'description') + ->sortable() + ->searchable(), + + Column::make('Permission', 'permission') + ->sortable() + ->searchable(), + + Column::make('Server', 'server') + ->sortable() + ->searchable(), + + Column::action('Action') + ->headerAttribute('text-center'), + ]; + } + + public function actions(Tag $row): array + { + return [ + Button::add('edit') + ->attributes(['data-mdb-ripple-init' => '', 'data-mdb-modal-init' => '', 'data-mdb-target' => '#editTagModal']) + ->slot('edit') + ->can(auth()->user()->can('edit_tags')) + ->id() + ->class('bg-transparent border-0') + ->dispatch('edit', ['rowId' => $row->id]), + Button::add('delete') + ->attributes(['data-mdb-ripple-init' => '', 'data-mdb-modal-init' => '', 'data-mdb-target' => '#deleteTagModal']) + ->slot('delete') + ->can(auth()->user()->can('edit_tags')) + ->id() + ->class('bg-transparent border-0') + ->dispatch('delete', ['rowId' => $row->id]), + ]; + } +} diff --git a/addons/UltimateTags/resources/views/livewire/show-tags.blade.php b/addons/UltimateTags/resources/views/livewire/show-tags.blade.php index fcc90f4..59ee752 100644 --- a/addons/UltimateTags/resources/views/livewire/show-tags.blade.php +++ b/addons/UltimateTags/resources/views/livewire/show-tags.blade.php @@ -1,70 +1,16 @@
@include('ultimatetags::livewire.tags-modals') - @if (session()->has('message')) + @if (session('message'))
{{ session('message') }}
@endif - @if (session()->has('error')) + @if (session('error'))
{{ session('error') }}
@endif -
-
-
- Tags -
- -
-
- - -
-
-
- -
- - - - - - - - - - - - - - @foreach($tags as $tag) - - - - - - - - - - @endforeach - - -
IDNameTagDescriptionPermissionServerActions
{{ $tag->id }}{{ $tag->name }}{!! $tag->tag !!}{{ $tag->description }}{{ $tag->permission }}{{ $tag->server }} - - -
- {{ $tags->links() }} -
-
+ + @livewire('ultimatetags::tags-table') +