Skip to content

Commit 13fafc0

Browse files
authored
Adjust SessionStorage locations
1 parent f149208 commit 13fafc0

File tree

5 files changed

+147
-140
lines changed

5 files changed

+147
-140
lines changed

src/Traits/Configuration/SessionStorageConfiguration.php

Lines changed: 0 additions & 40 deletions
This file was deleted.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
namespace Rappasoft\LaravelLivewireTables\Traits\Core\SessionStorage;
4+
5+
trait HasColumnSelectSessionStorage
6+
{
7+
protected function storeColumnSelectInSessionStatus(bool $status): self
8+
{
9+
$this->setSessionStorageStatus('columnselect', $status);
10+
11+
return $this;
12+
}
13+
14+
public function storeColumnSelectInSessionEnabled(): self
15+
{
16+
return $this->storeColumnSelectInSessionStatus(true);
17+
}
18+
19+
public function storeColumnSelectInSessionDisabled(): self
20+
{
21+
return $this->storeColumnSelectInSessionStatus(false);
22+
}
23+
24+
public function shouldStoreColumnSelectInSession(): bool
25+
{
26+
return $this->getSessionStorageStatus('columnselect');
27+
}
28+
29+
public function getColumnSelectSessionKey(): string
30+
{
31+
return $this->getTableName().'-stored-columnselect';
32+
}
33+
34+
public function storeColumnSelectValues(): void
35+
{
36+
if ($this->shouldStoreColumnSelectInSession()) {
37+
$this->clearStoredColumnSelectValues();
38+
session([$this->getColumnSelectSessionKey() => $this->selectedColumns]);
39+
}
40+
}
41+
42+
public function restoreColumnSelectValues(): void
43+
{
44+
$this->selectedColumns = $this->getStoredColumnSelectValues();
45+
}
46+
47+
public function getStoredColumnSelectValues(): array
48+
{
49+
if ($this->shouldStoreColumnSelectInSession() && session()->has($this->getColumnSelectSessionKey())) {
50+
return session()->get($this->getColumnSelectSessionKey());
51+
}
52+
53+
return [];
54+
}
55+
56+
public function clearStoredColumnSelectValues(): void
57+
{
58+
if ($this->shouldStoreColumnSelectInSession() && session()->has($this->getColumnSelectSessionKey())) {
59+
session()->forget($this->getColumnSelectSessionKey());
60+
}
61+
}
62+
63+
protected function forgetColumnSelectSession(): void
64+
{
65+
session()->forget($this->getColumnSelectSessionKey());
66+
}
67+
68+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace Rappasoft\LaravelLivewireTables\Traits\Core\SessionStorage;
4+
5+
trait HasFilterSessionStorage
6+
{
7+
protected function storeFiltersInSessionStatus(bool $status): self
8+
{
9+
$this->setSessionStorageStatus('filters', $status);
10+
11+
return $this;
12+
}
13+
14+
public function storeFiltersInSessionEnabled(): self
15+
{
16+
return $this->storeFiltersInSessionStatus(true);
17+
}
18+
19+
public function storeFiltersInSessionDisabled(): self
20+
{
21+
return $this->storeFiltersInSessionStatus(false);
22+
}
23+
24+
public function shouldStoreFiltersInSession(): bool
25+
{
26+
return $this->getSessionStorageStatus('filters');
27+
}
28+
29+
public function getFilterSessionKey(): string
30+
{
31+
return $this->getTableName().'-stored-filters';
32+
}
33+
34+
public function storeFilterValues(): void
35+
{
36+
if ($this->shouldStoreFiltersInSession()) {
37+
$this->clearStoredFilterValues();
38+
session([$this->getFilterSessionKey() => $this->appliedFilters]);
39+
}
40+
}
41+
42+
public function restoreFilterValues(): void
43+
{
44+
if (empty($this->filterComponents) || empty($this->appliedFilters)) {
45+
$this->filterComponents = $this->appliedFilters = $this->getStoredFilterValues();
46+
}
47+
}
48+
49+
public function getStoredFilterValues(): array
50+
{
51+
if ($this->shouldStoreFiltersInSession() && session()->has($this->getFilterSessionKey())) {
52+
return session()->get($this->getFilterSessionKey());
53+
}
54+
55+
return [];
56+
}
57+
58+
public function clearStoredFilterValues(): void
59+
{
60+
if ($this->shouldStoreFiltersInSession() && session()->has($this->getFilterSessionKey())) {
61+
session()->forget($this->getFilterSessionKey());
62+
}
63+
}
64+
}

src/Traits/Helpers/SessionStorageHelpers.php

Lines changed: 0 additions & 96 deletions
This file was deleted.

src/Traits/WithSessionStorage.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,27 @@
22

33
namespace Rappasoft\LaravelLivewireTables\Traits;
44

5-
use Rappasoft\LaravelLivewireTables\Traits\Configuration\SessionStorageConfiguration;
6-
use Rappasoft\LaravelLivewireTables\Traits\Helpers\SessionStorageHelpers;
5+
use Rappasoft\LaravelLivewireTables\Traits\Core\SessionStorage\{HasFilterSessionStorage,HasColumnSelectSessionStorage};
76

87
trait WithSessionStorage
98
{
10-
use SessionStorageConfiguration,
11-
SessionStorageHelpers;
9+
use HasFilterSessionStorage,
10+
HasColumnSelectSessionStorage;
1211

1312
public array $sessionStorageStatus = [
1413
'columnselect' => true,
1514
'filters' => false,
1615
];
16+
17+
protected function getSessionStorageStatus(string $name): bool
18+
{
19+
return $this->sessionStorageStatus[$name] ?? false;
20+
}
21+
22+
protected function setSessionStorageStatus(string $name, bool $status): self
23+
{
24+
$this->sessionStorageStatus[$name] = $status;
25+
26+
return $this;
27+
}
1728
}

0 commit comments

Comments
 (0)