-
-
Notifications
You must be signed in to change notification settings - Fork 337
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <[email protected]>
- Loading branch information
Showing
8 changed files
with
151 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |