From dcefb1b3defe38d1ccc94a8cf2bd8d32fff124ce Mon Sep 17 00:00:00 2001 From: Henning Zimmermann Date: Wed, 17 Apr 2024 16:40:49 +0200 Subject: [PATCH] - the persisting can now be used with session variables instead of cookies --- resources/config/livewire-powergrid.php | 13 +++++++++++++ src/Concerns/Persist.php | 21 +++++++++++++++------ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/resources/config/livewire-powergrid.php b/resources/config/livewire-powergrid.php index 1113fc797..eb5027312 100644 --- a/resources/config/livewire-powergrid.php +++ b/resources/config/livewire-powergrid.php @@ -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 diff --git a/src/Concerns/Persist.php b/src/Concerns/Persist.php index 13fdf93ff..920e981bd 100644 --- a/src/Concerns/Persist.php +++ b/src/Concerns/Persist.php @@ -2,7 +2,7 @@ namespace PowerComponents\LivewirePowerGrid\Concerns; -use Illuminate\Support\Facades\Cookie; +use Illuminate\Support\Facades\{Cookie, Session}; use PowerComponents\LivewirePowerGrid\PowerGridComponent; /** @codeCoverageIgnore */ @@ -47,7 +47,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 @@ -56,14 +60,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) {