Skip to content

Commit b314e3f

Browse files
committed
Datatables : Refactor to Query Method
Datatables: Ability to Override Query for Search, Filter, Sort. Datatables: Removed Json Resource in favor of simple & plain response
1 parent ccf0611 commit b314e3f

File tree

8 files changed

+430
-2
lines changed

8 files changed

+430
-2
lines changed
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
<?php
2+
3+
namespace Flavorly\VanillaComponents\Core\Integrations;
4+
5+
use Closure;
6+
use Flavorly\VanillaComponents\Core\Concerns\EvaluatesClosures;
7+
use Illuminate\Support\Arr;
8+
9+
class VanillaHybridly
10+
{
11+
use EvaluatesClosures;
12+
13+
public function __construct(
14+
protected bool $reload = false,
15+
protected ?string $url = null,
16+
protected array $query = [],
17+
protected array $options = [],
18+
){}
19+
20+
public function reload(bool|Closure $reload = true): static
21+
{
22+
$this->reload = $this->evaluate($reload);
23+
24+
return $this;
25+
}
26+
27+
public function visit(string|Closure $url): static
28+
{
29+
$this->url = $this->evaluate($url);
30+
$this->reload = false;
31+
32+
return $this;
33+
}
34+
35+
public function route(string|Closure $name): static
36+
{
37+
return $this->visit(route($this->evaluate($name)));
38+
}
39+
40+
public function preserve(bool|Closure $state = true, bool|Closure $scroll = true): static
41+
{
42+
$this->options['preserveState'] = $this->evaluate($state);
43+
$this->options['preserveScroll'] = $this->evaluate($scroll);
44+
45+
return $this;
46+
}
47+
48+
public function replace(bool|Closure $replace = true): static
49+
{
50+
$this->options['replace'] = $this->evaluate($replace);
51+
return $this;
52+
}
53+
54+
public function method(string|Closure $method = 'POST'): static
55+
{
56+
$this->options['method'] = $this->evaluate($method);
57+
return $this;
58+
}
59+
60+
public function post(array|Closure $data = []): static
61+
{
62+
$data = $this->evaluate($data);
63+
if(!empty($data)){
64+
$this->data($data);
65+
}
66+
return $this->method();
67+
}
68+
69+
public function get(): static
70+
{
71+
return $this->method('GET');
72+
}
73+
74+
public function put(): static
75+
{
76+
return $this->method('PUT');
77+
}
78+
79+
public function patch(): static
80+
{
81+
return $this->method('PATCH');
82+
}
83+
84+
public function delete(): static
85+
{
86+
return $this->method('DELETE');
87+
}
88+
89+
public function only(array|Closure $keys): static
90+
{
91+
$this->options['only'] = array_unique(
92+
array_merge(
93+
Arr::get($this->options, 'only', []),
94+
$this->evaluate($keys)
95+
)
96+
);
97+
return $this;
98+
}
99+
100+
public function except(array|Closure $keys): static
101+
{
102+
$this->options['except'] = array_unique(
103+
array_merge(
104+
Arr::get($this->options, 'except', []),
105+
$this->evaluate($keys)
106+
)
107+
);
108+
return $this;
109+
}
110+
111+
public function headers(array|Closure $headers): static
112+
{
113+
$this->options['headers'] = array_unique(
114+
array_merge(
115+
Arr::get($this->options, 'headers', []),
116+
$this->evaluate($headers)
117+
)
118+
);
119+
return $this;
120+
}
121+
122+
public function data(array|Closure $data = []): static
123+
{
124+
$this->options['data'] = array_merge_recursive(
125+
Arr::get($this->options, 'data', []),
126+
$this->evaluate($data)
127+
);
128+
return $this;
129+
}
130+
131+
public function query(array|Closure $params = []): self
132+
{
133+
$this->query = array_unique(
134+
array_merge(
135+
$this->query,
136+
$params
137+
)
138+
);
139+
return $this;
140+
}
141+
142+
public function options(array|Closure $options = []): static
143+
{
144+
$this->options = array_unique(
145+
array_merge(
146+
$this->options,
147+
$this->evaluate($options)
148+
)
149+
);
150+
return $this;
151+
}
152+
153+
public function toArray(): array
154+
{
155+
return array_filter(array_merge([
156+
'url' => $this->url,
157+
'reload' => $this->reload,
158+
'query' => $this->query,
159+
'method' => 'GET',
160+
'preserveState' => true,
161+
'preserveScroll' => true,
162+
], $this->options));
163+
}
164+
165+
public function __toString(): string
166+
{
167+
return json_encode($this->toArray());
168+
}
169+
}
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
<?php
2+
3+
namespace Flavorly\VanillaComponents\Core\Integrations;
4+
5+
use Closure;
6+
use Flavorly\VanillaComponents\Core\Concerns\EvaluatesClosures;
7+
use Illuminate\Support\Arr;
8+
9+
class VanillaInertia
10+
{
11+
use EvaluatesClosures;
12+
13+
public function __construct(
14+
protected bool $reload = false,
15+
protected ?string $url = null,
16+
protected array $query = [],
17+
protected array $options = [],
18+
){}
19+
20+
public function reload(bool|Closure $reload = true): static
21+
{
22+
$this->reload = $this->evaluate($reload);
23+
24+
return $this;
25+
}
26+
27+
public function visit(string|Closure $url): static
28+
{
29+
$this->url = $this->evaluate($url);
30+
$this->reload = false;
31+
32+
return $this;
33+
}
34+
35+
public function route(string|Closure $name): static
36+
{
37+
return $this->visit(route($this->evaluate($name)));
38+
}
39+
40+
public function preserve(bool|Closure $state = true, bool|Closure $scroll = true): static
41+
{
42+
$this->options['preserveState'] = $this->evaluate($state);
43+
$this->options['preserveScroll'] = $this->evaluate($scroll);
44+
45+
return $this;
46+
}
47+
48+
public function replace(bool|Closure $replace = true): static
49+
{
50+
$this->options['replace'] = $this->evaluate($replace);
51+
return $this;
52+
}
53+
54+
public function method(string|Closure $method = 'POST'): static
55+
{
56+
$this->options['method'] = $this->evaluate($method);
57+
return $this;
58+
}
59+
60+
public function post(array|Closure $data = []): static
61+
{
62+
$data = $this->evaluate($data);
63+
if(!empty($data)){
64+
$this->data($data);
65+
}
66+
return $this->method();
67+
}
68+
69+
public function get(): static
70+
{
71+
return $this->method('GET');
72+
}
73+
74+
public function put(): static
75+
{
76+
return $this->method('PUT');
77+
}
78+
79+
public function patch(): static
80+
{
81+
return $this->method('PATCH');
82+
}
83+
84+
public function delete(): static
85+
{
86+
return $this->method('DELETE');
87+
}
88+
89+
public function only(array|Closure $keys): static
90+
{
91+
$this->options['only'] = array_unique(
92+
array_merge(
93+
Arr::get($this->options, 'only', []),
94+
$this->evaluate($keys)
95+
)
96+
);
97+
return $this;
98+
}
99+
100+
public function headers(array|Closure $headers): static
101+
{
102+
$this->options['headers'] = array_unique(
103+
array_merge(
104+
Arr::get($this->options, 'headers', []),
105+
$this->evaluate($headers)
106+
)
107+
);
108+
return $this;
109+
}
110+
111+
public function data(array|Closure $data = []): static
112+
{
113+
$this->options['data'] = array_merge_recursive(
114+
Arr::get($this->options, 'data', []),
115+
$this->evaluate($data)
116+
);
117+
return $this;
118+
}
119+
120+
public function query(array|Closure $params = []): self
121+
{
122+
$this->query = array_unique(
123+
array_merge(
124+
$this->query,
125+
$params
126+
)
127+
);
128+
return $this;
129+
}
130+
131+
public function options(array|Closure $options = []): static
132+
{
133+
$this->options = array_unique(
134+
array_merge(
135+
$this->options,
136+
$this->evaluate($options)
137+
)
138+
);
139+
return $this;
140+
}
141+
142+
public function toArray(): array
143+
{
144+
return [
145+
'url' => $this->url,
146+
'reload' => $this->reload,
147+
'query' => $this->query,
148+
'options' => array_filter(array_merge([
149+
'method' => 'GET',
150+
'preserveState' => true,
151+
'preserveScroll' => true,
152+
],$this->options)),
153+
];
154+
}
155+
156+
public function __toString(): string
157+
{
158+
return json_encode($this->toArray());
159+
}
160+
}

src/Datatables/Actions/Action.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,16 @@ class Action implements CoreContracts\HasToArray
2222
use Concerns\HasPolling;
2323
use Concerns\HasPermissions;
2424
use Concerns\HasHooks;
25+
use Concerns\HasRedirect;
2526
use Concerns\HasPayload;
2627
use Concerns\CanBeExecuted;
2728
use Concerns\CanClearSelected;
2829
use Concerns\CanBeConfirmed;
2930
use Concerns\CanResetFilters;
3031
use Concerns\CanBeConvertedToModels;
3132
use Concerns\CanRefresh;
33+
use Concerns\HasInertiaAction;
34+
use Concerns\HasHybridlyAction;
3235
use Macroable;
3336

3437
public function __construct()
@@ -58,10 +61,12 @@ protected function ensureDefaults()
5861
->title($this->getName())
5962
->text(trans('laravel-vanilla-components::translations.confirmation.text'));
6063

61-
// Other stuff
64+
// After: Hooks
6265
$this->after['clearSelected'] = $this->getShouldClearSelectionAfterAction() ?? true;
6366
$this->after['resetFilters'] = $this->getShouldClearFiltersAfterAction() ?? false;
6467
$this->after['polling'] = $this->getPolling() ?? $polling;
68+
69+
// Before: Hooks
6570
$this->before['confirm'] = $this->getConfirmation() ?? $confirmation;
6671
$this->before['refresh'] = $this->getShouldRefreshAfterExecuted() ?? true;
6772
}

src/Datatables/Actions/Concerns/HasAfter.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ protected function getAfterToArray(): array
3434

3535
return $value;
3636
})
37+
->when($this->getInertia() !== null, function ($collection) {
38+
return $collection->put('inertia', $this->getInertia()->toArray());
39+
})
40+
->when($this->getRedirectUrl() !== null, function ($collection) {
41+
return $collection->put('redirect', [
42+
'url' => $this->getRedirectUrl(),
43+
'newTab' => $this->shouldRedirectToNewTab(),
44+
]);
45+
})
3746
->toArray();
3847
}
3948
}

0 commit comments

Comments
 (0)