Skip to content
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

Schedule post feature does not work #20

Open
alokVishu opened this issue Jun 3, 2024 · 7 comments
Open

Schedule post feature does not work #20

alokVishu opened this issue Jun 3, 2024 · 7 comments

Comments

@alokVishu
Copy link

alokVishu commented Jun 3, 2024

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
Copy link

edeoliv commented Jun 3, 2024

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
Copy link
Author

alokVishu commented Jun 4, 2024

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
Copy link

edeoliv commented Jun 4, 2024

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
Copy link
Author

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
Copy link
Collaborator

sagautam5 commented Jun 4, 2024

@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
Copy link

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
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants