I am trying to pass the current url around for the purpose of something similar to “current active tab”.
I realize the best way (maybe there are better ways) to accomplish this is via live_session and on_mount function.
But, how do I get the URL within on_mount function? just like the handle_params did.
Code:
defmodule WoohooWeb.InitAssigns do
@moduledoc """
Ensures common `assigns` are applied to all LiveViews attaching this hook.
"""
import Phoenix.LiveView
alias Woohoo.Accounts
alias Woohoo.Accounts.User
def on_mount(:default, _params, session, socket) do
socket = assign_defaults(session, socket)
{:cont, socket}
end
def on_mount(:user, _params, session, socket) do
socket = assign_defaults(session, socket)
{:cont, socket}
end
def on_mount(:admin, _params, _session, socket) do
{:cont, socket}
end
defp assign_defaults(session, socket) do
socket = assign_new(socket, :current_user, fn -> find_current_user(session) end)
socket
end
defp find_current_user(session) do
with user_token when not is_nil(user_token) <- session["user_token"],
%User{} = user <- Accounts.get_user_by_session_token(user_token),
do: user
end
end
Wouldn’t that be nice? You simply can’t. the url is currently only available from within the handle_params/3 callback. So if you want to replicate some logic like “current active tab” across your views, you’ll have to replicate that callback everywhere. My current solution is to define the callback in the __using__/1 macro of a module and then have all views use that module.
Oh, and if you’re thinking of live_render a subview in a template and put the logic in there, that won’t work either because handle_params/3 only works for top-level views.