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 19, 2024
1 parent 1581c97 commit 44a9e9e
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 6 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.
| 'session': persist in the session.
| 'cookies': persist with cookies (default).
|
*/

'persist_driver' => 'cookies',

/*
|--------------------------------------------------------------------------
| Cache
Expand Down
44 changes: 38 additions & 6 deletions src/Concerns/Persist.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -20,6 +21,9 @@ public function persist(array $tableItems): PowerGridComponent
return $this;
}

/**
* @throws Exception
*/
protected function persistState(string $tableItem): void
{
$state = [];
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}
}
76 changes: 76 additions & 0 deletions tests/Feature/PersistTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

use Illuminate\Support\Facades\{Cookie};
use Livewire\Features\SupportTesting\Testable;
use PowerComponents\LivewirePowerGrid\Facades\Filter;
use PowerComponents\LivewirePowerGrid\Tests\Concerns\Components\DishTableBase;

use function PowerComponents\LivewirePowerGrid\Tests\Plugins\livewire;

use PowerComponents\LivewirePowerGrid\{PowerGridComponent};

$component = new class () extends DishTableBase {
public function setUp(): array
{
$this->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);

0 comments on commit 44a9e9e

Please sign in to comment.