This repository was archived by the owner on May 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsongs_live.ex
More file actions
86 lines (74 loc) · 2.58 KB
/
songs_live.ex
File metadata and controls
86 lines (74 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
defmodule MusicboxWeb.SongsLive do
use Phoenix.LiveView
alias Musicbox.Player
require Logger
def render(assigns) do
~L"""
<h3 class="title is-title">Songs</h3>
<%= if Enum.empty?(@songs) do %>
<p>There are no songs yet</p>
<% else %>
<table class="table">
<thead>
<tr>
<th>Song</th>
<th>Duration</th>
<th>On a Playlist?</th>
<th>Playlists</th>
<th>Quick Add</th>
</tr>
</thead>
<%= for song <- @songs do %>
<tr>
<td><%= Musicbox.Song.description(song) %></td>
<td><%= Musicbox.Song.duration(song) %></td>
<td style="text-align: center;">
<%= if Musicbox.Song.on_a_playlist?(song) do %>
<span class="icon has-text-success">
<i class="mdi mdi-24px mdi-check-circle" aria-hidden="true"></i>
</span>
<% else %>
<span class="icon has-text-danger">
<i class="mdi mdi-24px mdi-close-circle" aria-hidden="true"></i>
</span>
<% end %>
</td>
<td><%= song.playlists %></td>
<td>
<form phx-change="quick-add">
<input name="song" id="song" type="hidden" value="<%= song.path %>" />
<select name="playlist" id="playlist">
<option value="" selected>-- Select a Playlist --</option>
<%= for playlist <- @playlists do %>
<option value="<%= playlist.id %>"><%= playlist.name %></option>
<% end %>
</select>
</form>
</td>
</tr>
<% end %>
</table>
<% end %>
<a href="/songs/new" class="button">Add Song</a>
"""
end
def mount(_session, socket) do
if connected?(socket), do: :timer.send_interval(10_000, self(), :tick)
Player.subscribe(self())
{:ok, put_status(socket)}
end
def handle_info(:tick, socket) do
{:noreply, put_status(socket)}
end
def handle_info({:paracusia, _}, socket) do
# When there's an event, just fetch the latest status
{:noreply, put_status(socket)}
end
def handle_event("quick-add", %{"playlist" => playlist, "song" => song}, socket) do
Player.add_to_playlist(playlist, song)
{:noreply, put_status(socket)}
end
defp put_status(socket) do
assign(socket, songs: Player.list_songs, playlists: Player.list_playlists)
end
end