Skip to content

Commit

Permalink
widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Mar 19, 2024
1 parent 3dd6108 commit 4b99646
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 24 deletions.
12 changes: 0 additions & 12 deletions src/Resources/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
use Cone\Root\Traits\ResolvesActions;
use Cone\Root\Traits\ResolvesFilters;
use Cone\Root\Traits\ResolvesWidgets;
use Cone\Root\Widgets\Metric;
use Cone\Root\Widgets\Widget;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\MessageBag;
Expand Down Expand Up @@ -349,16 +347,6 @@ protected function resolveAction(Request $request, Action $action): void
$action->setQuery($this->resolveFilteredQuery($request));
}

/**
* Handle the callback for the widget resolution.
*/
protected function resolveWidget(Request $request, Widget $widget): void
{
if ($widget instanceof Metric) {
$widget->setQuery($this->resolveFilteredQuery($request)->clone()->withoutEagerLoads());
}
}

/**
* Get the per page options.
*/
Expand Down
40 changes: 30 additions & 10 deletions src/Widgets/Metric.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Cone\Root\Widgets;

use Closure;
use DateInterval;
use DatePeriod;
use DateTime;
Expand All @@ -11,11 +12,6 @@

abstract class Metric extends Widget
{
/**
* The Eloquent query.
*/
protected ?Builder $query = null;

/**
* The metric timezone.
*/
Expand All @@ -27,11 +23,35 @@ abstract class Metric extends Widget
protected bool $async = true;

/**
* Set the query.
* The query resolver.
*/
protected ?Closure $queryResolver = null;

/**
* Create a new Eloquent query.
*/
abstract public function query(): Builder;

/**
* Resolve the query.
*/
public function resolveQuery(Request $request): Builder
{
$query = $this->query();

if (! is_null($this->queryResolver)) {
call_user_func_array($this->queryResolver, [$request, $query]);
}

return $query;
}

/**
* Set the query resolver callback.
*/
public function setQuery(Builder $query): static
public function resolverQueryUsing(Closure $callback): static
{
$this->query = $query;
$this->queryResolver = $callback;

return $this;
}
Expand Down Expand Up @@ -129,7 +149,7 @@ public function data(Request $request): array
{
return array_merge(parent::data($request), [
'ranges' => $this->ranges(),
'data' => ! $this->async || $request->isTurboFrameRequest() ? $this->calculate($request) : [],
'data' => (! $this->async || $request->isTurboFrameRequest()) ? $this->calculate($request) : [],
]);
}

Expand Down Expand Up @@ -215,6 +235,6 @@ public function result(Builder $query): array
*/
public function calculate(Request $request): array
{
return $this->count($request, $this->query);
return $this->count($request, $this->resolveQuery($request));
}
}
10 changes: 9 additions & 1 deletion stubs/Trend.stub
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,19 @@ use Illuminate\Http\Request;

class {{ class }} extends Trend
{
/**
* Create a new Eloquent query.
*/
public function query(): Builder
{
//
}

/**
* Calculate the results.
*/
public function calculate(Request $request): array
{
return $this->count($request, $this->query);
return $this->count($request, $this->resolveQuery($request));
}
}
10 changes: 9 additions & 1 deletion stubs/Value.stub
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,19 @@ use Illuminate\Http\Request;

class {{ class }} extends Value
{
/**
* Create a new Eloquent query.
*/
public function query(): Builder
{
//
}

/**
* Calculate the results.
*/
public function calculate(Request $request): array
{
return $this->count($request, $this->query);
return $this->count($request, $this->resolveQuery($request));
}
}

0 comments on commit 4b99646

Please sign in to comment.