orchidsoftware / platform

Orchid is a @laravel package that allows for rapid application development of back-office applications, admin/user panels, and dashboards.
https://orchid.software
MIT License
4.26k stars 631 forks source link

Using Async Parameters in Modal rendering #2710

Open r4bick opened 10 months ago

r4bick commented 10 months ago

Is your feature request related to a problem? Please describe. There is no possibility to get parameters that getting by async request in during specify modal layouts.

Describe the solution you'd like To have a method to get access to these params for more flexibility. Now we can get only access to result of query method.

drabodan commented 10 months ago

Perhaps you're talking about this - https://orchid.software/en/docs/modals/#asynchronous-data-loading

In general everything is similar to the usual work with data in a Screen, only for the Screen the data is prepared in query, and for the modal in the method indicated in ->async and then this data can be accessed in modal layouts..

r4bick commented 9 months ago

How could i decide the following problem:

In UserEditLayout i need to get access to User model that i get in asyncGetUser method to render layout depends on its state?

class UserListScreen extends Screen
{
    public function query(): iterable
    {
        return [
            'users' => User::with('roles')
                ->paginate(),
        ];
    }

    public function layout(): iterable
    {
        return [
            UserFiltersLayout::class,
            UserListLayout::class,

            Layout::modal('asyncEditUserModal', UserEditLayout::class)
                ->async('asyncGetUser'),
        ];
    }

    public function asyncGetUser(User $user): iterable
    {
        return [
            'user' => $user,
        ];
    }
}
class UserEditLayout extends Rows
{
    public function fields(): array
    {
        // need to access to User model
        return [

        ];
    }
}
mrneatly commented 2 months ago

I have the same question. The documentation shows how to pass a variable to a modal, but there is no clear example how to refer to the passed variable from within the modal layout.

SimonErich commented 1 month ago

Just had the same issue and the easiest solution is to just use request('my-parameter-key') inside of the Modal component class. Because the async request is a normal route call, this works perfectly fine. :)

hope this helps.

mrneatly commented 1 month ago

@SimonErich you saved my day! That wasn't the most obvious solution but it worked well. @tabuna would you mind reflecting this case in the documentation?