thefireflytech / filament-blog

Blog plugin for laravel filament
https://packagist.org/packages/firefly/filament-blog
Other
91 stars 39 forks source link

Schedule post feature does not work #20

Open alokVishu opened 5 months ago

alokVishu commented 5 months ago

Hello,

I attempted to use the schedule post feature, but it doesn't seem to be working. I followed the installation steps and tried to schedule the post, but it's not working. Are there any special steps I need to follow? I can schedule the post but it's not get published when time meets.

Thank you for your time.

edeoliv commented 5 months ago

To resolve this issue, add the following code to your form in the Post model:

DateTimePicker::make('published_at')
    ->visible(function ($get) {
        return $get('status') === PostStatus::PUBLISHED->value;
    })
    ->required(function ($get) {
        return $get('status') === PostStatus::PUBLISHED->value;
    })
    ->native(false);

Toggle::make('is_published')
    ->label('Published')
    ->default(false)
    ->required(function ($get) {
        return $get('status') === PostStatus::PUBLISHED->value;
    });

The complete code should be as follows:

Fieldset::make('Status')
    ->schema([
        ToggleButtons::make('status')
            ->live()
            ->inline()
            ->options(PostStatus::class)
            ->required(),
        DateTimePicker::make('scheduled_for')
            ->visible(function ($get) {
                return $get('status') === PostStatus::SCHEDULED->value;
            })
            ->required(function ($get) {
                return $get('status') === PostStatus::SCHEDULED->value;
            })
            ->native(false),
        DateTimePicker::make('published_at')
            ->visible(function ($get) {
                return $get('status') === PostStatus::PUBLISHED->value;
            })
            ->required(function ($get) {
                return $get('status') === PostStatus::PUBLISHED->value;
            })
            ->native(false),
    ]);

Toggle::make('is_published')
    ->label('Published')
    ->default(false)
    ->required(function ($get) {
        return $get('status') === PostStatus::PUBLISHED->value;
    });
With this, your code should work perfectly.
alokVishu commented 5 months ago

Hello @edeoliv,

Thanks for the reply.

I've followed the installation steps for the blog plugin, but I haven't created a Post model in my project yet. The installation guide doesn't mention creating a Post model. Do I need to create a Post model and use your code to enable auto-publish scheduled blog? I'm looking forward to your response.

A detailed answer with an example would help me a lot.

https://filamentphp.com/plugins/firefly-blog#installation

Awaiting your reply.

Best Regards

edeoliv commented 5 months ago

You must add the code in the Post model of the library. I will open a PR with the fix to see if the author updates the package with the solution.

alokVishu commented 5 months ago

Hey @edeoliv,

Thank you for your prompt response.

I implemented your pull request (PR) in my local project and incorporated all the changes you made. While doing so, I encountered a missing import error:

use Filament\Forms\Components\Toggle;

I added the missing import, and the new field was successfully added along with a publish tab. However, I noticed that the auto-publishing(Schedule post) feature for posts on a future date is not working as expected. The new date field doesn't seem to be functioning, and the publish switch also appears to be non-operational. image

image

I have added an screen recoding for your reference: https://vimeo.com/953500495

Please me know if I am doing something wrong or there is some extra step to make it work. Best Regards

sagautam5 commented 5 months ago

@alokVishu In current implementation of post scheduling feature, we have used laravel queue. This plugin dispatches the schedule action to queue. You need to setup queue worker in your application.

Firefly\FilamentBlog\Resources\PostResource\Pages\CreatePost

protected function afterCreate()
{
    if ($this->record->isScheduled()) {

        $now = Carbon::now();
        $scheduledFor = Carbon::parse($this->record->scheduled_for);
        PostScheduleJob::dispatch($this->record)
            ->delay($now->diffInSeconds($scheduledFor));
    }
    if ($this->record->isStatusPublished()) {
        $this->record->published_at = date('Y-m-d H:i:s');
        $this->record->save();
        event(new BlogPublished($this->record));
    }
}

You can visit laravel official documentation to get more information about how queue works. https://laravel.com/docs/11.x/queues#main-content

This information about schedule feature should have been included in the documentation, we will add soon.

yagnikvamja commented 5 months ago

Hello @sagautam5,

I also would like to use the post scheduling feature but I'm having difficulty understanding how to do so. Can you provide me simple example of its usage because it would be very helpful for me.

~Best Regards

alokVishu commented 5 months ago

Hello @sagautam5, I haven't published all plugin-related files. When and where should I dispatch the job? A detailed answer would be helpful. We appreciate your help.