diff --git a/lib/music_library_web/controllers/stats_html.ex b/lib/music_library_web/controllers/stats_html.ex deleted file mode 100644 index dcb1ef01..00000000 --- a/lib/music_library_web/controllers/stats_html.ex +++ /dev/null @@ -1,7 +0,0 @@ -defmodule MusicLibraryWeb.StatsHTML do - use MusicLibraryWeb, :html - - alias MusicLibrary.Records.Record - - embed_templates "stats_html/*" -end diff --git a/lib/music_library_web/controllers/stats_controller.ex b/lib/music_library_web/live/stats_live/index.ex similarity index 51% rename from lib/music_library_web/controllers/stats_controller.ex rename to lib/music_library_web/live/stats_live/index.ex index 42f71c30..e52ccce2 100644 --- a/lib/music_library_web/controllers/stats_controller.ex +++ b/lib/music_library_web/live/stats_live/index.ex @@ -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 diff --git a/lib/music_library_web/controllers/stats_html/index.html.heex b/lib/music_library_web/live/stats_live/index.html.heex similarity index 100% rename from lib/music_library_web/controllers/stats_html/index.html.heex rename to lib/music_library_web/live/stats_live/index.html.heex diff --git a/lib/music_library_web/router.ex b/lib/music_library_web/router.ex index 184f0fc2..0e1c987a 100644 --- a/lib/music_library_web/router.ex +++ b/lib/music_library_web/router.ex @@ -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 diff --git a/priv/gettext/default.pot b/priv/gettext/default.pot index 39024ebf..4f74847d 100644 --- a/priv/gettext/default.pot +++ b/priv/gettext/default.pot @@ -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 "" diff --git a/test/music_library_web/controllers/stats_controller_test.exs b/test/music_library_web/live/stats_index_test.exs similarity index 65% rename from test/music_library_web/controllers/stats_controller_test.exs rename to test/music_library_web/live/stats_index_test.exs index fb15a44c..98d08ae5 100644 --- a/test/music_library_web/controllers/stats_controller_test.exs +++ b/test/music_library_web/live/stats_index_test.exs @@ -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