Hi, a good saturday afternoon to take a walk, here is South Korea.
I have a map %{sort_by: :stars, sort_dir: :desc}
, want to compose a path from it, ?sort_by=distance&sort_ord=asc
, then, reload the page using {:noreply, push_patch(socket, to: ~p"/products/?sort_by=distance&sort_ord=asc")}.
to show sorted list. How to do that?
Below is the code I am working.
PS
The latest LiveView 0.8 seems NOT reload page even though the content of socket.assigns has been changed in handle_params(). I guess it’s due to the new stream function.
In addition, Route.live_path/3
also seems NOT work in the latest LiveView even after changing use Phoenix.Router, helpers: true
and alias it to Route.
Thank you for reading.
defmodule MarketWeb.ProductLive.Index do
....
def mount(_params, _session, socket) do
{:ok, **stream**(socket, :products, Catalog.list_products())}
end
def handle_params(params, _url, socket) do
# params: %{sort_by: :stars, sort_dir: :desc}
socket = sort_products_using_params(socket)
** this is what I want to make work**
{:noreply, push_patch(socket, to: ~p"/products/?sort_by=distance&sort_ord=asc")}
end
... omitted
end
router.ex
scope "/", MarketWeb do
pipe_through([:browser, :require_authenticated_user])
live_session :require_authenticated_user,
on_mount: [{MarketWeb.UserAuth, :ensure_authenticated}] do
live("/users/settings", UserSettingsLive, :edit)
live("/users/settings/confirm_email/:token", UserSettingsLive, :confirm_email)
live("/guess", WrongLive)
live("/", ProductLive) # ???
live("/products", ProductLive.Index, :index)
live("/products/new", ProductLive.Index, :new)
live("/products/:id/edit", ProductLive.Index, :edit)
live("/products/:id", ProductLive.Show, :show)
live("/products/:id/show/edit", ProductLive.Show, :edit)
end
end