Only render delete song button if owner

This commit is contained in:
Chris McCord 2021-11-12 06:21:12 -05:00
parent c45510cb6e
commit ed8ad74141
4 changed files with 18 additions and 21 deletions

View file

@ -16,7 +16,7 @@ defmodule LiveBeats.MediaLibrary do
defdelegate playing?(song), to: Song
defdelegate paused?(song), to: Song
def subscribe_to_profile(%Profile{} = profile, from \\ nil) do
def subscribe_to_profile(%Profile{} = profile) do
Phoenix.PubSub.subscribe(@pubsub, topic(profile.user_id))
end

View file

@ -361,7 +361,10 @@ defmodule LiveBeatsWeb.LiveHelpers do
end
def table(assigns) do
assigns = assign_new(assigns, :row_id, fn -> false end)
assigns =
assigns
|> assign_new(:row_id, fn -> false end)
|> assign(:col, for(col <- assigns.col, col[:if] != false, do: col))
~H"""
<div class="hidden mt-8 sm:block">
@ -401,6 +404,7 @@ defmodule LiveBeatsWeb.LiveHelpers do
assigns
|> assign_new(:row_id, fn -> false end)
|> assign_new(:active_id, fn -> nil end)
|> assign(:col, for(col <- assigns.col, col[:if] != false, do: col))
~H"""
<div class="hidden mt-8 sm:block">

View file

@ -138,9 +138,8 @@ defmodule LiveBeatsWeb.PlayerLive do
defp switch_profile(socket, profile_user_id) do
profile = get_profile(profile_user_id)
if connected?(socket) and profile do
if profile && connected?(socket) do
current_user = Accounts.update_active_profile(socket.assigns.current_user, profile.user_id)
send(self(), :play_current)
socket
@ -151,26 +150,21 @@ defmodule LiveBeatsWeb.PlayerLive do
end
end
defp assign_profile(socket, profile) do
if prev_profile = connected?(socket) && socket.assigns.profile do
MediaLibrary.unsubscribe_to_profile(prev_profile)
defp assign_profile(socket, profile)
when is_struct(profile, MediaLibrary.Profile) or is_nil(profile) do
%{profile: prev_profile, current_user: current_user} = socket.assigns
if connected?(socket) do
prev_profile && MediaLibrary.unsubscribe_to_profile(prev_profile)
profile && MediaLibrary.subscribe_to_profile(profile)
end
if profile, do: MediaLibrary.subscribe_to_profile(profile)
assign(socket,
profile: profile,
own_profile?: profile && MediaLibrary.owns_profile?(socket.assigns.current_user, profile)
own_profile?: !!profile && MediaLibrary.owns_profile?(current_user, profile)
)
end
defp assign_inactive_profile(socket, %Song{} = song) do
if socket.assigns.profile && MediaLibrary.owns_song?(socket.assigns.profile, song) do
socket
else
switch_profile(socket, song.user_id)
end
end
def handle_event("play_pause", _, socket) do
%{song: song, playing: playing, current_user: current_user} = socket.assigns
song = MediaLibrary.get_song!(song.id)
@ -245,7 +239,6 @@ defmodule LiveBeatsWeb.PlayerLive do
socket
|> push_play(song, elapsed)
|> assign(song: song, playing: true)
|> assign_inactive_profile(song)
end
defp stop_song(socket) do

View file

@ -32,7 +32,7 @@ defmodule LiveBeatsWeb.SongLive.Index do
</:actions>
</.title_bar>
<%= for song <- @songs, id = "delete-modal-#{song.id}" do %>
<%= for song <- if(@owns_profile?, do: @songs, else: []), id = "delete-modal-#{song.id}" do %>
<.modal
id={id}
on_confirm={JS.push("delete", value: %{id: song.id}) |> hide_modal(id) |> hide("#song-#{song.id}")}
@ -52,7 +52,7 @@ defmodule LiveBeatsWeb.SongLive.Index do
<:col let={%{song: song}} label="Artist"><%= song.artist %></:col>
<:col let={%{song: song}} label="Attribution" class="max-w-5xl break-words text-gray-600 font-light"><%= song.attribution %></:col>
<:col let={%{song: song}} label="Duration"><%= MP3Stat.to_mmss(song.duration) %></:col>
<:col let={%{song: song}} label="">
<:col let={%{song: song}} label="" if={@owns_profile?}>
<.link phx-click={show_modal("delete-modal-#{song.id}")} class="inline-flex items-center px-3 py-2 text-sm leading-4 font-medium">
<.icon name={:trash} class="-ml-0.5 mr-2 h-4 w-4"/>
Delete
@ -70,7 +70,7 @@ defmodule LiveBeatsWeb.SongLive.Index do
|> MediaLibrary.get_profile!()
if connected?(socket) do
MediaLibrary.subscribe_to_profile(profile, __MODULE__)
MediaLibrary.subscribe_to_profile(profile)
Accounts.subscribe(current_user.id)
end