wire-elements / spotlight

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

[question] use command dependencies for model creation #5

Closed felixsc closed 3 years ago

felixsc commented 3 years ago

Is it possible to use the input of a SpotlightCommandDependency to create a new model instead of searching for one?

What I'm trying to do I would like to start with a spotlight command that opens a text input like it does now. But instead of using the input of the dependency as a search query and returning models, I'd just take the string and create a new model.

Start pretty much as it is now:

protected string $name = 'Draft';
protected string $description = 'create a quick draft right in spotlight';

public function dependencies(): ?SpotlightCommandDependencies
    {
        return SpotlightCommandDependencies::collection()
            ->add(
                SpotlightCommandDependency::make('draft')
                // user should enter the content that will fill the model
                ->setPlaceholder('Enter your draft text.')
            );
    }

Then instead of returning a collection of SpotlightSearchResult there would be an action:

/**
 * 
 */
public function createDraft($content)
{
   return auth()->user()->drafts()->create(["content" => $content]);
}

As I understand it, right now it'll automatically search for a searchDrafts method and expect a collection of SpotlightSearchResult returned. For the above use case, it probably shouldn't call the createDraft method until the user submits what they typed into the input.

I understand if it's out of scope for this package & feel free to close the issue.

But I'm curious if it is maybe somehow possible already?

PhiloNL commented 3 years ago

It's not possible right now, but this use case has crossed my mind. I didn't end up needing this functionality at that moment but it can definitely be quite powerful. I'll take a look at this. Thanks for the feedback 😄

PhiloNL commented 3 years ago

So not sure if this fully covers your use case but you can now define the dependency type like this 😄

        return SpotlightCommandDependencies::collection()
            ->add(
                SpotlightCommandDependency::make('product')
                ->setPlaceholder('For which product do you want to create a license?')
            )
            ->add(
                SpotlightCommandDependency::make('name')
                    ->setPlaceholder('How do you want to name this license?')
                    ->setType(SpotlightCommandDependency::INPUT)
            );

It does not call a method at the moment after an "INPUT" dependency is submitted. But you can use the dependency in the execute method:

    public function execute(Spotlight $spotlight, Product $product, string $name)
    {
        // $spotlight->emit('openModal', 'license-create', ['product' => $product->id, 'name' => $name]);

       $draft = auth()->user()->drafts()->create(["name" => $name]);
       $sptlight->redirectRoute('drafts.edit', $draft);
    }
felixsc commented 3 years ago

It works perfectly. I can now do exactly what I wanted.

Thank you, Philo!