Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 133 additions & 0 deletions app/Filament/Resources/ArticleResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php

declare(strict_types=1);

namespace App\Filament\Resources;

use App\Filament\Resources\ArticleResource\Pages;
use App\Models\Article;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Actions\Action;
use Filament\Tables\Actions\ActionGroup;
use Filament\Tables\Actions\BulkAction;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\Filter;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;

final class ArticleResource extends Resource
{
protected static ?string $model = Article::class;

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

public static function table(Table $table): Table
{
return $table
->columns([
TextColumn::make('title')
->label('Titre')
->sortable(),
TextColumn::make('status')
->getStateUsing(function ($record) {
if ($record->approved_at) {
return 'Approuver';
} elseif ($record->declined_at) {
return 'Décliner';
} elseif ($record->submitted_at) {
return 'Soumis';
} else {
return 'Brouillon';
}
})
->colors([
'success' => 'Approuver',
'danger' => 'Décliner',
'warning' => 'Soumis',
'default' => 'Brouillon',
])
->badge(),
TextColumn::make('submitted_at')
->label('Date de soumission')
->dateTime(),
TextColumn::make('user.name')
->label('Auteur')
->sortable(),
])
->filters([
Filter::make('submitted_at')->query(fn (Builder $query) => $query->whereNotNull('submitted_at'))->label('Soumis'),
Filter::make('declined_at')->query(fn (Builder $query) => $query->whereNotNull('declined_at'))->label('Décliner'),
Filter::make('approved_at')->query(fn (Builder $query) => $query->whereNotNull('approved_at'))->label('Approuver'),
])

->actions([
ActionGroup::make([
Action::make('approved')
->visible(fn (Article $record) => $record->isAwaitingApproval())
->label('Approuver')
->icon('heroicon-s-check')
->color('success')
->modalHeading(__('Voulez vous approuver cet article'))
->successNotificationTitle(__('Opération effectuée avec succès'))
->requiresConfirmation()
->modalIcon('heroicon-s-check')
->action(function ($record): void {
$record->approved_at = now();
$record->save();
}),
Action::make('declined')
->visible(fn (Article $record) => $record->isAwaitingApproval())
->label('Décliner')
->icon('heroicon-s-x-mark')
->color('warning')
->modalHeading(__('Voulez vous décliner cet article'))
->successNotificationTitle(__('Opération effectuée avec succès'))
->requiresConfirmation()
->modalIcon('heroicon-s-x-mark')
->action(function ($record): void {
$record->declined_at = now();
$record->save();
}),
Tables\Actions\DeleteAction::make('delete'),
]),

])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
BulkAction::make('approved')
->label('Approuver la sélection')
->icon('heroicon-s-check')
->color('success')
->action(fn (Collection $records) => $records->each->update(['approved_at' => now()]))
->deselectRecordsAfterCompletion()
->requiresConfirmation()
->modalIcon('heroicon-s-check')
->modalHeading('Approuver')
->modalSubheading('Voulez-vous vraiment approuver ces articles ?')
->modalButton('Confirmer'),
BulkAction::make('declined')
->label('Décliner la sélection')
->icon('heroicon-s-x-mark')
->color('warning')
->action(fn (Collection $records) => $records->each->update(['declined_at' => now()]))
->deselectRecordsAfterCompletion()
->requiresConfirmation()
->modalIcon('heroicon-s-x-mark')
->modalHeading('Décliner')
->modalSubheading('Voulez-vous vraiment décliner ces articles ?')
->modalButton('Confirmer'),

Tables\Actions\DeleteBulkAction::make(),
]),
]);
}

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

declare(strict_types=1);

namespace App\Filament\Resources\ArticleResource\Pages;

use App\Filament\Resources\ArticleResource;
use Filament\Resources\Pages\ListRecords;

final class ListArticles extends ListRecords
{
protected static string $resource = ArticleResource::class;
}
61 changes: 61 additions & 0 deletions tests/Feature/Filament/ArticleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

use App\Filament\Resources\ArticleResource;
use App\Models\Article;
use Livewire\Livewire;

beforeEach(function (): void {
$this->user = $this->login();
$this->articles = Article::factory()->count(10)->create([
'submitted_at' => now(),
]);
});

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

it('page can display table with records', function (): void {
Livewire::test(ArticleResource\Pages\ListArticles::class)
->assertCanSeeTableRecords($this->articles);
});

it('admin user can approved article', function (): void {
$article = $this->articles->first();

Livewire::test(ArticleResource\Pages\ListArticles::class)
->callTableAction('approved', $article);

$article->refresh();

expect($article->approved_at)
->not
->toBe(null)
->and($article->declined_at)
->toBe(null);

Livewire::test(ArticleResource\Pages\ListArticles::class)
->assertTableActionHidden('approved', $article)
->assertTableActionHidden('declined', $article);
});

it('admin user can declined article', function (): void {
$article = $this->articles->first();

Livewire::test(ArticleResource\Pages\ListArticles::class)
->callTableAction('declined', $article);

$article->refresh();

expect($article->declined_at)
->not
->toBe(null)
->and($article->approved_at)
->toBe(null);

Livewire::test(ArticleResource\Pages\ListArticles::class)
->assertTableActionHidden('approved', $article)
->assertTableActionHidden('declined', $article);

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