Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed Jul 2, 2024
1 parent 55dd142 commit 90572f6
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 18 deletions.
16 changes: 2 additions & 14 deletions app/Actions/CreatePostFromLinkAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,17 @@ class CreatePostFromLinkAction
{
public function execute(Link $link): Carbon
{
$publishDate = $this->determinePublishDate($link);
$publishDate = Post::nextFreePublishDate();

Post::create([
'submitted_by_user_id' => $link->user_id,
'title' => $link->title,
'text' => $link->text,
'external_url' => $link->url,
'published' => false,
'publish_date' => $this->determinePublishDate($link),
'publish_date' => $publishDate,
]);

return $publishDate;
}

protected function determinePublishDate(Link $link): Carbon
{
$publishDate = now()->hour(14);

// If the date falls on a weekend or a post already exists on this date, increment the date
while ($publishDate->isWeekend() || Post::whereDate('publish_date', $publishDate)->exists()) {
$publishDate->addDay();
}

return $publishDate;
}
}
10 changes: 9 additions & 1 deletion app/Filament/Resources/PostResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public static function form(Form $form): Form
->required(),

Forms\Components\DateTimePicker::make('publish_date')
->withoutSeconds()
->nullable(),
SpatieTagsInput::make('tags'),
]),
Expand Down Expand Up @@ -71,6 +70,15 @@ public static function table(Table $table): Table
->actions([
Tables\Actions\Action::make('preview')
->url(fn (Post $record) => $record->adminPreviewUrl(), shouldOpenInNewTab: true),
Tables\Actions\Action::make('schedule')->action(function(Post $post) {
if ($post->publish_date) {
return;
}

$post->update([
'publish_date' => Post::nextFreePublishDate(),
]);
}),
Tables\Actions\EditAction::make(),
])
->bulkActions([
Expand Down
19 changes: 16 additions & 3 deletions app/Filament/Resources/PostResource/Pages/EditPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace App\Filament\Resources\PostResource\Pages;

use App\Filament\Resources\PostResource;
use Filament\Pages\Actions;
use App\Models\Post;
use Filament\Actions\Action;
use Filament\Actions\DeleteAction;
use Filament\Resources\Pages\EditRecord;

class EditPost extends EditRecord
Expand All @@ -13,8 +15,19 @@ class EditPost extends EditRecord
protected function getHeaderActions(): array
{
return [
Actions\Action::make('preview')->url($this->record->adminPreviewUrl(), shouldOpenInNewTab: true),
Actions\DeleteAction::make(),
Action::make('preview')->url($this->record->adminPreviewUrl(), shouldOpenInNewTab: true),
Action::make('schedule')->action(function(Post $post) {
if ($post->publish_date) {
return;
}

$post->update([
'publish_date' => Post::nextFreePublishDate(),
]);

$this->redirect(back()->getTargetUrl());
}),
DeleteAction::make(),
];
}
}
13 changes: 13 additions & 0 deletions app/Models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Models\Concerns\HasSlug;
use App\Models\Concerns\Sluggable;
use App\Models\Presenters\PostPresenter;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
Expand Down Expand Up @@ -358,4 +359,16 @@ public function shouldShow(): bool

return true;
}

public static function nextFreePublishDate(): Carbon
{
$publishDate = now()->hour(14);

// If the date falls on a weekend or a post already exists on this date, increment the date
while ($publishDate->isWeekend() || Post::whereDate('publish_date', $publishDate)->exists()) {
$publishDate->addDay();
}

return $publishDate;
}
}

0 comments on commit 90572f6

Please sign in to comment.