-
-
Notifications
You must be signed in to change notification settings - Fork 78
Add devblog #179
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
Merged
Merged
Add devblog #179
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
7d073ef
add blog to menu's
gwleuverink 2234621
add article model & factory
gwleuverink 975a805
wire up article listing & pagination
gwleuverink 7d5a86f
wire up article detail
gwleuverink 802ebbe
add published_at guards
gwleuverink 7bf7b15
add article scheduling tests
gwleuverink 9136db1
wip - tidy scope
gwleuverink 9b62780
add filament resource
gwleuverink 007090b
add dark mode styles for prose elements
gwleuverink 00f083d
add publish & schedule actions
gwleuverink f0efe11
tidy - improve filament article sorting
gwleuverink daa961c
wip - validation
gwleuverink beb8a4d
add unpublish action
gwleuverink 53c3927
add preview action and update user admin check
gwleuverink 1afbdf9
add article preview tests
gwleuverink 4f50cf9
update auto-slug logic
gwleuverink File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
<?php | ||
|
||
namespace App\Filament\Resources; | ||
|
||
use App\Filament\Resources\ArticleResource\Actions\PreviewAction; | ||
use App\Filament\Resources\ArticleResource\Actions\PublishAction; | ||
use App\Filament\Resources\ArticleResource\Actions\ScheduleAction; | ||
use App\Filament\Resources\ArticleResource\Actions\UnpublishAction; | ||
use App\Filament\Resources\ArticleResource\Pages; | ||
use App\Models\Article; | ||
use Filament\Forms\Components\MarkdownEditor; | ||
use Filament\Forms\Components\Textarea; | ||
use Filament\Forms\Components\TextInput; | ||
use Filament\Forms\Form; | ||
use Filament\Forms\Set; | ||
use Filament\Resources\Resource; | ||
use Filament\Tables; | ||
use Filament\Tables\Actions\ActionGroup; | ||
use Filament\Tables\Columns\TextColumn; | ||
use Filament\Tables\Table; | ||
use Illuminate\Contracts\Database\Eloquent\Builder; | ||
use Illuminate\Support\Str; | ||
|
||
class ArticleResource extends Resource | ||
{ | ||
protected static ?string $model = Article::class; | ||
|
||
protected static ?string $recordRouteKeyName = 'id'; | ||
|
||
protected static ?string $recordTitleAttribute = 'title'; | ||
|
||
protected static ?string $navigationIcon = 'heroicon-o-newspaper'; | ||
|
||
public static function form(Form $form): Form | ||
{ | ||
return $form | ||
->schema([ | ||
TextInput::make('title') | ||
->required() | ||
->maxLength(255) | ||
->live(onBlur: true) | ||
->afterStateUpdated(function (Article $article, Set $set, ?string $state) { | ||
if ($article->isPublished()) { | ||
return; | ||
} | ||
|
||
$set('slug', Str::slug($state)); | ||
}), | ||
|
||
TextInput::make('slug') | ||
->required() | ||
->maxLength(255) | ||
->live(onBlur: true) | ||
->unique(Article::class, 'slug', ignoreRecord: true) | ||
->disabled(fn (Article $article) => $article->isPublished()) | ||
->afterStateUpdated( | ||
fn (Set $set, ?string $state) => $set('slug', Str::slug($state)) | ||
) | ||
->helperText(fn (Article $article) => $article->isPublished() | ||
? 'The slug cannot be changed after the article is published.' | ||
: false | ||
), | ||
|
||
Textarea::make('excerpt') | ||
->required() | ||
->maxLength(400) | ||
->columnSpanFull(), | ||
|
||
MarkdownEditor::make('content') | ||
->required() | ||
->columnSpanFull(), | ||
]); | ||
} | ||
|
||
public static function table(Table $table): Table | ||
{ | ||
return $table | ||
->columns([ | ||
TextColumn::make('title') | ||
->searchable() | ||
->sortable(), | ||
|
||
TextColumn::make('excerpt') | ||
->searchable() | ||
->limit(50), | ||
|
||
TextColumn::make('author.name') | ||
->label('Author') | ||
->searchable() | ||
->sortable(), | ||
|
||
TextColumn::make('published_at') | ||
->badge() | ||
->dateTime('M j, Y H:i') | ||
->color(fn ($state) => $state && $state->isPast() ? 'success' : 'warning') | ||
->sortable(query: function (Builder $query, string $direction): Builder { | ||
return $query->orderByRaw("published_at IS NULL {$direction}, published_at {$direction}"); | ||
}), | ||
]) | ||
->filters([ | ||
// | ||
]) | ||
->actions([ | ||
ActionGroup::make([ | ||
PreviewAction::make('preview'), | ||
Tables\Actions\EditAction::make()->url(fn ($record) => static::getUrl('edit', ['record' => $record->id])), | ||
UnpublishAction::make('unpublish'), | ||
PublishAction::make('publish'), | ||
ScheduleAction::make('schedule'), | ||
]), | ||
]) | ||
->bulkActions([ | ||
Tables\Actions\BulkActionGroup::make([ | ||
Tables\Actions\DeleteBulkAction::make(), | ||
]), | ||
]) | ||
->defaultSort('published_at', 'desc'); | ||
} | ||
|
||
public static function getRelations(): array | ||
{ | ||
return [ | ||
// | ||
]; | ||
} | ||
|
||
public static function getPages(): array | ||
{ | ||
return [ | ||
'index' => Pages\ListArticles::route('/'), | ||
'create' => Pages\CreateArticle::route('/create'), | ||
'edit' => Pages\EditArticle::route('/{record}/edit'), | ||
]; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
app/Filament/Resources/ArticleResource/Actions/PreviewAction.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace App\Filament\Resources\ArticleResource\Actions; | ||
|
||
use App\Models\Article; | ||
use Filament\Tables\Actions\Action; | ||
|
||
class PreviewAction extends Action | ||
{ | ||
protected function setUp(): void | ||
{ | ||
$this | ||
->label('Preview') | ||
->icon('heroicon-o-eye') | ||
->url(fn (Article $article) => route('article', $article)) | ||
->openUrlInNewTab(); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
app/Filament/Resources/ArticleResource/Actions/PublishAction.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace App\Filament\Resources\ArticleResource\Actions; | ||
|
||
use App\Models\Article; | ||
use Filament\Tables\Actions\Action; | ||
|
||
class PublishAction extends Action | ||
{ | ||
protected function setUp(): void | ||
{ | ||
$this | ||
->label('Publish') | ||
->icon('heroicon-o-newspaper') | ||
->action(fn (Article $article) => $article->publish()) | ||
->visible(fn (Article $article) => ! $article->isPublished()) | ||
->requiresConfirmation(); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
app/Filament/Resources/ArticleResource/Actions/ScheduleAction.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace App\Filament\Resources\ArticleResource\Actions; | ||
|
||
use App\Models\Article; | ||
use Filament\Forms\Components\DateTimePicker; | ||
use Filament\Tables\Actions\Action; | ||
use Illuminate\Support\Carbon; | ||
|
||
class ScheduleAction extends Action | ||
{ | ||
protected function setUp(): void | ||
{ | ||
$this | ||
->label('Schedule') | ||
->icon('heroicon-o-calendar-days') | ||
->visible(fn (Article $record) => ! $record->isPublished()) | ||
->form(fn (Article $article) => [ | ||
DateTimePicker::make('published_at') | ||
->label('Published At') | ||
->displayFormat('M j, Y H:i') | ||
->seconds(false) | ||
->default($article->published_at) | ||
->afterOrEqual('now') | ||
->required(), | ||
]) | ||
->action(function (Article $article, array $data) { | ||
$article->publish(Carbon::parse($data['published_at'])); | ||
}) | ||
->requiresConfirmation(); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
app/Filament/Resources/ArticleResource/Actions/UnpublishAction.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace App\Filament\Resources\ArticleResource\Actions; | ||
|
||
use App\Models\Article; | ||
use Filament\Tables\Actions\Action; | ||
|
||
class UnpublishAction extends Action | ||
{ | ||
protected function setUp(): void | ||
{ | ||
$this | ||
->label('Unpublish') | ||
->icon('heroicon-o-archive-box-x-mark') | ||
->action(fn (Article $article) => $article->unpublish()) | ||
->visible(fn (Article $article) => $article->isPublished()) | ||
->requiresConfirmation(); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
app/Filament/Resources/ArticleResource/Pages/CreateArticle.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace App\Filament\Resources\ArticleResource\Pages; | ||
|
||
use App\Filament\Resources\ArticleResource; | ||
use Filament\Resources\Pages\CreateRecord; | ||
|
||
class CreateArticle extends CreateRecord | ||
{ | ||
protected static string $resource = ArticleResource::class; | ||
|
||
protected function getRedirectUrl(): string | ||
{ | ||
return static::getResource()::getUrl('edit', ['record' => $this->record->id]); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
app/Filament/Resources/ArticleResource/Pages/EditArticle.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace App\Filament\Resources\ArticleResource\Pages; | ||
|
||
use App\Filament\Resources\ArticleResource; | ||
use Filament\Actions; | ||
use Filament\Resources\Pages\EditRecord; | ||
|
||
class EditArticle extends EditRecord | ||
{ | ||
protected static string $resource = ArticleResource::class; | ||
|
||
protected function getHeaderActions(): array | ||
{ | ||
return [ | ||
Actions\DeleteAction::make(), | ||
]; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
app/Filament/Resources/ArticleResource/Pages/ListArticles.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace App\Filament\Resources\ArticleResource\Pages; | ||
|
||
use App\Filament\Resources\ArticleResource; | ||
use Filament\Actions; | ||
use Filament\Resources\Pages\ListRecords; | ||
|
||
class ListArticles extends ListRecords | ||
{ | ||
protected static string $resource = ArticleResource::class; | ||
|
||
protected function getHeaderActions(): array | ||
{ | ||
return [ | ||
Actions\CreateAction::make(), | ||
]; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Models\Article; | ||
|
||
class ShowBlogController extends Controller | ||
{ | ||
public function index() | ||
{ | ||
$articles = Article::query() | ||
->published() | ||
->paginate(6); | ||
|
||
return view('blog', [ | ||
'articles' => $articles, | ||
]); | ||
} | ||
|
||
public function show(Article $article) | ||
{ | ||
abort_unless($article->isPublished() || auth()->user()?->isAdmin(), 404); | ||
|
||
return view('article', [ | ||
'article' => $article, | ||
]); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.