Skip to content

Commit

Permalink
- the persisting can now be used with session variables instead of co…
Browse files Browse the repository at this point in the history
…okies
  • Loading branch information
marineusde committed Apr 17, 2024
1 parent 1581c97 commit 894e679
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
13 changes: 13 additions & 0 deletions resources/config/livewire-powergrid.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,19 @@

'filter' => 'inline',

/*
|--------------------------------------------------------------------------
| Persisting
|--------------------------------------------------------------------------
|
| PowerGrid supports persisting of the filters, columns and sorting.
| 'true': persist in the session.
| 'false': persist with cookies (default).
|
*/

'persist_in_session' => false,

/*
|--------------------------------------------------------------------------
| Cache
Expand Down
20 changes: 15 additions & 5 deletions src/Concerns/Persist.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PowerComponents\LivewirePowerGrid\Concerns;

use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\Session;
use PowerComponents\LivewirePowerGrid\PowerGridComponent;

/** @codeCoverageIgnore */
Expand Down Expand Up @@ -47,7 +48,11 @@ protected function persistState(string $tableItem): void
return;
}

Cookie::queue('pg:' . $this->tableName, strval(json_encode($state)), now()->addYears(5)->unix());
if (boolval(config('livewire-powergrid.persist_in_session', false))) {
Session::put('pg:' . $this->tableName, strval(json_encode($state)));
} else {
Cookie::queue('pg:' . $this->tableName, strval(json_encode($state)), now()->addYears(5)->unix());
}
}

private function restoreState(): void
Expand All @@ -56,14 +61,19 @@ private function restoreState(): void
return;
}

/** @var null|string $cookie */
$cookie = Cookie::get('pg:' . $this->tableName);
if (boolval(config('livewire-powergrid.persist_in_session', false))) {
/** @var null|string $cookieOrSession */
$cookieOrSession = Session::get('pg:' . $this->tableName);
} else {
/** @var null|string $cookieOrSession */
$cookieOrSession = Cookie::get('pg:' . $this->tableName);
}

if (is_null($cookie)) {
if (is_null($cookieOrSession)) {
return;
}

$state = (array) json_decode(strval($cookie), true);
$state = (array) json_decode(strval($cookieOrSession), true);

if (in_array('columns', $this->persist) && array_key_exists('columns', $state)) {
$this->columns = collect($this->columns)->map(function ($column) use ($state) {
Expand Down

0 comments on commit 894e679

Please sign in to comment.