Skip to content

Commit

Permalink
chore: merge staging into master (#539)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbarnsley authored Sep 12, 2023
2 parents 6e89235 + 32e4e13 commit 6399eda
Show file tree
Hide file tree
Showing 29 changed files with 396 additions and 205 deletions.
50 changes: 50 additions & 0 deletions app/Http/Livewire/Abstracts/TabbedTableComponent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace App\Http\Livewire\Abstracts;

use App\Http\Livewire\Concerns\HasTablePagination;
use Livewire\Component;
use Livewire\Livewire;

abstract class TabbedTableComponent extends Component
{
use HasTablePagination;

final public function resolvePage(): int
{
if (! Livewire::isDefinitelyLivewireRequest()) {
return $this->page;
}

return (int) $this->query('page', $this->page);
}

final public function resolvePerPage(): int
{
return (int) $this->query('perPage', static::defaultPerPage());
}

private function query(string $key, mixed $default = null): mixed
{
/** @var string|null $referer */
$referer = request()->header('Referer');

if ($referer === null) {
return $default;
}

if (strlen($referer) === 0) {
return $default;
}

parse_str((string) parse_url($referer, PHP_URL_QUERY), $refererQueryString);

if (array_key_exists($key, $refererQueryString)) {
return $refererQueryString[$key];
}

return $default;
}
}
27 changes: 19 additions & 8 deletions app/Http/Livewire/Concerns/HasTablePagination.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,32 @@

use ARKEcosystem\Foundation\UserInterface\Http\Livewire\Concerns\HasPagination;

/** @property int $perPage */
trait HasTablePagination
{
use HasPagination;

public int $perPage;
public ?int $perPage = null;

public function bootHasTablePagination(): void
final public function bootHasTablePagination(): void
{
$this->perPage = static::defaultPerPage();
if ($this->perPage === null) {
$this->perPage = static::defaultPerPage();
} else {
$this->perPage = $this->resolvePerPage();
}
}

public function queryStringHasTablePagination(): array
final public function queryStringHasTablePagination(): array
{
return [
'perPage' => ['except' => static::defaultPerPage()],
];
}

public function setPerPage(int $perPage): void
final public function setPerPage(int $perPage): void
{
if (! in_array($perPage, $this->perPageOptions(), true)) {
if (! in_array($perPage, static::perPageOptions(), true)) {
return;
}

Expand All @@ -35,12 +40,13 @@ public function setPerPage(int $perPage): void
$this->gotoPage(1);
}

public function perPageOptions(): array
// @phpstan-ignore-next-line
public static function perPageOptions(): array
{
return trans('pagination.per_page_options');
}

public static function defaultPerPage(): int
final public static function defaultPerPage(): int
{
if (defined(static::class.'::PER_PAGE')) {
$const = constant(static::class.'::PER_PAGE');
Expand All @@ -52,4 +58,9 @@ public static function defaultPerPage(): int

return intval(config('arkscan.pagination.per_page'));
}

private function resolvePerPage(): int
{
return $this->perPage;
}
}
71 changes: 55 additions & 16 deletions app/Http/Livewire/Concerns/HasTabs.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@

trait HasTabs
{
public array $tabQueryData = [];

public array $savedQueryData = [];

public function __get(mixed $property): mixed
{
$value = Arr::get($this->tabQueryData[$this->view], $property);
Expand All @@ -30,14 +34,14 @@ public function __set(string $property, mixed $value): void
if (Arr::has($this->tabQueryData[$this->view], $property)) {
$this->tabQueryData[$this->view][$property] = $value;
}

if (Arr::has($this->tabQueryData[$this->previousView], $property)) {
$this->tabQueryData[$this->previousView][$property] = $value;
}
}

public function triggerViewIsReady(?string $view = null): void
{
if (! array_key_exists($this->view, $this->savedQueryData)) {
$this->saveViewData();
}

if ($view === null) {
$view = $this->view;
}
Expand All @@ -50,6 +54,15 @@ public function triggerViewIsReady(?string $view = null): void
return;
}

if (array_key_exists('perPage', $this->tabQueryData[$this->view])) {
$component = $this->tabbedComponent();

$perPage = (int) $this->tabQueryData[$this->view]['perPage'];
if (! in_array($perPage, $component::perPageOptions(), true)) {
$this->tabQueryData[$this->view]['perPage'] = $component::defaultPerPage();
}
}

$this->emit('set'.Str::studly($view).'Ready');

$this->alreadyLoadedViews[$view] = true;
Expand All @@ -63,18 +76,7 @@ public function updatingView(string $newView): void

$this->previousView = $this->view;

SupportBrowserHistoryWrapper::init()->mergeRequestQueryStringWithComponent($this);

$this->savedQueryData[$this->view] = $this->tabQueryData[$this->view];

// Reset the querystring data on view change to clear the URL
$queryStringData = $this->queryString();
foreach ($this->tabQueryData[$this->view] as $key => $value) {
// @phpstan-ignore-next-line
$this->{$key} = $queryStringData[$key]['except'];
}

$this->triggerViewIsReady($newView);
$this->saveViewData($newView);
}

/**
Expand All @@ -91,4 +93,41 @@ public function updatedView(): void
}
}
}

abstract private function tabbedComponent();

private function saveViewData(?string $newView = null): void
{
SupportBrowserHistoryWrapper::init()->mergeRequestQueryStringWithComponent($this);

$this->savedQueryData[$this->view] = $this->tabQueryData[$this->view];

if ($newView === null) {
return;
}

// Reset the querystring data on view change to clear the URL
$queryStringData = $this->queryString();
foreach ($this->tabQueryData[$this->view] as $key => $value) {
// @phpstan-ignore-next-line
$this->{$key} = $queryStringData[$key]['except'];
}

$this->triggerViewIsReady($newView);
}

private function resolveView(): string
{
return request()->get('view', $this->view);
}

private function resolvePage(): int
{
return (int) request()->get('page', $this->page);
}

private function resolvePerPage(): int
{
return (int) request()->get('perPage', $this->perPage);
}
}
8 changes: 3 additions & 5 deletions app/Http/Livewire/Delegates/Delegates.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,24 @@
namespace App\Http\Livewire\Delegates;

use App\Facades\Network;
use App\Http\Livewire\Abstracts\TabbedTableComponent;
use App\Http\Livewire\Concerns\DeferLoading;
use App\Http\Livewire\Concerns\HasTableFilter;
use App\Http\Livewire\Concerns\HasTablePagination;
use App\Models\Scopes\OrderByBalanceScope;
use App\Models\Wallet;
use App\ViewModels\ViewModelFactory;
use Illuminate\Contracts\View\View;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Pagination\LengthAwarePaginator;
use Livewire\Component;

/**
* @property bool $isAllSelected
* @property LengthAwarePaginator $delegates
* */
final class Delegates extends Component
final class Delegates extends TabbedTableComponent
{
use DeferLoading;
use HasTableFilter;
use HasTablePagination;

public const PER_PAGE = 51;

Expand Down Expand Up @@ -100,7 +98,7 @@ public function getShowMissedBlocksProperty(): bool
return ($this->page - 1) * $this->perPage < Network::delegateCount();
}

public function perPageOptions(): array
public static function perPageOptions(): array
{
return trans('tables.delegates.delegate_per_page_options');
}
Expand Down
6 changes: 2 additions & 4 deletions app/Http/Livewire/Delegates/MissedBlocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,20 @@

namespace App\Http\Livewire\Delegates;

use App\Http\Livewire\Abstracts\TabbedTableComponent;
use App\Http\Livewire\Concerns\DeferLoading;
use App\Http\Livewire\Concerns\HasTablePagination;
use App\Models\ForgingStats;
use App\ViewModels\ViewModelFactory;
use Illuminate\Contracts\View\View;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Pagination\LengthAwarePaginator;
use Livewire\Component;

/**
* @property LengthAwarePaginator $missedBlocks
* */
final class MissedBlocks extends Component
final class MissedBlocks extends TabbedTableComponent
{
use DeferLoading;
use HasTablePagination;

/** @var mixed */
protected $listeners = [
Expand Down
6 changes: 2 additions & 4 deletions app/Http/Livewire/Delegates/RecentVotes.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,25 @@

namespace App\Http\Livewire\Delegates;

use App\Http\Livewire\Abstracts\TabbedTableComponent;
use App\Http\Livewire\Concerns\DeferLoading;
use App\Http\Livewire\Concerns\HasTableFilter;
use App\Http\Livewire\Concerns\HasTablePagination;
use App\Models\Scopes\OrderByTimestampScope;
use App\Models\Transaction;
use App\Services\Timestamp;
use App\ViewModels\ViewModelFactory;
use Illuminate\Contracts\View\View;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Pagination\LengthAwarePaginator;
use Livewire\Component;

/**
* @property bool $isAllSelected
* @property LengthAwarePaginator $recentVotes
* */
final class RecentVotes extends Component
final class RecentVotes extends TabbedTableComponent
{
use DeferLoading;
use HasTableFilter;
use HasTablePagination;

public array $filter = [
'vote' => true,
Expand Down
13 changes: 13 additions & 0 deletions app/Http/Livewire/Delegates/Tabs.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
use Illuminate\Contracts\View\View;
use Livewire\Component;

/**
* @property int $page
* @property int $perPage
*/
final class Tabs extends Component
{
use HasTabs;
Expand Down Expand Up @@ -82,4 +86,13 @@ public function showDelegatesView(string $view): void
{
$this->syncInput('view', $view);
}

private function tabbedComponent(): string
{
return [
'delegates' => Delegates::class,
'missed-blocks' => MissedBlocks::class,
'recent-votes' => RecentVotes::class,
][$this->view];
}
}
6 changes: 2 additions & 4 deletions app/Http/Livewire/WalletBlockTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,19 @@
namespace App\Http\Livewire;

use App\Facades\Wallets;
use App\Http\Livewire\Abstracts\TabbedTableComponent;
use App\Http\Livewire\Concerns\DeferLoading;
use App\Http\Livewire\Concerns\HasTablePagination;
use App\Models\Block;
use App\Models\Scopes\OrderByHeightScope;
use App\ViewModels\ViewModelFactory;
use App\ViewModels\WalletViewModel;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\View\View;
use Livewire\Component;

/** @property LengthAwarePaginator $blocks */
final class WalletBlockTable extends Component
final class WalletBlockTable extends TabbedTableComponent
{
use DeferLoading;
use HasTablePagination;

public string $publicKey;

Expand Down
Loading

0 comments on commit 6399eda

Please sign in to comment.