Skip to content

Commit 3d89d4f

Browse files
committed
route update & chart added
1 parent 655dce9 commit 3d89d4f

File tree

5 files changed

+164
-97
lines changed

5 files changed

+164
-97
lines changed

app/Http/Pages/Dashboard.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22

33
namespace App\Http\Pages;
44

5+
use App\Http\Resources\PluginUserResource\Widgets\ActiveChart;
56
use Filament\Pages\Dashboard as BasePage;
67

78
class Dashboard extends BasePage
89
{
9-
// ...
10+
protected function getHeaderWidgets(): array
11+
{
12+
return [ActiveChart::class];
13+
}
1014
}

app/Http/Resources/PluginUserResource.php

+67-54
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,18 @@
44

55
use App\Filament\Resources\PluginUserResource\Pages;
66
use App\Filament\Resources\PluginUserResource\RelationManagers;
7+
use App\Http\Resources\PluginUserResource\Pages\CreatePluginUser;
8+
use App\Http\Resources\PluginUserResource\Pages\EditPluginUser;
9+
use App\Http\Resources\PluginUserResource\Pages\ListPluginUsers;
10+
use App\Http\Resources\PluginUserResource\Widgets\ActiveChart;
711
use App\Models\PluginUser;
812
use Filament\Forms;
913
use Filament\Forms\Components\Actions\Action;
1014
use Filament\Resources\Form;
1115
use Filament\Resources\Resource;
1216
use Filament\Resources\Table;
1317
use Filament\Tables;
18+
use Filament\Widgets\ChartWidget;
1419
use Illuminate\Database\Eloquent\Builder;
1520

1621
class PluginUserResource extends Resource
@@ -24,57 +29,6 @@ class PluginUserResource extends Resource
2429

2530
protected static ?string $navigationIcon = 'heroicon-o-users';
2631

27-
public static function form(Form $form): Form
28-
{
29-
return $form
30-
->schema([
31-
Forms\Components\Card::make(array(
32-
Forms\Components\TextInput::make('name')
33-
->maxLength(20)
34-
->placeholder("Name of user's plugin"),
35-
Forms\Components\TextInput::make('version')
36-
->required()
37-
->mask(fn (Forms\Components\TextInput\Mask $mask) => $mask->pattern('0.0.0'))
38-
->maxLength(10)
39-
->placeholder('current version of plugin'),
40-
Forms\Components\TextInput::make('website')
41-
->url()
42-
->suffixAction(fn (?string $state): Action =>
43-
Action::make('visit')
44-
->icon('heroicon-s-external-link')
45-
->url(
46-
filled($state) ? "https://{$state}" : null,
47-
shouldOpenInNewTab: true,
48-
),
49-
)
50-
->maxLength(100)
51-
->placeholder('User website'),
52-
))->columns(3),
53-
Forms\Components\Card::make([
54-
Forms\Components\KeyValue::make('plugins')
55-
->keyLabel('#')
56-
->disableEditingKeys(true)
57-
->valueLabel('Name'),
58-
Forms\Components\KeyValue::make('server')
59-
->keyLabel('#')
60-
->disableEditingKeys(true)
61-
->valueLabel('Software'),
62-
])->columns(),
63-
64-
Forms\Components\Card::make([
65-
Forms\Components\DatePicker::make('activated_at')
66-
->required()
67-
->placeholder('month day, year'),
68-
Forms\Components\DatePicker::make('deactivated_at')
69-
->placeholder('month day, year'),
70-
Forms\Components\DatePicker::make('uninstalled_at')
71-
->placeholder('month day, year'),
72-
Forms\Components\Radio::make('status')
73-
->options(self::STATUS_ARRAY),
74-
])->columns(3),
75-
]);
76-
}
77-
7832
public static function table(Table $table): Table
7933
{
8034
return $table
@@ -130,6 +84,56 @@ public static function table(Table $table): Table
13084
]);
13185
}
13286

87+
public static function form(Form $form): Form
88+
{
89+
return $form
90+
->schema([
91+
Forms\Components\Card::make(array(
92+
Forms\Components\TextInput::make('name')
93+
->maxLength(20)
94+
->placeholder("Name of user's plugin"),
95+
Forms\Components\TextInput::make('version')
96+
->required()
97+
->mask(fn(Forms\Components\TextInput\Mask $mask) => $mask->pattern('0.0.0'))
98+
->maxLength(10)
99+
->placeholder('current version of plugin'),
100+
Forms\Components\TextInput::make('website')
101+
->url()
102+
->suffixAction(fn(?string $state): Action => Action::make('visit')
103+
->icon('heroicon-s-external-link')
104+
->url(
105+
filled($state) ? "https://{$state}" : null,
106+
shouldOpenInNewTab: true,
107+
),
108+
)
109+
->maxLength(100)
110+
->placeholder('User website'),
111+
))->columns(3),
112+
Forms\Components\Card::make([
113+
Forms\Components\KeyValue::make('plugins')
114+
->keyLabel('#')
115+
->disableEditingKeys(true)
116+
->valueLabel('Name'),
117+
Forms\Components\KeyValue::make('server')
118+
->keyLabel('#')
119+
->disableEditingKeys(true)
120+
->valueLabel('Software'),
121+
])->columns(),
122+
123+
Forms\Components\Card::make([
124+
Forms\Components\DatePicker::make('activated_at')
125+
->required()
126+
->placeholder('month day, year'),
127+
Forms\Components\DatePicker::make('deactivated_at')
128+
->placeholder('month day, year'),
129+
Forms\Components\DatePicker::make('uninstalled_at')
130+
->placeholder('month day, year'),
131+
Forms\Components\Radio::make('status')
132+
->options(self::STATUS_ARRAY),
133+
])->columns(3),
134+
]);
135+
}
136+
133137
public static function getRelations(): array
134138
{
135139
return [
@@ -140,9 +144,18 @@ public static function getRelations(): array
140144
public static function getPages(): array
141145
{
142146
return [
143-
'index' => \App\Http\Resources\PluginUserResource\Pages\ListPluginUsers::route('/'),
144-
'create' => \App\Http\Resources\PluginUserResource\Pages\CreatePluginUser::route('/create'),
145-
'edit' => \App\Http\Resources\PluginUserResource\Pages\EditPluginUser::route('/{record}/edit'),
147+
'index' => ListPluginUsers::route('/'),
148+
'create' => CreatePluginUser::route('/create'),
149+
'edit' => EditPluginUser::route('/{record}/edit'),
150+
];
151+
}
152+
153+
public static function getWidgets(): array
154+
{
155+
return [
156+
ActiveChart::class,
146157
];
147158
}
159+
160+
148161
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace App\Http\Resources\PluginUserResource\Widgets;
4+
5+
use Filament\Widgets\LineChartWidget;
6+
use Illuminate\Support\Facades\DB;
7+
8+
class ActiveChart extends LineChartWidget
9+
{
10+
protected static ?string $heading = 'Activation';
11+
12+
protected function getFilters(): ?array
13+
{
14+
return DB::table('plugin_users')
15+
->select('name')
16+
->groupBy('name')
17+
->orderBy('name')
18+
->get()
19+
->pluck('name', 'name')
20+
->toArray();
21+
}
22+
23+
protected function getData(): array
24+
{
25+
$activeFilter = $this->filter;
26+
$chart = DB::table('plugin_users')
27+
->select(DB::raw("DATE_FORMAT(activated_at, '%m') AS month, SUM(CASE WHEN status = 1 THEN 1 ELSE 0 END) AS activate_count, SUM(CASE WHEN status = 2 THEN 1 ELSE 0 END) AS deactivate_count"))
28+
->whereYear('activated_at', DB::raw('YEAR(CURRENT_DATE)'))
29+
->where('activated_at', '>=', DB::raw("DATE_FORMAT(DATE_SUB(CURRENT_DATE, INTERVAL 6 MONTH), '%Y-%m-01')"))
30+
->groupBy('month')
31+
->when($activeFilter, function ($q) use ($activeFilter) {
32+
return $q->where('name', '=', $activeFilter);
33+
})
34+
->orderBy('month')
35+
->get();
36+
37+
38+
return [
39+
'datasets' => [
40+
[
41+
'label' => 'Active',
42+
'data' => $chart->pluck('activate_count'),
43+
'borderColor' => '#4ade80'
44+
],
45+
[
46+
'label' => 'Deactivate',
47+
'data' => $chart->pluck('deactivate_count'),
48+
'borderColor' => '#f87171'
49+
],
50+
],
51+
'labels' => $chart->pluck('month'),
52+
];
53+
}
54+
}

0 commit comments

Comments
 (0)