Skip to content

feat:[LAR-33] Add filament channel crud in cpanel with feature test #204

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

98 changes: 98 additions & 0 deletions app/Filament/Resources/ChannelResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

declare(strict_types=1);

namespace App\Filament\Resources;

use App\Filament\Resources\ChannelResource\Pages;
use App\Models\Channel;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Forms\Get;
use Filament\Forms\Set;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Actions\ActionGroup;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Str;

final class ChannelResource extends Resource
{
protected static ?string $model = Channel::class;

protected static ?string $navigationIcon = 'heroicon-o-queue-list';

public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('name')
->required()
->live(onBlur: true)
->afterStateUpdated(function (Get $get, Set $set, ?string $old, ?string $state): void {
if (($get('slug') ?? '') !== Str::slug($old)) {

Check failure on line 34 in app/Filament/Resources/ChannelResource.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter #1 $title of static method Illuminate\Support\Str::slug() expects string, string|null given.
return;
}

$set('slug', Str::slug($state));

Check failure on line 38 in app/Filament/Resources/ChannelResource.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter #1 $title of static method Illuminate\Support\Str::slug() expects string, string|null given.
}),
Forms\Components\TextInput::make('slug')
->required(),
Forms\Components\Select::make('parent_id')
->relationship('parent', 'name', modifyQueryUsing: fn (Builder $query) => $query->whereNull('parent_id'))
->default(null),
Forms\Components\TextInput::make('color')
->maxLength(255)
->type('color'),
]);
}

public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('name')
->searchable(),
Tables\Columns\TextColumn::make('slug')
->searchable(),
Tables\Columns\TextColumn::make('parent.name')
->numeric()
->sortable(),
Tables\Columns\TextColumn::make('color')
->searchable(),
Tables\Columns\TextColumn::make('created_at')
->dateTime()
->sortable()
->toggleable(isToggledHiddenByDefault: true),
Tables\Columns\TextColumn::make('updated_at')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pas nécessaire. Rajoute plutôt le nombre total de threads que chaque channel possède. Et le tableau doit être un peu plus en accord avec ce qu'on en a BD. Tu affiches les channels parents et quand on déroule on voit les autres channels enfants

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dans le tableau il est possible de faire un tree?
car j'ai parcouru et je n'ai pas vue de solution dessus par contre dans un formulaire j'ai vue SelectTree de
https://filamentphp.com/plugins/codewithdennis-select-tree

->dateTime()
->sortable()
->toggleable(isToggledHiddenByDefault: true),
])
->filters([
//
])
->actions([
ActionGroup::make([
Tables\Actions\DeleteAction::make(),
Tables\Actions\EditAction::make()
->color('warning'),
]),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
]);
}

public static function getPages(): array
{
return [
'index' => Pages\ListChannels::route('/'),
'create' => Pages\CreateChannel::route('/create'),
'edit' => Pages\EditChannel::route('/{record}/edit'),
];
}
}
13 changes: 13 additions & 0 deletions app/Filament/Resources/ChannelResource/Pages/CreateChannel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace App\Filament\Resources\ChannelResource\Pages;

use App\Filament\Resources\ChannelResource;
use Filament\Resources\Pages\CreateRecord;

final class CreateChannel extends CreateRecord
{
protected static string $resource = ChannelResource::class;
}
21 changes: 21 additions & 0 deletions app/Filament/Resources/ChannelResource/Pages/EditChannel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace App\Filament\Resources\ChannelResource\Pages;

use App\Filament\Resources\ChannelResource;
use Filament\Actions;
use Filament\Resources\Pages\EditRecord;

final class EditChannel extends EditRecord
{
protected static string $resource = ChannelResource::class;

protected function getHeaderActions(): array
{
return [
Actions\DeleteAction::make(),
];
}
}
21 changes: 21 additions & 0 deletions app/Filament/Resources/ChannelResource/Pages/ListChannels.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace App\Filament\Resources\ChannelResource\Pages;

use App\Filament\Resources\ChannelResource;
use Filament\Actions;
use Filament\Resources\Pages\ListRecords;

final class ListChannels extends ListRecords
{
protected static string $resource = ChannelResource::class;

protected function getHeaderActions(): array
{
return [
Actions\CreateAction::make(),
];
}
}
52 changes: 52 additions & 0 deletions tests/Feature/Filament/ChannelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

use App\Filament\Resources\ChannelResource;
use App\Filament\Resources\ChannelResource\Pages\CreateChannel;
use App\Filament\Resources\ChannelResource\Pages\ListChannels;
use App\Models\Channel;
use Filament\Actions\EditAction;
use Livewire\Livewire;

beforeEach(function (): void {
$this->user = $this->login();
$this->channels = Channel::factory()
->count(10)
->create();
});

describe(ChannelResource::class, function (): void {

it('page can display table with records', function (): void {
Livewire::test(ListChannels::class)
->assertCanSeeTableRecords($this->channels);
});

it('Admin user can create channel', function (): void {
$name = 'my channel';

Livewire::test(CreateChannel::class)
->fillForm([
'name' => $name,
'color' => '#FFFFFF',
])
->call('create');
});

it('Admin user can edit channel', function (): void {
$channel = Channel::factory()->create();

Livewire::test(ListChannels::class)
->callTableAction(EditAction::class, $channel, data: [
'name' => 'Edited channel',
])
->assertHasNoTableActionErrors();

$channel->refresh();

expect($channel->name)
->toBe('Edited channel');
});

})->group('channels');
Loading