Skip to content

Commit

Permalink
fix: History table state
Browse files Browse the repository at this point in the history
  • Loading branch information
lewislarsen committed Aug 20, 2024
1 parent 3266893 commit 2b1f5d6
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 17 deletions.
72 changes: 56 additions & 16 deletions app/Livewire/BackupTasks/Tables/BackupTaskHistoryTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,88 @@
namespace App\Livewire\BackupTasks\Tables;

use App\Models\BackupTaskLog;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Facades\Auth;
use Illuminate\View\View;
use Livewire\Attributes\Computed;
use Livewire\Component;
use Livewire\WithPagination;

/**
* Manages the display of backup task history.
*
* This component handles the rendering and pagination of completed backup task logs.
*/
class BackupTaskHistoryTable extends Component
{
use WithPagination;

/** @var BackupTaskLog Collection of backup task logs */
public BackupTaskLog $backupTasks;
/**
* Unique key for the component instance.
*/
public string $tableKey;

/**
* Render the backup task history table.
* The current page number.
*/
public int $page = 1;

/**
* The name of the pagination query string.
*/
protected string $paginationQueryString = 'backup-task-logs';

/**
* Mount the component.
*/
public function mount(): void
{
$this->tableKey = 'backup-task-history-' . Auth::id();
$this->page = (int) request()->query($this->paginationQueryString, '1');
}

/**
* Define the query string parameters.
*
* Fetches and paginates finished backup task logs for the authenticated user.
* @return array<string, array<int, string>>
*/
public function render(): View
public function queryString(): array
{
$backupTaskLogs = BackupTaskLog::finished()
return [
'page' => [0 => $this->paginationQueryString],
];
}

/**
* Get the backup task logs.
*
* @return LengthAwarePaginator<BackupTaskLog>
*/
#[Computed]
public function backupTaskLogs(): LengthAwarePaginator
{
return BackupTaskLog::finished()
->with(['backupTask', 'backupTask.backupDestination', 'backupTask.RemoteServer'])
->whereHas('backupTask', function ($query): void {
$query->where('user_id', Auth::id());
})
->orderBy('created_at', 'desc')
->paginate(Auth::user()?->getAttribute('pagination_count') ?? 15, pageName: 'backup-task-logs');
->paginate(
Auth::user()?->getAttribute('pagination_count') ?? 15,
['*'],
$this->paginationQueryString,
$this->page
);
}

/**
* Render the backup task history table.
*/
public function render(): View
{
return view('livewire.backup-tasks.tables.backup-task-history-table', [
'backupTaskLogs' => $backupTaskLogs,
'backupTaskLogs' => $this->backupTaskLogs(),
]);
}

/**
* Get the listeners array.
*
* Defines the event listeners for this component.
*
* @return array<string, string>
*/
protected function getListeners(): array
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div>
<div wire:key="{{ $tableKey }}">
@if (count($backupTaskLogs) !== 0)
<div>
<x-table.table-wrapper
Expand Down

0 comments on commit 2b1f5d6

Please sign in to comment.