I’m creating a search form with filters and want both the query and the filters to change the URL. The filters and query can be set separately or together, and handle_params
can already deal with either being empty.
What I’m doing is triggering a push_patch
on events (filter click, form submit, clear search). Something like this:
def handle_event("update_filter", %{"filter" => filter}, socket) do
{:noreply,
push_patch(socket,
to: Routes.live_path(socket, Index, query: socket.assigns.query, filter: filter)
)}
end
def handle_event("search", %{"query" => query}, socket) do
{:noreply,
push_patch(socket,
to: Routes.live_path(socket, Index, query: query, filter: socket.assigns.filter)
)}
end
This works, but my “problem” is that I have to set all the url params every time I call push_patch
. Is there any way to say append this param to the current path? Alternatively, is there any other design pattern I should be following to set this up?