wire-elements / spotlight

Livewire component that brings Spotlight/Alfred-like functionality to your Laravel application.
MIT License
911 stars 71 forks source link

How I can add multiple (bulk) actions #43

Closed vlados closed 2 years ago

vlados commented 2 years ago

If I want to bulk add multiple actions to the search and to have one class how to do it? For example:

PhiloNL commented 2 years ago

You could do something like this:

class NavigateTo extends SpotlightCommand
{
    protected string $name = 'Navigate to';

    public function dependencies(): ?SpotlightCommandDependencies
    {
        return SpotlightCommandDependencies::collection()
            ->add(
                SpotlightCommandDependency::make('page')
                ->setPlaceholder('To which page do you want to navigate?')
            );
    }

    public function searchProduct(Request $request, $query)
    {
        return [
            new SpotlightSearchResult(
                    'products',
                    'Products',
                    'Navigate to products'
                ),
            new SpotlightSearchResult(
                    'users',
                    'Users',
                    'Navigate to users'
                ),
        ]
    }

    public function execute(Spotlight $spotlight, $page)
    {
        $spotlight->redirectRoute($page);
    }
}
vlados commented 2 years ago

Yes. But this will add a command Navigate to and then search for page. I want the initial spotlight to have all this pages as commands

PhiloNL commented 2 years ago

Not tested but this might work:

// AppServiceProvider
foreach(['products', 'users'] as $page) {
    LivewireUI\Spotlight\Spotlight::$commands[] = new RedirectCommand($page);
}
class RedirectCommand extends SpotlightCommand
{
    public function __construct($page)
    {
        $this->page = $page;
        $this->name = "View {$page}";
    }

    public function execute(Spotlight $spotlight)
    {
        $spotlight->redirectTo($this->page);
    }
}