From 5ca735766562f1591441d231dd5d0fb74f0fa692 Mon Sep 17 00:00:00 2001 From: Chris McCord Date: Fri, 12 Nov 2021 10:10:04 -0500 Subject: [PATCH] Show active users in sidebar --- lib/live_beats/media_library.ex | 13 ++ lib/live_beats_web/controllers/user_auth.ex | 4 +- lib/live_beats_web/live/live_helpers.ex | 11 +- lib/live_beats_web/live/nav.ex | 4 +- lib/live_beats_web/live/player_live.ex | 2 +- lib/live_beats_web/live/song_live/index.ex | 2 +- .../live/song_live/upload_form_component.ex | 2 +- .../templates/layout/live.html.heex | 178 +----------------- lib/live_beats_web/views/layout_view.ex | 112 +++++++++++ 9 files changed, 151 insertions(+), 177 deletions(-) diff --git a/lib/live_beats/media_library.ex b/lib/live_beats/media_library.ex index ab11e58..37e098a 100644 --- a/lib/live_beats/media_library.ex +++ b/lib/live_beats/media_library.ex @@ -192,6 +192,19 @@ defmodule LiveBeats.MediaLibrary do |> Repo.all() end + def list_active_profiles(opts) do + from(s in Song, + inner_join: u in LiveBeats.Accounts.User, + on: s.user_id == u.id, + where: s.status in [:playing], + limit: ^Keyword.fetch!(opts, :limit), + order_by: [desc: s.updated_at], + select: struct(u, [:id, :username, :profile_tagline]) + ) + |> Repo.all() + |> Enum.map(&get_profile!/1) + end + def get_current_active_song(%Profile{user_id: user_id}) do Repo.one(from s in Song, where: s.user_id == ^user_id and s.status in [:playing, :paused]) end diff --git a/lib/live_beats_web/controllers/user_auth.ex b/lib/live_beats_web/controllers/user_auth.ex index a4ccbd3..02b881d 100644 --- a/lib/live_beats_web/controllers/user_auth.ex +++ b/lib/live_beats_web/controllers/user_auth.ex @@ -135,5 +135,7 @@ defmodule LiveBeatsWeb.UserAuth do defp maybe_store_return_to(conn), do: conn - def signed_in_path(conn), do: Routes.song_index_path(conn, :index, conn.assigns.current_user.username) + def signed_in_path(conn) do + LiveBeatsWeb.LiveHelpers.profile_path(conn.assigns.current_user) + end end diff --git a/lib/live_beats_web/live/live_helpers.ex b/lib/live_beats_web/live/live_helpers.ex index dd77d96..a7e98f8 100644 --- a/lib/live_beats_web/live/live_helpers.ex +++ b/lib/live_beats_web/live/live_helpers.ex @@ -5,8 +5,15 @@ defmodule LiveBeatsWeb.LiveHelpers do alias LiveBeatsWeb.Router.Helpers, as: Routes alias Phoenix.LiveView.JS - def home_path(socket) do - Routes.song_index_path(socket, :index, socket.assigns.current_user.username) + alias LiveBeats.Accounts + alias LiveBeats.MediaLibrary + + def profile_path(%Accounts.User{} = current_user) do + Routes.song_index_path(LiveBeatsWeb.Endpoint, :index, current_user.username) + end + + def profile_path(%MediaLibrary.Profile{} = profile) do + Routes.song_index_path(LiveBeatsWeb.Endpoint, :index, profile.username) end def flash(%{kind: :error} = assigns) do diff --git a/lib/live_beats_web/live/nav.ex b/lib/live_beats_web/live/nav.ex index be6dec0..fb7a777 100644 --- a/lib/live_beats_web/live/nav.ex +++ b/lib/live_beats_web/live/nav.ex @@ -1,7 +1,9 @@ defmodule LiveBeatsWeb.Nav do import Phoenix.LiveView + alias LiveBeats.MediaLibrary + def on_mount(:default, _params, _session, socket) do - {:cont, assign(socket, genres: LiveBeats.MediaLibrary.list_genres())} + {:cont, assign(socket, :active_users, MediaLibrary.list_active_profiles(limit: 20))} end end diff --git a/lib/live_beats_web/live/player_live.ex b/lib/live_beats_web/live/player_live.ex index e63e972..a5d5291 100644 --- a/lib/live_beats_web/live/player_live.ex +++ b/lib/live_beats_web/live/player_live.ex @@ -38,7 +38,7 @@ defmodule LiveBeatsWeb.PlayerLive do
<%= if @profile do %> <.link - redirect_to={Routes.song_index_path(@socket, :index, @profile.username)} + redirect_to={profile_path(@profile)} class="mx-auto flex outline border-2 border-white border-opacity-20 rounded-md p-1 pr-2" > <.icon name={:user_circle}/> diff --git a/lib/live_beats_web/live/song_live/index.ex b/lib/live_beats_web/live/song_live/index.ex index 14c64d7..27d8d0c 100644 --- a/lib/live_beats_web/live/song_live/index.ex +++ b/lib/live_beats_web/live/song_live/index.ex @@ -178,7 +178,7 @@ defmodule LiveBeatsWeb.SongLive.Index do LayoutComponent.show_modal(UploadFormComponent, %{ id: :new, confirm: {"Save", type: "submit", form: "song-form"}, - patch_to: home_path(socket), + patch_to: profile_path(socket.assigns.current_user), song: socket.assigns.song, title: socket.assigns.page_title, current_user: socket.assigns.current_user diff --git a/lib/live_beats_web/live/song_live/upload_form_component.ex b/lib/live_beats_web/live/song_live/upload_form_component.ex index 4c93609..b14bfe9 100644 --- a/lib/live_beats_web/live/song_live/upload_form_component.ex +++ b/lib/live_beats_web/live/song_live/upload_form_component.ex @@ -54,7 +54,7 @@ defmodule LiveBeatsWeb.SongLive.UploadFormComponent do {:noreply, socket |> put_flash(:info, "#{map_size(songs)} song(s) uploaded") - |> push_redirect(to: home_path(socket))} + |> push_redirect(to: profile_path(current_user))} {:error, _reason} -> {:noreply, socket} diff --git a/lib/live_beats_web/templates/layout/live.html.heex b/lib/live_beats_web/templates/layout/live.html.heex index 8521b3e..d602b08 100644 --- a/lib/live_beats_web/templates/layout/live.html.heex +++ b/lib/live_beats_web/templates/layout/live.html.heex @@ -22,83 +22,11 @@ alt="Workflow">
@@ -118,49 +46,7 @@ diff --git a/lib/live_beats_web/views/layout_view.ex b/lib/live_beats_web/views/layout_view.ex index 512e481..66c8160 100644 --- a/lib/live_beats_web/views/layout_view.ex +++ b/lib/live_beats_web/views/layout_view.ex @@ -4,4 +4,116 @@ defmodule LiveBeatsWeb.LayoutView do # Phoenix LiveDashboard is available only in development by default, # so we instruct Elixir to not warn if the dashboard route is missing. @compile {:no_warn_undefined, {Routes, :live_dashboard_path, 2}} + + def sidebar_active_users(assigns) do + ~H""" +
+

+ Active Users +

+
+ <%= for user <- @users do %> + <.link redirect_to={profile_path(user)} + class="group flex items-center px-3 py-2 text-base leading-5 font-medium text-gray-600 rounded-md hover:text-gray-900 hover:bg-gray-50" + > + + + <%= user.username %> + + + <% end %> +
+
+ """ + end + + def sidebar_nav_links(assigns) do + ~H""" +
+ <%= if @current_user do %> + <.link + redirect_to={profile_path(@current_user)} + class="text-gray-700 hover:text-gray-900 hover:bg-gray-50 group flex items-center px-2 py-2 text-sm font-medium rounded-md" + > + <.icon name={:music_note} outlined class="text-gray-400 group-hover:text-gray-500 mr-3 flex-shrink-0 h-6 w-6"/> + My Songs + + + <.link + redirect_to={Routes.settings_path(LiveBeatsWeb.Endpoint, :edit)} + class="text-gray-700 hover:text-gray-900 hover:bg-gray-50 group flex items-center px-2 py-2 text-sm font-medium rounded-md" + > + <.icon name={:adjustments} outlined class="text-gray-400 group-hover:text-gray-500 mr-3 flex-shrink-0 h-6 w-6"/> + Settings + + <% else %> + <.link redirect_to={Routes.sign_in_path(LiveBeatsWeb.Endpoint, :index)} + class="text-gray-700 hover:text-gray-900 hover:bg-gray-50 group flex items-center px-2 py-2 text-sm font-medium rounded-md" + > + + Sign in + + <% end %> +
+ """ + end + + def sidebar_account_dropdown(assigns) do + ~H""" + +
+
+ +
+ +
+ """ + end end