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 defmodule MusicLibraryWeb.StatsLive.Index do
use MusicLibraryWeb, :controller use MusicLibraryWeb, :live_view
alias MusicLibrary.{Records, Wishlist} alias MusicLibrary.{Records, Wishlist}
alias Records.Record
def index(conn, _params) do def mount(_params, _session, socket) do
collection_count_by_format = collection_count_by_format =
Records.count_records_by_format() Records.count_records_by_format()
|> Enum.sort_by(fn {_format, count} -> count end, :desc) |> Enum.sort_by(fn {_format, count} -> count end, :desc)
@@ -19,15 +20,16 @@ defmodule MusicLibraryWeb.StatsController do
latest_record = Records.get_latest_record!() latest_record = Records.get_latest_record!()
conn {:ok,
|> assign(:page_title, gettext("Stats")) socket
|> render(:index, |> assign(
collection_count_by_format: collection_count_by_format, page_title: gettext("Stats"),
collection_count_by_type: collection_count_by_type, collection_count_by_format: collection_count_by_format,
collection_count: collection_count, collection_count_by_type: collection_count_by_type,
wishlist_count: wishlist_count, collection_count: collection_count,
latest_record: latest_record, wishlist_count: wishlist_count,
nav_section: :stats latest_record: latest_record,
) nav_section: :stats
)}
end end
end end
+2 -1
View File
@@ -29,7 +29,8 @@ defmodule MusicLibraryWeb.Router do
pipe_through :require_login pipe_through :require_login
get "/covers/:record_id", CoverController, :show get "/covers/:record_id", CoverController, :show
get "/", StatsController, :index
live "/", StatsLive.Index, :index
live "/records", RecordLive.Index, :index live "/records", RecordLive.Index, :index
live "/records/import", RecordLive.Index, :import live "/records/import", RecordLive.Index, :import
+7 -7
View File
@@ -84,7 +84,7 @@ msgid "Logout"
msgstr "" msgstr ""
#: lib/music_library_web/components/layouts/app.html.heex:7 #: 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 #, elixir-autogen, elixir-format
msgid "Stats" msgid "Stats"
msgstr "" msgstr ""
@@ -305,32 +305,32 @@ msgstr ""
msgid "Login" msgid "Login"
msgstr "" 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 #, elixir-autogen, elixir-format
msgid "Latest purchase" msgid "Latest purchase"
msgstr "" 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 #, elixir-autogen, elixir-format
msgid "Total collection" msgid "Total collection"
msgstr "" 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 #, elixir-autogen, elixir-format
msgid "Total wishlist" msgid "Total wishlist"
msgstr "" 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 #, elixir-autogen, elixir-format
msgid "Basics" msgid "Basics"
msgstr "" 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 #, elixir-autogen, elixir-format
msgid "Formats" msgid "Formats"
msgstr "" 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 #, elixir-autogen, elixir-format
msgid "Types" msgid "Types"
msgstr "" msgstr ""
@@ -1,9 +1,9 @@
defmodule MusicLibraryWeb.StatsControllerTest do defmodule MusicLibraryWeb.StatsIndexTest do
use MusicLibraryWeb.ConnCase use MusicLibraryWeb.ConnCase
alias MusicLibrary.Records.Record import Phoenix.LiveViewTest
import MusicLibrary.RecordsFixtures import MusicLibrary.RecordsFixtures
alias MusicLibrary.Records.Record
defp fill_collection(_) do defp fill_collection(_) do
records = Enum.map(1..99, fn _ -> record_fixture() end) records = Enum.map(1..99, fn _ -> record_fixture() end)
@@ -28,24 +28,22 @@ defmodule MusicLibraryWeb.StatsControllerTest do
conn: conn, conn: conn,
collection: collection collection: collection
} do } do
conn = get(conn, "/") {:ok, _stats_live, html} = live(conn, "/")
response = html_response(conn, 200) assert html =~ collection |> length() |> Integer.to_string()
assert response =~ collection |> length() |> Integer.to_string()
collection collection
|> Enum.frequencies_by(& &1.format) |> Enum.frequencies_by(& &1.format)
|> Enum.each(fn {format, count} -> |> Enum.each(fn {format, count} ->
assert response =~ "\n#{count}\n" assert html =~ "\n#{count}\n"
assert response =~ "\n#{Record.format_long_label(format)}\n" assert html =~ "\n#{Record.format_long_label(format)}\n"
end) end)
collection collection
|> Enum.frequencies_by(& &1.type) |> Enum.frequencies_by(& &1.type)
|> Enum.each(fn {type, count} -> |> Enum.each(fn {type, count} ->
assert response =~ "\n#{count}\n" assert html =~ "\n#{count}\n"
assert response =~ "\n#{Record.type_long_label(type)}\n" assert html =~ "\n#{Record.type_long_label(type)}\n"
end) end)
end end
@@ -54,21 +52,19 @@ defmodule MusicLibraryWeb.StatsControllerTest do
# highest purchsed_at value doesn't work, as it picks the wrong value. # highest purchsed_at value doesn't work, as it picks the wrong value.
latest_record = List.last(collection) 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 for artist <- latest_record.artists do
assert html_response(conn, 200) =~ escape(artist.name) assert html =~ escape(artist.name)
end end
end end
test "it shows the wishlist total count", %{conn: conn, wishlist: wishlist} do 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 html =~ wishlist |> length() |> Integer.to_string()
assert response =~ wishlist |> length() |> Integer.to_string()
end end
end end
end end