From 6c80df0db2519f79f0431e02d3ec5db8a8bbd1e1 Mon Sep 17 00:00:00 2001 From: Henning <105713989+marineusde@users.noreply.github.com> Date: Mon, 29 Apr 2024 11:50:41 +0200 Subject: [PATCH] - the persisting can now be used with session variables instead of cookies (#1509) --- resources/config/livewire-powergrid.php | 13 +++++ src/Concerns/Persist.php | 44 ++++++++++++-- tests/Feature/PersistTest.php | 76 +++++++++++++++++++++++++ 3 files changed, 127 insertions(+), 6 deletions(-) create mode 100644 tests/Feature/PersistTest.php diff --git a/resources/config/livewire-powergrid.php b/resources/config/livewire-powergrid.php index 1113fc79..ef1a47b3 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. + | 'session': persist in the session. + | 'cookies': persist with cookies (default). + | + */ + + 'persist_driver' => 'cookies', + /* |-------------------------------------------------------------------------- | Cache diff --git a/src/Concerns/Persist.php b/src/Concerns/Persist.php index 13fdf93f..4eb53180 100644 --- a/src/Concerns/Persist.php +++ b/src/Concerns/Persist.php @@ -2,7 +2,8 @@ namespace PowerComponents\LivewirePowerGrid\Concerns; -use Illuminate\Support\Facades\Cookie; +use Exception; +use Illuminate\Support\Facades\{Cookie, Session}; use PowerComponents\LivewirePowerGrid\PowerGridComponent; /** @codeCoverageIgnore */ @@ -20,6 +21,9 @@ public function persist(array $tableItems): PowerGridComponent return $this; } + /** + * @throws Exception + */ protected function persistState(string $tableItem): void { $state = []; @@ -47,23 +51,37 @@ protected function persistState(string $tableItem): void return; } - Cookie::queue('pg:' . $this->tableName, strval(json_encode($state)), now()->addYears(5)->unix()); + if ($this->getPersistDriverConfig() === 'session') { + Session::put('pg:' . $this->tableName, strval(json_encode($state))); + } elseif ($this->getPersistDriverConfig() === 'cookies') { + Cookie::queue('pg:' . $this->tableName, strval(json_encode($state)), now()->addYears(5)->unix()); + } } + /** + * @throws Exception + */ private function restoreState(): void { if (empty($this->persist)) { return; } - /** @var null|string $cookie */ - $cookie = Cookie::get('pg:' . $this->tableName); + $cookieOrSession = null; + + if ($this->getPersistDriverConfig() === 'session') { + /** @var null|string $cookieOrSession */ + $cookieOrSession = Session::get('pg:' . $this->tableName); + } elseif ($this->getPersistDriverConfig() === 'cookies') { + /** @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) { @@ -87,4 +105,18 @@ private function restoreState(): void $this->multiSort = $state['multiSort']; } } + + /** + * @throws Exception + */ + private function getPersistDriverConfig(): string + { + $persistDriver = strval(config('livewire-powergrid.persist_driver', 'cookies')); + + if (!in_array($persistDriver, ['session', 'cookies'])) { + throw new Exception('Invalid persist driver'); + } + + return $persistDriver; + } } diff --git a/tests/Feature/PersistTest.php b/tests/Feature/PersistTest.php new file mode 100644 index 00000000..d7ee91fa --- /dev/null +++ b/tests/Feature/PersistTest.php @@ -0,0 +1,76 @@ +persist(['filters', 'enabledFilters']); + + return parent::setUp(); + } + + public function filters(): array + { + return array_merge(parent::filters(), [ + Filter::inputText('name'), + ]); + } +}; + +$params = [ + 'tailwind -> id' => [$component::class, 'tailwind', 'name'], + 'bootstrap -> id' => [$component::class, 'bootstrap', 'name'], +]; + +it('should be able to set persist_driver for session', function (string $componentString, string $theme, string $field) { + config()->set('livewire-powergrid.persist_driver', 'session'); + + $component = livewire($componentString) + ->call($theme); + + /** @var PowerGridComponent $component */ + expect($component->filters) + ->toMatchArray([]); + + /** @var Testable $component */ + $component->call('filterInputText', $field, 'ba', 'test'); + + expect(session('pg:default'))->toBe('{"filters":[],"enabledFilters":[{"field":"' . $field . '","label":"test"}]}'); +})->group('filters') + ->with($params); + +it('should be able to set persist_driver for cookies', function (string $componentString, string $theme, string $field) { + config()->set('livewire-powergrid.persist_driver', 'cookies'); + + $component = livewire($componentString) + ->call($theme); + + /** @var PowerGridComponent $component */ + expect($component->filters) + ->toMatchArray([]); + + /** @var Testable $component */ + $component->call('filterInputText', $field, 'ba', 'test'); + + expect(Cookie::queued('pg:default')->getValue())->toBe('{"filters":[],"enabledFilters":[{"field":"' . $field . '","label":"test"}]}'); +}) + ->with($params); + +it('should not be able to set invalid persist driver', function (string $componentString, string $theme) { + # change config + config()->set('livewire-powergrid.persist_driver', 'invalid'); + + expect(static function () use ($componentString, $theme) { + livewire($componentString) + ->call($theme); + })->toThrow(Exception::class); +}) + ->with($params);