Convert stats page to a live view

This commit is contained in:
Claudio Ortolina
2024-10-29 16:32:34 +00:00
parent 804727082c
commit 7d574c7fd9
6 changed files with 38 additions and 46 deletions
@@ -1,7 +0,0 @@
defmodule MusicLibraryWeb.StatsHTML do
use MusicLibraryWeb, :html
alias MusicLibrary.Records.Record
embed_templates "stats_html/*"
end
@@ -1,9 +1,10 @@
defmodule MusicLibraryWeb.StatsController do
use MusicLibraryWeb, :controller
defmodule MusicLibraryWeb.StatsLive.Index do
use MusicLibraryWeb, :live_view
alias MusicLibrary.{Records, Wishlist}
alias Records.Record
def index(conn, _params) do
def mount(_params, _session, socket) do
collection_count_by_format =
Records.count_records_by_format()
|> Enum.sort_by(fn {_format, count} -> count end, :desc)
@@ -19,15 +20,16 @@ defmodule MusicLibraryWeb.StatsController do
latest_record = Records.get_latest_record!()
conn
|> assign(:page_title, gettext("Stats"))
|> render(:index,
collection_count_by_format: collection_count_by_format,
collection_count_by_type: collection_count_by_type,
collection_count: collection_count,
wishlist_count: wishlist_count,
latest_record: latest_record,
nav_section: :stats
)
{:ok,
socket
|> assign(
page_title: gettext("Stats"),
collection_count_by_format: collection_count_by_format,
collection_count_by_type: collection_count_by_type,
collection_count: collection_count,
wishlist_count: wishlist_count,
latest_record: latest_record,
nav_section: :stats
)}
end
end
+2 -1
View File
@@ -29,7 +29,8 @@ defmodule MusicLibraryWeb.Router do
pipe_through :require_login
get "/covers/:record_id", CoverController, :show
get "/", StatsController, :index
live "/", StatsLive.Index, :index
live "/records", RecordLive.Index, :index
live "/records/import", RecordLive.Index, :import
+7 -7
View File
@@ -84,7 +84,7 @@ msgid "Logout"
msgstr ""
#: lib/music_library_web/components/layouts/app.html.heex:7
#: lib/music_library_web/controllers/stats_controller.ex:23
#: lib/music_library_web/live/stats_live/index.ex:26
#, elixir-autogen, elixir-format
msgid "Stats"
msgstr ""
@@ -305,32 +305,32 @@ msgstr ""
msgid "Login"
msgstr ""
#: lib/music_library_web/controllers/stats_html/index.html.heex:17
#: lib/music_library_web/live/stats_live/index.html.heex:17
#, elixir-autogen, elixir-format
msgid "Latest purchase"
msgstr ""
#: lib/music_library_web/controllers/stats_html/index.html.heex:38
#: lib/music_library_web/live/stats_live/index.html.heex:38
#, elixir-autogen, elixir-format
msgid "Total collection"
msgstr ""
#: lib/music_library_web/controllers/stats_html/index.html.heex:53
#: lib/music_library_web/live/stats_live/index.html.heex:53
#, elixir-autogen, elixir-format
msgid "Total wishlist"
msgstr ""
#: lib/music_library_web/controllers/stats_html/index.html.heex:3
#: lib/music_library_web/live/stats_live/index.html.heex:3
#, elixir-autogen, elixir-format
msgid "Basics"
msgstr ""
#: lib/music_library_web/controllers/stats_html/index.html.heex:70
#: lib/music_library_web/live/stats_live/index.html.heex:70
#, elixir-autogen, elixir-format
msgid "Formats"
msgstr ""
#: lib/music_library_web/controllers/stats_html/index.html.heex:91
#: lib/music_library_web/live/stats_live/index.html.heex:91
#, elixir-autogen, elixir-format
msgid "Types"
msgstr ""
@@ -1,9 +1,9 @@
defmodule MusicLibraryWeb.StatsControllerTest do
defmodule MusicLibraryWeb.StatsIndexTest do
use MusicLibraryWeb.ConnCase
alias MusicLibrary.Records.Record
import Phoenix.LiveViewTest
import MusicLibrary.RecordsFixtures
alias MusicLibrary.Records.Record
defp fill_collection(_) do
records = Enum.map(1..99, fn _ -> record_fixture() end)
@@ -28,24 +28,22 @@ defmodule MusicLibraryWeb.StatsControllerTest do
conn: conn,
collection: collection
} do
conn = get(conn, "/")
{:ok, _stats_live, html} = live(conn, "/")
response = html_response(conn, 200)
assert response =~ collection |> length() |> Integer.to_string()
assert html =~ collection |> length() |> Integer.to_string()
collection
|> Enum.frequencies_by(& &1.format)
|> Enum.each(fn {format, count} ->
assert response =~ "\n#{count}\n"
assert response =~ "\n#{Record.format_long_label(format)}\n"
assert html =~ "\n#{count}\n"
assert html =~ "\n#{Record.format_long_label(format)}\n"
end)
collection
|> Enum.frequencies_by(& &1.type)
|> Enum.each(fn {type, count} ->
assert response =~ "\n#{count}\n"
assert response =~ "\n#{Record.type_long_label(type)}\n"
assert html =~ "\n#{count}\n"
assert html =~ "\n#{Record.type_long_label(type)}\n"
end)
end
@@ -54,21 +52,19 @@ defmodule MusicLibraryWeb.StatsControllerTest do
# highest purchsed_at value doesn't work, as it picks the wrong value.
latest_record = List.last(collection)
conn = get(conn, "/")
{:ok, _stats_live, html} = live(conn, "/")
assert html_response(conn, 200) =~ escape(latest_record.title)
assert html =~ escape(latest_record.title)
for artist <- latest_record.artists do
assert html_response(conn, 200) =~ escape(artist.name)
assert html =~ escape(artist.name)
end
end
test "it shows the wishlist total count", %{conn: conn, wishlist: wishlist} do
conn = get(conn, "/")
{:ok, _stats_live, html} = live(conn, "/")
response = html_response(conn, 200)
assert response =~ wishlist |> length() |> Integer.to_string()
assert html =~ wishlist |> length() |> Integer.to_string()
end
end
end