From 570abdc840eac03221069ebaee397feb56d4ee75 Mon Sep 17 00:00:00 2001 From: Joe <104938042+lrljoe@users.noreply.github.com> Date: Sun, 1 Sep 2024 13:39:39 +0100 Subject: [PATCH] Save filter selection to session (BETA) (#1910) * 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 --- docs/filters/available-methods.md | 24 +++++++++ .../SessionStorageConfiguration.php | 23 ++++++++ src/Traits/HasAllTraits.php | 1 + src/Traits/Helpers/FilterHelpers.php | 6 +++ src/Traits/Helpers/SessionStorageHelpers.php | 52 +++++++++++++++++++ src/Traits/WithFilters.php | 2 + src/Traits/WithSessionStorage.php | 16 ++++++ .../Helpers/SessionStorageHelpersTest.php | 27 ++++++++++ 8 files changed, 151 insertions(+) create mode 100644 src/Traits/Configuration/SessionStorageConfiguration.php create mode 100644 src/Traits/Helpers/SessionStorageHelpers.php create mode 100644 src/Traits/WithSessionStorage.php create mode 100644 tests/Traits/Helpers/SessionStorageHelpersTest.php diff --git a/docs/filters/available-methods.md b/docs/filters/available-methods.md index 5dd8c42f5..f8845dbaf 100644 --- a/docs/filters/available-methods.md +++ b/docs/filters/available-methods.md @@ -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(); +} +``` + ---- diff --git a/src/Traits/Configuration/SessionStorageConfiguration.php b/src/Traits/Configuration/SessionStorageConfiguration.php new file mode 100644 index 000000000..cc51d7107 --- /dev/null +++ b/src/Traits/Configuration/SessionStorageConfiguration.php @@ -0,0 +1,23 @@ +sessionStorageStatus['filters'] = $status; + + return $this; + } + + public function storeFiltersInSessionEnabled(): self + { + return $this->storeFiltersInSessionStatus(true); + } + + public function storeFiltersInSessionDisabled(): self + { + return $this->storeFiltersInSessionStatus(false); + } +} diff --git a/src/Traits/HasAllTraits.php b/src/Traits/HasAllTraits.php index 567afccd3..7bef6fee8 100644 --- a/src/Traits/HasAllTraits.php +++ b/src/Traits/HasAllTraits.php @@ -27,6 +27,7 @@ trait HasAllTraits WithRefresh, WithReordering, WithSecondaryHeader, + WithSessionStorage, WithTableAttributes, WithTools; } diff --git a/src/Traits/Helpers/FilterHelpers.php b/src/Traits/Helpers/FilterHelpers.php index 205322ccd..4a78b333b 100644 --- a/src/Traits/Helpers/FilterHelpers.php +++ b/src/Traits/Helpers/FilterHelpers.php @@ -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()) { @@ -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 @@ -173,6 +177,7 @@ public function setFilterDefaults(): void $this->resetFilter($filter); } } + } /** @@ -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 diff --git a/src/Traits/Helpers/SessionStorageHelpers.php b/src/Traits/Helpers/SessionStorageHelpers.php new file mode 100644 index 000000000..ae8af24f1 --- /dev/null +++ b/src/Traits/Helpers/SessionStorageHelpers.php @@ -0,0 +1,52 @@ +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()); + } + } +} diff --git a/src/Traits/WithFilters.php b/src/Traits/WithFilters.php index 49c91da8c..9ec7e60aa 100644 --- a/src/Traits/WithFilters.php +++ b/src/Traits/WithFilters.php @@ -78,6 +78,7 @@ public function applyFilters(): Builder } } } + $this->storeFilterValues(); } return $this->getBuilder(); @@ -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); } + } } diff --git a/src/Traits/WithSessionStorage.php b/src/Traits/WithSessionStorage.php new file mode 100644 index 000000000..b106886e3 --- /dev/null +++ b/src/Traits/WithSessionStorage.php @@ -0,0 +1,16 @@ + false, + ]; +} diff --git a/tests/Traits/Helpers/SessionStorageHelpersTest.php b/tests/Traits/Helpers/SessionStorageHelpersTest.php new file mode 100644 index 000000000..fe1852751 --- /dev/null +++ b/tests/Traits/Helpers/SessionStorageHelpersTest.php @@ -0,0 +1,27 @@ +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()); + } +}