Skip to content

Commit

Permalink
Updated UltimateTags addon to use powergrid.
Browse files Browse the repository at this point in the history
  • Loading branch information
ChimpGamer committed Dec 17, 2024
1 parent 00cb7b1 commit e70bb35
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
64 changes: 37 additions & 27 deletions addons/UltimateTags/Livewire/ShowTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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',
Expand All @@ -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([
Expand All @@ -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;
Expand All @@ -84,7 +83,7 @@ public function editTag(Tag $tag)
$this->server = $tag->server;
}

public function updateTag()
public function updateTag(): void
{
$validatedData = $this->validate();

Expand All @@ -103,29 +102,39 @@ 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) {
$this->dispatch('close-modal', $modalId);
}
}

public function resetInput()
public function resetInput(): void
{
$this->tagId = -1;
$this->name = null;
Expand All @@ -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');
}
}
95 changes: 95 additions & 0 deletions addons/UltimateTags/Livewire/TagsTable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

namespace Addons\UltimateTags\Livewire;

use Addons\UltimateTags\App\Models\Tag;
use Illuminate\Database\Eloquent\Builder;
use PowerComponents\LivewirePowerGrid\Button;
use PowerComponents\LivewirePowerGrid\Column;
use PowerComponents\LivewirePowerGrid\Facades\PowerGrid;
use PowerComponents\LivewirePowerGrid\PowerGridComponent;
use PowerComponents\LivewirePowerGrid\PowerGridFields;

final class TagsTable extends PowerGridComponent
{
public string $tableName = 'tags-table';

public function setUp(): array
{
return [
PowerGrid::header()
->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('<i class="material-icons text-warning">edit</i>')
->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('<i class="material-icons text-danger">delete</i>')
->can(auth()->user()->can('edit_tags'))
->id()
->class('bg-transparent border-0')
->dispatch('delete', ['rowId' => $row->id]),
];
}
}
64 changes: 5 additions & 59 deletions addons/UltimateTags/resources/views/livewire/show-tags.blade.php
Original file line number Diff line number Diff line change
@@ -1,70 +1,16 @@
<div>
@include('ultimatetags::livewire.tags-modals')

@if (session()->has('message'))
@if (session('message'))
<h5 class="alert alert-success">{{ session('message') }}</h5>
@endif
@if (session()->has('error'))
@if (session('error'))
<h5 class="alert alert-danger">{{ session('error') }}</h5>
@endif

<div class="card">
<div class="card-header py-3">
<h5 class="mb-0 text-center">
<strong>Tags</strong>
</h5>

<div class="float-end d-inline" wire:ignore>
<div class="form-outline" data-mdb-input-init>
<input type="search" id="tagsSearch" class="form-control" wire:model.live="search"/>
<label class="form-label" for="tagsSearch"
style="font-family: Roboto, 'FontAwesome'">Search...</label>
</div>
</div>
</div>

<div class="card-body border-0 shadow table-responsive">
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Tag</th>
<th>Description</th>
<th>Permission</th>
<th>Server</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
@foreach($tags as $tag)
<tr>
<td>{{ $tag->id }}</td>
<td>{{ $tag->name }}</td>
<td>{!! $tag->tag !!}</td>
<td>{{ $tag->description }}</td>
<td>{{ $tag->permission }}</td>
<td>{{ $tag->server }}</td>
<td>
<button type="button" style="background: transparent; border: none;"
data-mdb-ripple-init data-mdb-modal-init
data-mdb-target="#editTagModal" wire:click="editTag({{$tag->id}})">
<i class="material-icons text-warning">edit</i>
</button>
<button type="button" style="background: transparent; border: none;"
data-mdb-ripple-init data-mdb-modal-init
data-mdb-target="#deleteTagModal" wire:click="deleteTag({{$tag->id}})">
<i class="material-icons text-danger">delete</i>
</button>
</td>
</tr>
@endforeach

</tbody>
</table>
{{ $tags->links() }}
</div>
</div>
<x-card-table title="Tags">
@livewire('ultimatetags::tags-table')
</x-card-table>
<div class="p-4">
<button type="button" class="btn btn-primary" data-mdb-ripple-init data-mdb-modal-init
data-mdb-target="#addTagModal"
Expand Down

0 comments on commit e70bb35

Please sign in to comment.