Skip to content

Commit

Permalink
Save filter selection to session (BETA) (#1910)
Browse files Browse the repository at this point in the history
* Initial Commit

* Fix styling

* Initial Commit

* Fix styling

* Fix nullable return

* Add Initial Tests - Remove Non-Required Method

* Fix styling

* Swap methods

* Add default docs

* Remove errant note

* Adjust docs

* Adjust Docs for storeFiltersInSessionEnabled

---------

Co-authored-by: lrljoe <[email protected]>
  • Loading branch information
lrljoe and lrljoe authored Sep 1, 2024
1 parent 244951b commit 570abdc
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 0 deletions.
24 changes: 24 additions & 0 deletions docs/filters/available-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,30 @@ public function configure(): void
}
```

### storeFiltersInSessionEnabled

Optional behaviour - stores filter values in the session (specific to table - based on the table name)

#### Exercise Caution
If re-using the same Livewire Table Component multiple times in your site, with the same table name, this may cause clashes in filter values

```php
public function configure(): void
{
$this->storeFiltersInSessionEnabled();
}
```
### storeFiltersInSessionDisabled

Default behaviour - does not store filters in the session

```php
public function configure(): void
{
$this->storeFiltersInSessionDisabled();
}
```


----

Expand Down
23 changes: 23 additions & 0 deletions src/Traits/Configuration/SessionStorageConfiguration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Rappasoft\LaravelLivewireTables\Traits\Configuration;

trait SessionStorageConfiguration
{
protected function storeFiltersInSessionStatus(bool $status): self
{
$this->sessionStorageStatus['filters'] = $status;

return $this;
}

public function storeFiltersInSessionEnabled(): self
{
return $this->storeFiltersInSessionStatus(true);
}

public function storeFiltersInSessionDisabled(): self
{
return $this->storeFiltersInSessionStatus(false);
}
}
1 change: 1 addition & 0 deletions src/Traits/HasAllTraits.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ trait HasAllTraits
WithRefresh,
WithReordering,
WithSecondaryHeader,
WithSessionStorage,
WithTableAttributes,
WithTools;
}
6 changes: 6 additions & 0 deletions src/Traits/Helpers/FilterHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ trait FilterHelpers
*/
public function mountFilterHelpers(): void
{
$this->restoreFilterValues();

foreach ($this->getFilters() as $filter) {
if (! isset($this->appliedFilters[$filter->getKey()])) {
if ($filter->hasFilterDefaultValue()) {
Expand Down Expand Up @@ -145,6 +147,8 @@ public function setFilter(string $filterKey, string|array|null $value): void
event(new FilterApplied($this->getTableName(), $filterKey, $value));
}
$this->dispatch('filter-was-set', tableName: $this->getTableName(), filterKey: $filterKey, value: $value);
$this->storeFilterValues();

}

public function selectAllFilterOptions(string $filterKey): void
Expand Down Expand Up @@ -173,6 +177,7 @@ public function setFilterDefaults(): void
$this->resetFilter($filter);
}
}

}

/**
Expand Down Expand Up @@ -252,6 +257,7 @@ public function resetFilter($filter): void
$this->callHook('filterReset', ['filter' => $filter->getKey()]);
$this->callTraitHook('filterReset', ['filter' => $filter->getKey()]);
$this->setFilter($filter->getKey(), $filter->getDefaultValue());

}

public function getFilterLayout(): string
Expand Down
52 changes: 52 additions & 0 deletions src/Traits/Helpers/SessionStorageHelpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Rappasoft\LaravelLivewireTables\Traits\Helpers;

trait SessionStorageHelpers
{
protected function getSessionStorageStatus(string $name): bool
{
return $this->sessionStorageStatus[$name] ?? false;
}

public function shouldStoreFiltersInSession(): bool
{
return $this->getSessionStorageStatus('filters');
}

public function getFilterSessionKey(): string
{
return $this->getTableName().'-stored-filters';
}

public function storeFilterValues(): void
{
if ($this->shouldStoreFiltersInSession()) {
$this->clearStoredFilterValues();
session([$this->getFilterSessionKey() => $this->appliedFilters]);
}
}

public function restoreFilterValues(): void
{
if (empty($this->filterComponents) || empty($this->appliedFilters)) {
$this->filterComponents = $this->appliedFilters = $this->getStoredFilterValues();
}
}

public function getStoredFilterValues(): array
{
if ($this->shouldStoreFiltersInSession() && session()->has($this->getFilterSessionKey())) {
return session()->get($this->getFilterSessionKey());
}

return [];
}

public function clearStoredFilterValues(): void
{
if ($this->shouldStoreFiltersInSession() && session()->has($this->getFilterSessionKey())) {
session()->forget($this->getFilterSessionKey());
}
}
}
2 changes: 2 additions & 0 deletions src/Traits/WithFilters.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public function applyFilters(): Builder
}
}
}
$this->storeFilterValues();
}

return $this->getBuilder();
Expand Down Expand Up @@ -110,5 +111,6 @@ public function updatedFilterComponents(string|array|null $value, string $filter
$this->dispatch('filter-was-set', tableName: $this->getTableName(), filterKey: $filter->getKey(), value: $value);

}

}
}
16 changes: 16 additions & 0 deletions src/Traits/WithSessionStorage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Rappasoft\LaravelLivewireTables\Traits;

use Rappasoft\LaravelLivewireTables\Traits\Configuration\SessionStorageConfiguration;
use Rappasoft\LaravelLivewireTables\Traits\Helpers\SessionStorageHelpers;

trait WithSessionStorage
{
use SessionStorageConfiguration,
SessionStorageHelpers;

public array $sessionStorageStatus = [
'filters' => false,
];
}
27 changes: 27 additions & 0 deletions tests/Traits/Helpers/SessionStorageHelpersTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rappasoft\LaravelLivewireTables\Tests\Traits\Helpers;

use Rappasoft\LaravelLivewireTables\Tests\TestCase;

final class SessionStorageHelpersTest extends TestCase
{
public function test_can_get_session_storage_status_for_filters(): void
{
$this->assertFalse($this->basicTable->shouldStoreFiltersInSession());

$this->basicTable->storeFiltersInSessionEnabled();

$this->assertTrue($this->basicTable->shouldStoreFiltersInSession());

$this->basicTable->storeFiltersInSessionDisabled();

$this->assertFalse($this->basicTable->shouldStoreFiltersInSession());

}

public function test_can_get_session_storage_status_filter_key(): void
{
$this->assertSame($this->basicTable->getTableName().'-stored-filters', $this->basicTable->getFilterSessionKey());
}
}

0 comments on commit 570abdc

Please sign in to comment.