Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Mar 19, 2024
1 parent 4b99646 commit 70c8a8a
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 73 deletions.
85 changes: 38 additions & 47 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 17 additions & 16 deletions src/Actions/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Cone\Root\Actions;

use Closure;
use Cone\Root\Exceptions\QueryResolutionException;
use Cone\Root\Fields\Field;
use Cone\Root\Fields\Relation;
Expand Down Expand Up @@ -59,9 +60,9 @@ abstract class Action implements Arrayable, Form, JsonSerializable
protected bool $standalone = false;

/**
* The Eloquent query.
* The query resolver.
*/
protected ?Builder $query = null;
protected ?Closure $queryResolver = null;

/**
* Handle the action.
Expand Down Expand Up @@ -101,25 +102,25 @@ public function getModalKey(): string
}

/**
* Set the Eloquent query.
* Resolve the query.
*/
public function setQuery(Builder $query): static
public function resolveQuery(Request $request): Builder
{
$this->query = $query;
if (is_null($this->queryResolver)) {
throw new QueryResolutionException();
}

return $this;
return call_user_func_array($this->queryResolver, [$request]);
}

/**
* Get the Eloquent query.
* Set the query resolver callback.
*/
public function getQuery(): Builder
public function withQuery(Closure $callback): static
{
if (is_null($this->query)) {
throw new QueryResolutionException();
}
$this->queryResolver = $callback;

return $this->query;
return $this;
}

/**
Expand Down Expand Up @@ -203,8 +204,8 @@ public function handleFormRequest(Request $request, Model $model): void

$models = match (true) {
$this->isStandalone() => new Collection([$model]),
$request->boolean('all') => $this->getQuery()->get(),
default => $this->getQuery()->findMany($request->input('models', [])),
$request->boolean('all') => $this->resolveQuery($request)->get(),
default => $this->resolveQuery($request)->findMany($request->input('models', [])),
};

$this->handle($request, $models);
Expand All @@ -215,7 +216,7 @@ public function handleFormRequest(Request $request, Model $model): void
*/
public function perform(Request $request): Response
{
$this->handleFormRequest($request, $this->getQuery()->getModel());
$this->handleFormRequest($request, $this->resolveQuery($request)->getModel());

return Redirect::back()->with(
sprintf('alerts.action-%s', $this->getKey()),
Expand Down Expand Up @@ -273,7 +274,6 @@ public function toArray(): array
'modalKey' => $this->getModalKey(),
'name' => $this->getName(),
'template' => $this->template,
'url' => $this->getUri(),
];
}

Expand All @@ -283,6 +283,7 @@ public function toArray(): array
public function toForm(Request $request, Model $model): array
{
return array_merge($this->toArray(), [
'url' => ! is_null($request->route()) ? $this->replaceRoutePlaceholders($request->route()) : null,
'open' => $this->errors($request)->isNotEmpty(),
'fields' => $this->resolveFields($request)->mapToInputs($request, $model),
]);
Expand Down
13 changes: 13 additions & 0 deletions src/Fields/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Cone\Root\Fields;

use Closure;
use Cone\Root\Actions\Action;
use Cone\Root\Exceptions\SaveFormDataException;
use Cone\Root\Filters\Filter;
use Cone\Root\Filters\RenderableFilter;
Expand Down Expand Up @@ -409,6 +410,18 @@ protected function resolveField(Request $request, Field $field): void
}
}

/**
* Handle the callback for the field resolution.
*/
protected function resolveAction(Request $request, Action $action): void
{
$action->withQuery(function (Request $request): Builder {
$model = $request->route('resourceModel');

return $this->resolveFilters($request)->apply($request, $this->getRelation($model)->getQuery());
});
}

/**
* Handle the callback for the filter resolution.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ protected function resolveFilter(Request $request, Filter $filter): void
*/
protected function resolveAction(Request $request, Action $action): void
{
$action->setQuery($this->resolveFilteredQuery($request));
$action->withQuery(fn (): Builder => $this->resolveFilteredQuery($request));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Widgets/Metric.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function resolveQuery(Request $request): Builder
/**
* Set the query resolver callback.
*/
public function resolverQueryUsing(Closure $callback): static
public function withQuery(Closure $callback): static
{
$this->queryResolver = $callback;

Expand Down
Loading

0 comments on commit 70c8a8a

Please sign in to comment.