Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wip] refactor: transaction filter as a modal for mobile devices #462

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions app/Http/Livewire/Concerns/LivewireLengthAwarePaginator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace App\Http\Livewire\Concerns;

use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Pagination\LengthAwarePaginator as Base;
use IteratorAggregate;
use Livewire\Wireable;
use Traversable;

class LivewireLengthAwarePaginator implements Wireable, Arrayable, IteratorAggregate//, ArrayAccess, Countable, Jsonable, JsonSerializable
{
public function __construct(
public Base $paginator,
public ?string $model = null,
) {
//
}

public function getIterator(): Traversable
{
return $this->paginator->getIterator();
}

public function __call(string $method, mixed $args)
{
return $this->paginator->{$method}(...$args);
}

public function toLivewire(): array
{
if ($this->items() !== []) {
// dd($this->toArray());
}
return $this->toArray();
}

public function toArray(): array
{
$items = $this->paginator->items();
if ($this->model !== null) {
$items = array_map(fn ($item) => $item->model()->toArray(), $items);
}

return [
'items' => $items,
'total' => $this->paginator->total(),
'perPage' => $this->paginator->perPage(),
'currentPage' => $this->paginator->currentPage(),
'options' => $this->paginator->getOptions(),
'model' => $this->model,
];
}

public static function fromLivewire($value): self
{
if ($value['model'] !== null && $value['items'] !== null) {
$value['items'] = array_map(fn ($item) => (new $value['model']())->fill($item), $value['items']);
}

return new static(
new Base($value['items'], $value['total'], $value['perPage'], $value['currentPage'], $value['options']),
$value['model']
);
}
}
54 changes: 54 additions & 0 deletions app/Http/Livewire/Filters/WalletTransactions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace App\Http\Livewire\Filters;

use ARKEcosystem\Foundation\UserInterface\Http\Livewire\Concerns\HasModal;
use Illuminate\Contracts\View\View;
use Livewire\Component;

class WalletTransactions extends Component
{
use HasModal;

public array $filter = [
'outgoing' => true,
'incoming' => true,
'transfers' => true,
'votes' => true,
'multipayments' => true,
'others' => true,
];

public bool $selectAllFilters = true;

/** @var mixed */
protected $listeners = [
'setTransactionsFilter',
'setTransactionsFilterSelectAll',
];

public function render(): View
{
return view('livewire.filters.wallet-transactions');
}

public function updatedSelectAllFilters(bool $value): void
{
$this->emitUp('setTransactionsFilterSelectAll', $value);
}

public function updatedFilter(bool $value, string $key): void
{
$this->emitUp('setTransactionsFilter', $key, $value);
}

public function setTransactionsFilter(string $filter, bool $value): void
{
$this->filter[$filter] = $value;
}

public function setTransactionsFilterSelectAll(bool $value): void
{
$this->selectAllFilters = $value;
}
}
50 changes: 39 additions & 11 deletions app/Http/Livewire/WalletTransactionTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,33 @@
use App\Facades\Wallets;
use App\Http\Livewire\Concerns\DeferLoading;
use App\Http\Livewire\Concerns\HasTablePagination;
use App\Http\Livewire\Concerns\LivewireLengthAwarePaginator;
use App\Models\Scopes\OrderByTimestampScope;
use App\Models\Transaction;
use App\ViewModels\ViewModelFactory;
use App\ViewModels\WalletViewModel;
use ArkEcosystem\Crypto\Enums\Types;
use ARKEcosystem\Foundation\UserInterface\Http\Livewire\Concerns\HasModal;
use Illuminate\Contracts\View\View;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Pagination\LengthAwarePaginator;
use Livewire\Component;

/**
* @property bool $isAllSelected
* @property LengthAwarePaginator $transactions
* */
final class WalletTransactionTable extends Component
{
use DeferLoading;
use HasModal;
use HasTablePagination;

public string $address;

public ?string $publicKey = null;

public LivewireLengthAwarePaginator $transactionValues;

public array $filter = [
'outgoing' => true,
'incoming' => true,
Expand Down Expand Up @@ -93,13 +97,15 @@ public function mount(WalletViewModel $wallet, bool $deferLoading = true): void
if (! $deferLoading) {
$this->setIsReady();
}

$this->updateTransactions();
}

public function render(): View
{
return view('livewire.wallet-transaction-table', [
'wallet' => ViewModelFactory::make(Wallets::findByAddress($this->address)),
'transactions' => ViewModelFactory::paginate($this->transactions),
'transactions' => ViewModelFactory::paginate($this->transactionValues->paginator),
]);
}

Expand All @@ -118,7 +124,7 @@ public function getNoResultsMessageProperty(): null|string
return trans('tables.transactions.no_results.no_addressing_filters');
}

if ($this->transactions->total() === 0) {
if ($this->transactionValues->total() === 0) {
return trans('tables.transactions.no_results.no_results');
}

Expand All @@ -139,24 +145,46 @@ public function updatedFilter(): void
$this->setPage(1);
}

public function getTransactionsProperty(): LengthAwarePaginator
public function setIsReady(): void
{
$this->isReady = true;
$this->updateTransactions();
}

public function updatedPage(): void
{
$this->updateTransactions();
}

public function updatedPerPage(): void
{
$this->updateTransactions();
}

public function updateTransactions(): void
{
$emptyResults = new LengthAwarePaginator([], 0, $this->perPage);
$this->transactionValues = new LivewireLengthAwarePaginator(
new LengthAwarePaginator([], 0, $this->perPage),
Transaction::class,
);
if (! $this->isReady) {
return $emptyResults;
return;
}

if (! $this->hasAddressingFilters()) {
return $emptyResults;
return;
}

if (! $this->hasTransactionTypeFilters()) {
return $emptyResults;
return;
}

return $this->getTransactionsQuery()
->withScope(OrderByTimestampScope::class)
->paginate($this->perPage);
$this->transactionValues = new LivewireLengthAwarePaginator(
$this->getTransactionsQuery()
->withScope(OrderByTimestampScope::class)
->paginate($this->perPage),
Transaction::class,
);
}

private function hasAddressingFilters(): bool
Expand Down
18 changes: 18 additions & 0 deletions app/Models/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,24 @@ final class Transaction extends Model

private bool|string|null $vendorFieldContent = false;

public function toArray(): array
{
return [
'id' => $this->id,
'block_id' => $this->block_id,
'block_height' => $this->block_height,
'type' => $this->type,
'type_group' => $this->type_group,
'sender_public_key' => $this->sender_public_key,
'recipient_id' => $this->recipient_id,
'timestamp' => $this->timestamp,
'fee' => $this->fee->__toString(),
'amount' => $this->amount->__toString(),
'nonce' => $this->nonce,
'asset' => $this->asset,
];
}

/**
* Get the indexable data array for the model.
*
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"ext-gmp": "*",
"ardenthq/arkvault-url": "^1.1",
"arkecosystem/crypto": "^1.8",
"arkecosystem/foundation": "^13.14",
"arkecosystem/foundation": "^13.15",
"blade-ui-kit/blade-icons": "^1.5",
"brick/math": "^0.11",
"doctrine/dbal": "^3.0",
Expand Down
Loading