lepikhinb / momentum-modal

MIT License
442 stars 26 forks source link

Shared props issues #29

Open jaulz opened 1 year ago

jaulz commented 1 year ago

While using your great package (thanks!), I noticed that the approach sometimes does not generate the desired output. For example, my HandleInertiaRequests middleware returns the breadcrumbs for every request but when I render a modal the breadcrumbs of the background page are obviously not correct because the route of the modal is used. In general, I think as soon as the shared props are different for modal and background page it causes issues. Should the middleware hence run twice? Not sure how Laravel will behave then so it's more like an architectural question.

jaulz commented 1 year ago

Maybe it's also worth storing the baseRequest in the request itself so it can be retrieved from within the HandleInertiaRequests and the developer can decide which requests he needs.

jenky commented 1 year ago

I also getting problems with shared props today. For example, form.errors when using useForm is not working because the errors prop returns in page props instead of modal.props

epalmans commented 1 year ago

there might be a side-effect, but it may help to derive the baseRoute - or actually its URL - from the referer header. E.g. In my HandleInertiaRequests I'm using a macro on the Request class to then check the route; either being the one accessed directly, or the one in the "backdrop" used as baseRoute:

public function share(Request $request)
{
    return array_merge(parent::share($request),
        $request->isRouteOrReferer('admin.*') ? [
            // your data to pass in for this specific route-pattern
        ] : [],

    );
}

the macro:

use Illuminate\Http\Request;

Request::macro('isRouteOrReferer', function (String $routePattern) {
    if ($this->routeIs($routePattern)) {
        return true;
    }

    if ($refererUrl = $this->header('referer')) {
        $routeName = rescue(
            fn () => app('router')->getRoutes()->match(app('request')->create($refererUrl, 'GET'))->getName(),
            report: false
        );

        return Str::is($routePattern, $routeName);
    }

    return false;
});
dellow commented 1 year ago

I'm also having the same issue. My component error props are intercepted by the base route not the rendered modal. If I remove the baseRoute it works just fine.