defmodule LiveBeatsWeb.ProfileLive.SongEntryComponent do
use LiveBeatsWeb, :live_component
alias LiveBeats.MP3Stat
def send_progress(%Phoenix.LiveView.UploadEntry{} = entry) do
send_update(__MODULE__, id: entry.ref, progress: entry.progress)
end
def render(assigns) do
~H"""
<%= if @duration do %>
Title (<%= MP3Stat.to_mmss(@duration) %>)
<% else %>
Title
(calculating duration <.spinner class="inline-block animate-spin h-2.5 w-2.5 text-gray-400"/>)
<% end %>
Artist
<.error input_name={"songs[#{@ref}][title]"} field={:title} errors={@errors} class="-mt-1"/>
<.error input_name={"songs[#{@ref}][artist]"} field={:artist} errors={@errors} class="-mt-1"/>
License Attribution (as required by artist)
<.error input_name={"songs[#{@ref}][attribution]"} field={:attribution} errors={@errors} class="-mt-1"/>
"""
end
def update(%{progress: progress}, socket) do
{:ok, assign(socket, progress: progress)}
end
def update(%{changeset: changeset, id: id, index: index}, socket) do
{:ok,
socket
|> assign(ref: id)
|> assign(index: index)
|> assign(:errors, changeset.errors)
|> assign(title: Ecto.Changeset.get_field(changeset, :title))
|> assign(artist: Ecto.Changeset.get_field(changeset, :artist))
|> assign(duration: Ecto.Changeset.get_field(changeset, :duration))
|> assign(attribution: Ecto.Changeset.get_field(changeset, :attribution))
|> assign_new(:progress, fn -> 0 end)}
end
end