Skip to content

Commit

Permalink
metrics wip
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Sep 23, 2023
1 parent dbddde2 commit 7985953
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 84 deletions.
51 changes: 3 additions & 48 deletions resources/views/dashboard.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,53 +6,8 @@
{{-- Content --}}
@section('content')
<div class="l-row l-row--column:sm:2 l-row--column:lg:3">
<div class="app-widget app-widget--primary">
<div class="app-widget__column">
<h2 class="app-widget__title">Orders</h2>
<p class="app-widget__data">65</p>
<div class="trending trending--up app-widget__trending">
<span class="trending__caption">+12%</span>
<svg aria-hidden='true' fill='none' focusable='false' height='24' stroke-linecap='round' stroke-linejoin='round' stroke-width='2' stroke='currentColor' viewBox='0 0 24 24' width='24' xmlns='http://www.w3.org/2000/svg' class='trending__icon'>
<polyline points='23 6 13.5 15.5 8.5 10.5 1 18'></polyline>
<polyline points='17 6 23 6 23 12'></polyline>
</svg>
</div>
</div>
<div class="app-widget__chart">
<div id="chart01"></div>
</div>
</div>
<div class="app-widget ">
<div class="app-widget__column">
<h2 class="app-widget__title">Customers</h2>
<p class="app-widget__data">54</p>
<div class="trending trending--up app-widget__trending">
<span class="trending__caption">+6%</span>
<svg aria-hidden='true' fill='none' focusable='false' height='24' stroke-linecap='round' stroke-linejoin='round' stroke-width='2' stroke='currentColor' viewBox='0 0 24 24' width='24' xmlns='http://www.w3.org/2000/svg' class='trending__icon'>
<polyline points='23 6 13.5 15.5 8.5 10.5 1 18'></polyline>
<polyline points='17 6 23 6 23 12'></polyline>
</svg>
</div>
</div>
<div class="app-widget__chart">
<div id="chart02"></div>
</div>
</div>
<div class="app-widget ">
<div class="app-widget__column">
<h2 class="app-widget__title">Users</h2>
<p class="app-widget__data">184</p>
<div class="trending trending--down app-widget__trending">
<span class="trending__caption">-12%</span>
<svg aria-hidden='true' fill='none' focusable='false' height='24' stroke-linecap='round' stroke-linejoin='round' stroke-width='2' stroke='currentColor' viewBox='0 0 24 24' width='24' xmlns='http://www.w3.org/2000/svg' class='trending__icon'>
<polyline points='23 18 13.5 8.5 8.5 13.5 1 6'></polyline>
<polyline points='17 18 23 18 23 12'></polyline>
</svg>
</div>
</div>
<div class="app-widget__chart">
<div id="chart03"></div>
</div>
</div>
@foreach($widgets as $widget)
{!! $widget !!}
@endforeach
</div>
@endsection
6 changes: 3 additions & 3 deletions resources/views/resources/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

{{-- Content --}}
@section('content')
{{-- @if($widgets->isNotEmpty())
@if(! empty($widgets))
<div class="l-row l-row--column:sm:2 l-row--column:lg:3">
@foreach($widgets->all() as $widget)
@foreach($widgets as $widget)
{!! $widget !!}
@endforeach
</div>
@endif --}}
@endif

{!! $table !!}
@endsection
13 changes: 13 additions & 0 deletions resources/views/widgets/trend.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<div class="app-widget app-widget--primary">
<div class="app-widget__column">
<h2 class="app-widget__title">{{ $name }}</h2>
<p class="app-widget__data">65</p>
<div class="trending trending--up app-widget__trending">
<span class="trending__caption">+12%</span>
<x-root::icon name="trending-up" class="trending__icon" />
</div>
</div>
<div class="app-widget__chart">
<div id="chart01"></div>
</div>
</div>
23 changes: 1 addition & 22 deletions resources/views/widgets/widget.blade.php
Original file line number Diff line number Diff line change
@@ -1,22 +1 @@
<div class="app-widget app-widget--summary">
<div class="app-widget__icon">
<svg aria-hidden='true' fill='none' focusable='false' height='24' stroke-linecap='round' stroke-linejoin='round' stroke-width='2' stroke='currentColor' viewBox='0 0 24 24' width='24' xmlns='http://www.w3.org/2000/svg' class='icon'>
<path d='M6 2L3 6v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2V6l-3-4z'></path>
<line x1='3' y1='6' x2='21' y2='6'></line>
<path d='M16 10a4 4 0 0 1-8 0'></path>
</svg>
</div>
<div class="app-widget__column">
<h2 class="app-widget__title">Total Products</h2>
<div class="app-widget__data-row">
<p class="app-widget__data">341</p>
<div class="trending trending--up widget__trending">
<span class="trending__caption">+12%</span>
<svg aria-hidden='true' fill='none' focusable='false' height='24' stroke-linecap='round' stroke-linejoin='round' stroke-width='2' stroke='currentColor' viewBox='0 0 24 24' width='24' xmlns='http://www.w3.org/2000/svg' class='trending__icon'>
<polyline points='23 6 13.5 15.5 8.5 10.5 1 18'></polyline>
<polyline points='17 6 23 6 23 12'></polyline>
</svg>
</div>
</div>
</div>
</div>
<div class="app-widget"></div>
15 changes: 14 additions & 1 deletion src/Form/Fields/Fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Closure;
use Cone\Root\Form\Form;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Traits\ForwardsCalls;

Expand All @@ -31,14 +32,26 @@ public function __construct(Form $form, array $fields = [])
$this->fields = new Collection($fields);
}

/**
* Register the given fields.
*/
public function register(array|Field $fields): static
{
foreach (Arr::wrap($fields) as $field) {
$this->fields->push($field);
}

return $this;
}

/**
* Add a new field to the collection.
*/
public function field(string $field, string $label, string $modelAttribute = null, ...$params): Field
{
$instance = new $field($this->form, $label, $modelAttribute, ...$params);

$this->push($instance);
$this->register($field);

return $instance;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Controllers/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class DashboardController extends Controller
public function __invoke(Request $request, Root $root): Response
{
return ResponseFactory::View('root::dashboard', [
'widgets' => $root->widgets,
'widgets' => $root->widgets->all(),
]);
}
}
1 change: 1 addition & 0 deletions src/Resources/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ public function toIndex(Request $request): array
return array_merge($this->toArray(), [
'title' => $this->getName(),
'table' => $this->toTable($request, $this->resolveQuery($request)),
'widgets' => $this->resolveWidgets($request)->all(),
]);
}

Expand Down
31 changes: 23 additions & 8 deletions src/Traits/ResolvesWidgets.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,46 @@

namespace Cone\Root\Traits;

use Closure;
use Cone\Root\Widgets\Widget;
use Cone\Root\Widgets\Widgets;
use Illuminate\Http\Request;

trait ResolvesWidgets
{
/**
* The resolved widgets.
* The widgets collection.
*/
public readonly Widgets $widgets;
protected ?Widgets $widgets = null;

/**
* Define the widgets.
*/
public function widgets(): array
public function widgets(Request $request): array
{
return [];
}

/**
* Apply the callback on the widgets.
* Resolve the widgets collection.
*/
public function withWidgets(Closure $callback): static
public function resolveWidgets(Request $request): Widgets
{
call_user_func_array($callback, [$this->widgets]);
if (is_null($this->widgets)) {
$this->widgets = new Widgets($this->widgets($request));

return $this;
$this->widgets->each(function (Widget $widget) use ($request): void {
$this->resolveWidget($request, $widget);
});
}

return $this->widgets;
}

/**
* Handle the callback for the widget resolution.
*/
protected function resolveWidget(Request $request, Widget $widget): void
{
//
}
}
24 changes: 23 additions & 1 deletion src/Widgets/Metric.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,29 @@

namespace Cone\Root\Widgets;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Redis;

abstract class Metric extends Widget
{
//
/**
* Calculate the metric data.
*/
abstract public function calculate(Request $request): array;

/**
* Get the data.
*/
public function toArray(): array
{
return array_merge(
parent::toArray(),
App::call(function (Request $request): array {
return [
'data' => $this->calculate($request),
];
}),
);
}
}
21 changes: 21 additions & 0 deletions src/Widgets/Trend.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Cone\Root\Widgets;

use Illuminate\Http\Request;

abstract class Trend extends Metric
{
/**
* The Blade template.
*/
protected string $template = 'root::widgets.trend';

/**
* Calculate the metric data.
*/
public function calculate(Request $request): array
{
return [];
}
}

0 comments on commit 7985953

Please sign in to comment.