I have 2 live components on the same page, one for pagination and another for search. Both write their state into URL query params which are later handled by the live controller.
def handle_event("pagination", %{"page" => _} = params, socket) do
path = ITJWeb.Router.Helpers.live_path(socket, ITJWeb.OffersLive, params)
socket = push_patch(socket, to: path)
{:noreply, socket}
end
def handle_event("offer_search", %{"search" => search}, socket) do
path = ITJWeb.Router.Helpers.live_path(socket, ITJWeb.OffersLive, search)
socket = push_patch(socket, to: path)
{:noreply, socket}
end
def mount(params, _session, socket) do
{:noreply, socket} = handle_params(params, nil, socket)
{:ok, socket}
end
def handle_params(params, _uri, socket) do
...
{:noreply, socket}
end
The issue is that each component resets all existing query params. In other words, when you change the page, the search gets reset. There is a live demo: itj.orsinium.dev/offers.
The issue is that live_path
requires all query params to be explicitly passed as a third argument but the existing query params (if I understand correctly) are not passed into handle_event
.
- Is there a way to get current URL query params inside
handle_event
? - Is there a way to do
live_path
+push_patch
with preserving the current URL query params? - Should I approach the whole problem differently? Is
handle_event
of a live component a good place to modify URL or should I delegate it to the controller somehow?