diff --git a/lib/pinchflat_web/controllers/media_profiles/media_profile_controller.ex b/lib/pinchflat_web/controllers/media_profiles/media_profile_controller.ex index 34b7bf8..32a0a06 100644 --- a/lib/pinchflat_web/controllers/media_profiles/media_profile_controller.ex +++ b/lib/pinchflat_web/controllers/media_profiles/media_profile_controller.ex @@ -1,20 +1,31 @@ defmodule PinchflatWeb.MediaProfiles.MediaProfileController do use PinchflatWeb, :controller use Pinchflat.Sources.SourcesQuery + use Pinchflat.Profiles.ProfilesQuery alias Pinchflat.Repo alias Pinchflat.Profiles + alias Pinchflat.Sources.Source alias Pinchflat.Profiles.MediaProfile alias Pinchflat.Profiles.MediaProfileDeletionWorker def index(conn, _params) do - media_profiles = - MediaProfile - |> where([mp], is_nil(mp.marked_for_deletion_at)) - |> order_by(asc: :name) - |> Repo.all() + media_profiles_query = + from mp in MediaProfile, + as: :media_profile, + where: is_nil(mp.marked_for_deletion_at), + order_by: [asc: mp.name], + select: map(mp, ^MediaProfile.__schema__(:fields)), + select_merge: %{ + source_count: + subquery( + from s in Source, + where: s.media_profile_id == parent_as(:media_profile).id, + select: count(s.id) + ) + } - render(conn, :index, media_profiles: media_profiles) + render(conn, :index, media_profiles: Repo.all(media_profiles_query)) end def new(conn, _params) do diff --git a/lib/pinchflat_web/controllers/media_profiles/media_profile_html/index.html.heex b/lib/pinchflat_web/controllers/media_profiles/media_profile_html/index.html.heex index caee38d..9252ad4 100644 --- a/lib/pinchflat_web/controllers/media_profiles/media_profile_html/index.html.heex +++ b/lib/pinchflat_web/controllers/media_profiles/media_profile_html/index.html.heex @@ -14,7 +14,22 @@
- <%= live_render(@conn, PinchflatWeb.MediaProfiles.IndexTableLive) %> + <.table rows={@media_profiles} table_class="text-black dark:text-white"> + <:col :let={media_profile} label="Name"> + <.subtle_link href={~p"/media_profiles/#{media_profile.id}"}> + <%= media_profile.name %> + + + <:col :let={media_profile} label="Preferred Resolution"> + <%= media_profile.preferred_resolution %> + + <:col :let={media_profile} label="Sources"> + <.localized_number number={media_profile.source_count} /> + + <:col :let={media_profile} label="" class="flex justify-end"> + <.icon_link href={~p"/media_profiles/#{media_profile.id}/edit"} icon="hero-pencil-square" class="mr-4" /> + +
diff --git a/lib/pinchflat_web/controllers/media_profiles/media_profile_html/index_table_live.ex b/lib/pinchflat_web/controllers/media_profiles/media_profile_html/index_table_live.ex deleted file mode 100644 index 9309a5a..0000000 --- a/lib/pinchflat_web/controllers/media_profiles/media_profile_html/index_table_live.ex +++ /dev/null @@ -1,57 +0,0 @@ -defmodule PinchflatWeb.MediaProfiles.IndexTableLive do - use PinchflatWeb, :live_view - use Pinchflat.Profiles.ProfilesQuery - - alias Pinchflat.Repo - - def render(assigns) do - ~H""" - <.table rows={@media_profiles} table_class="text-black dark:text-white"> - <:col :let={media_profile} label="Name"> - <.subtle_link href={~p"/media_profiles/#{media_profile.id}"}> - <%= media_profile.name %> - - - <:col :let={media_profile} label="Preferred Resolution"> - <%= media_profile.preferred_resolution %> - - <:col :let={media_profile} label="Enabled?"> - <.input - name={"media_profile[#{media_profile.id}][enabled]"} - id={"media_profile_#{media_profile.id}_enabled"} - phx-hook="formless-input" - data-subscribe="change" - data-event-name="toggle_enabled" - data-identifier={media_profile.id} - type="toggle" - /> - - <:col :let={media_profile} label="" class="flex justify-end"> - <.icon_link href={~p"/media_profiles/#{media_profile.id}/edit"} icon="hero-pencil-square" class="mr-4" /> - - - """ - end - - def mount(_params, _session, socket) do - new_assigns = %{ - media_profiles: get_media_profiles() - } - - {:ok, assign(socket, new_assigns)} - end - - def handle_event("formless-input", %{"event" => "toggle_enabled"} = params, socket) do - # should_enable = params["value"] == "true" - IO.inspect(params) - - {:noreply, socket} - end - - defp get_media_profiles do - ProfilesQuery.new() - |> where([mp], is_nil(mp.marked_for_deletion_at)) - |> order_by(asc: :name) - |> Repo.all() - end -end