Skip to content

Commit

Permalink
widgets wip
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Dec 19, 2023
1 parent 7f97389 commit aa35847
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 17 deletions.
6 changes: 3 additions & 3 deletions src/Http/Controllers/WidgetController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ class WidgetController extends Controller
*/
public function __invoke(Request $request): Response
{
$widget = $request->route('rootWidget');
$widget = $request->route('widget');

Gate::allowIf($widget->authorized($request));
// Gate::allowIf($widget->authorized($request));

return new Response($widget->render());
return $widget->toResponse($request);
}
}
13 changes: 13 additions & 0 deletions src/Resources/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
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 @@ -298,6 +300,16 @@ 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));
}
}

/**
* Get the per page options.
*/
Expand Down Expand Up @@ -374,6 +386,7 @@ public function registerRoutes(Request $request, Router $router): void

$router->prefix($this->getUriKey())->group(function (Router $router) use ($request): void {
$this->resolveActions($request)->registerRoutes($request, $router);
$this->resolveWidgets($request)->registerRoutes($request, $router);

$router->prefix('{resourceModel}')->group(function (Router $router) use ($request): void {
$this->resolveFields($request)->registerRoutes($request, $router);
Expand Down
30 changes: 20 additions & 10 deletions src/Widgets/Metric.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,38 @@

namespace Cone\Root\Widgets;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;

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

/**
* Calculate the metric data.
*/
abstract public function calculate(Request $request): array;

/**
* Set the query.
*/
public function setQuery(Builder $query): static
{
$this->query = $query->clone()->withoutEagerLoads();

return $this;
}

/**
* Get the data.
*/
public function toArray(): array
public function data(Request $request): array
{
return array_merge(
parent::toArray(),
App::call(function (Request $request): array {
return [
'data' => $this->calculate($request),
];
}),
);
return array_merge(parent::data($request), [
'data' => $this->calculate($request),
]);
}
}
50 changes: 46 additions & 4 deletions src/Widgets/Widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,27 @@

namespace Cone\Root\Widgets;

use Cone\Root\Http\Controllers\WidgetController;
use Cone\Root\Traits\Authorizable;
use Cone\Root\Traits\HasAttributes;
use Cone\Root\Traits\Makeable;
use Cone\Root\Traits\RegistersRoutes;
use Cone\Root\Traits\ResolvesVisibility;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Responsable;
use Illuminate\Contracts\View\View;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Router;
use Illuminate\Support\Facades\View as ViewFactory;
use Illuminate\Support\Str;
use JsonSerializable;

abstract class Widget implements Arrayable, JsonSerializable
abstract class Widget implements Arrayable, Responsable
{
use Authorizable;
use HasAttributes;
use Makeable;
use RegistersRoutes;
use ResolvesVisibility;

/**
Expand All @@ -39,13 +47,37 @@ public function getName(): string
}

/**
* Convert the element to a JSON serializable format.
* Get the URI key.
*/
public function jsonSerialize(): mixed
public function getUriKey(): string
{
return $this->getKey();
}

/**
* Render the widget.
*/
public function render(): View
{
return ViewFactory::make($this->template);
}

/**
* Get the view data.
*/
public function data(Request $request): array
{
return $this->toArray();
}

/**
* The routes should be registered.
*/
public function routes(Router $router): void
{
$router->get('/', WidgetController::class);
}

/**
* Convert the widget to an array.
*/
Expand All @@ -58,4 +90,14 @@ public function toArray(): array
'template' => $this->template,
];
}

/**
* Convert the widget to an HTTP Response.
*/
public function toResponse($request): Response
{
return new Response(
$this->render()->with($this->data($request))->render()
);
}
}
13 changes: 13 additions & 0 deletions src/Widgets/Widgets.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Routing\Router;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;

Expand Down Expand Up @@ -36,4 +37,16 @@ public function visible(string|array $context): static
{
return $this->filter->visible($context)->values();
}

/**
* Register the widget routes.
*/
public function registerRoutes(Request $request, Router $router): void
{
$router->prefix('widgets')->group(function (Router $router) use ($request): void {
$this->each(static function (Widget $widget) use ($request, $router): void {
$widget->registerRoutes($request, $router);
});
});
}
}

0 comments on commit aa35847

Please sign in to comment.