Skip to content

Commit 6661b64

Browse files
feat:[LAR-33] Add filament channel crud in cpanel with feature test
1 parent 4d67906 commit 6661b64

File tree

5 files changed

+205
-0
lines changed

5 files changed

+205
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Filament\Resources;
6+
7+
use App\Filament\Resources\ChannelResource\Pages;
8+
use App\Models\Channel;
9+
use Filament\Forms;
10+
use Filament\Forms\Form;
11+
use Filament\Forms\Get;
12+
use Filament\Forms\Set;
13+
use Filament\Resources\Resource;
14+
use Filament\Tables;
15+
use Filament\Tables\Actions\ActionGroup;
16+
use Filament\Tables\Table;
17+
use Illuminate\Database\Eloquent\Builder;
18+
use Illuminate\Support\Str;
19+
20+
final class ChannelResource extends Resource
21+
{
22+
protected static ?string $model = Channel::class;
23+
24+
protected static ?string $navigationIcon = 'heroicon-o-queue-list';
25+
26+
public static function form(Form $form): Form
27+
{
28+
return $form
29+
->schema([
30+
Forms\Components\TextInput::make('name')
31+
->required()
32+
->live(onBlur: true)
33+
->afterStateUpdated(function (Get $get, Set $set, ?string $old, ?string $state): void {
34+
if (($get('slug') ?? '') !== Str::slug($old)) {
35+
return;
36+
}
37+
38+
$set('slug', Str::slug($state));
39+
}),
40+
Forms\Components\TextInput::make('slug')
41+
->required(),
42+
Forms\Components\Select::make('parent_id')
43+
->relationship('parent', 'name', modifyQueryUsing: fn (Builder $query) => $query->whereNull('parent_id'))
44+
->default(null),
45+
Forms\Components\TextInput::make('color')
46+
->maxLength(255)
47+
->type('color'),
48+
]);
49+
}
50+
51+
public static function table(Table $table): Table
52+
{
53+
return $table
54+
->columns([
55+
Tables\Columns\TextColumn::make('name')
56+
->searchable(),
57+
Tables\Columns\TextColumn::make('slug')
58+
->searchable(),
59+
Tables\Columns\TextColumn::make('parent.name')
60+
->numeric()
61+
->sortable(),
62+
Tables\Columns\TextColumn::make('color')
63+
->searchable(),
64+
Tables\Columns\TextColumn::make('created_at')
65+
->dateTime()
66+
->sortable()
67+
->toggleable(isToggledHiddenByDefault: true),
68+
Tables\Columns\TextColumn::make('updated_at')
69+
->dateTime()
70+
->sortable()
71+
->toggleable(isToggledHiddenByDefault: true),
72+
])
73+
->filters([
74+
//
75+
])
76+
->actions([
77+
ActionGroup::make([
78+
Tables\Actions\DeleteAction::make(),
79+
Tables\Actions\EditAction::make()
80+
->color('warning'),
81+
]),
82+
])
83+
->bulkActions([
84+
Tables\Actions\BulkActionGroup::make([
85+
Tables\Actions\DeleteBulkAction::make(),
86+
]),
87+
]);
88+
}
89+
90+
public static function getPages(): array
91+
{
92+
return [
93+
'index' => Pages\ListChannels::route('/'),
94+
'create' => Pages\CreateChannel::route('/create'),
95+
'edit' => Pages\EditChannel::route('/{record}/edit'),
96+
];
97+
}
98+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Filament\Resources\ChannelResource\Pages;
6+
7+
use App\Filament\Resources\ChannelResource;
8+
use Filament\Resources\Pages\CreateRecord;
9+
10+
final class CreateChannel extends CreateRecord
11+
{
12+
protected static string $resource = ChannelResource::class;
13+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Filament\Resources\ChannelResource\Pages;
6+
7+
use App\Filament\Resources\ChannelResource;
8+
use Filament\Actions;
9+
use Filament\Resources\Pages\EditRecord;
10+
11+
final class EditChannel extends EditRecord
12+
{
13+
protected static string $resource = ChannelResource::class;
14+
15+
protected function getHeaderActions(): array
16+
{
17+
return [
18+
Actions\DeleteAction::make(),
19+
];
20+
}
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Filament\Resources\ChannelResource\Pages;
6+
7+
use App\Filament\Resources\ChannelResource;
8+
use Filament\Actions;
9+
use Filament\Resources\Pages\ListRecords;
10+
11+
final class ListChannels extends ListRecords
12+
{
13+
protected static string $resource = ChannelResource::class;
14+
15+
protected function getHeaderActions(): array
16+
{
17+
return [
18+
Actions\CreateAction::make(),
19+
];
20+
}
21+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use App\Filament\Resources\ChannelResource;
6+
use App\Filament\Resources\ChannelResource\Pages\CreateChannel;
7+
use App\Filament\Resources\ChannelResource\Pages\ListChannels;
8+
use App\Models\Channel;
9+
use Filament\Actions\EditAction;
10+
use Livewire\Livewire;
11+
12+
beforeEach(function (): void {
13+
$this->user = $this->login();
14+
$this->channels = Channel::factory()
15+
->count(10)
16+
->create();
17+
});
18+
19+
describe(ChannelResource::class, function (): void {
20+
21+
it('page can display table with records', function (): void {
22+
Livewire::test(ListChannels::class)
23+
->assertCanSeeTableRecords($this->channels);
24+
});
25+
26+
it('Admin user can create channel', function (): void {
27+
$name = 'my channel';
28+
29+
Livewire::test(CreateChannel::class)
30+
->fillForm([
31+
'name' => $name,
32+
'color' => '#FFFFFF',
33+
])
34+
->call('create');
35+
});
36+
37+
it('Admin user can edit channel', function (): void {
38+
$channel = Channel::factory()->create();
39+
40+
Livewire::test(ListChannels::class)
41+
->callTableAction(EditAction::class, $channel, data: [
42+
'name' => 'Edited channel',
43+
])
44+
->assertHasNoTableActionErrors();
45+
46+
$channel->refresh();
47+
48+
expect($channel->name)
49+
->toBe('Edited channel');
50+
});
51+
52+
})->group('channels');

0 commit comments

Comments
 (0)