live_beats/lib/live_beats_web/live/nav.ex

38 lines
939 B
Elixir
Raw Normal View History

2021-10-29 16:12:23 +00:00
defmodule LiveBeatsWeb.Nav do
import Phoenix.LiveView
2021-11-12 15:10:04 +00:00
alias LiveBeats.MediaLibrary
2021-11-22 14:57:24 +00:00
alias LiveBeatsWeb.{ProfileLive, SettingsLive}
2021-11-12 15:10:04 +00:00
2021-11-22 15:31:30 +00:00
def on_mount(:default, _params, _session, socket) do
{:cont,
socket
|> assign(active_users: MediaLibrary.list_active_profiles(limit: 20))
|> attach_hook(:active_tab, :handle_params, &handle_active_tab_params/3)}
end
2021-11-24 03:06:48 +00:00
defp handle_active_tab_params(params, _url, socket) do
2021-11-19 19:50:36 +00:00
active_tab =
2021-11-22 15:31:30 +00:00
case {socket.view, socket.assigns.live_action} do
2021-11-24 03:06:48 +00:00
{ProfileLive, _} ->
2021-11-30 15:03:08 +00:00
if params["profile_username"] == current_user_profile_username(socket) do
2021-11-24 03:06:48 +00:00
:profile
end
{SettingsLive, _} ->
:settings
{_, _} ->
nil
2021-11-22 14:57:24 +00:00
end
2021-11-19 19:50:36 +00:00
{:cont, assign(socket, active_tab: active_tab)}
2021-10-29 16:12:23 +00:00
end
2021-11-24 03:06:48 +00:00
2021-11-30 15:03:08 +00:00
defp current_user_profile_username(socket) do
2021-11-24 03:06:48 +00:00
if user = socket.assigns.current_user do
user.username
end
end
2021-10-29 16:12:23 +00:00
end