From 648dacdd1ad433e564fd45149f2746f497421f58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro?= Date: Sun, 16 Jun 2024 19:32:23 +0200 Subject: [PATCH] load champion detail asynchronously --- .../champion_components/summoner_spells.ex | 2 +- .../live/champion_live/show.ex | 130 +++++++++++++++--- .../live/champion_live/show.html.heex | 22 +-- 3 files changed, 113 insertions(+), 41 deletions(-) diff --git a/apps/lol_analytics_web/lib/lol_analytics_web/components/champion_components/summoner_spells.ex b/apps/lol_analytics_web/lib/lol_analytics_web/components/champion_components/summoner_spells.ex index a68fc7b..db45a35 100644 --- a/apps/lol_analytics_web/lib/lol_analytics_web/components/champion_components/summoner_spells.ex +++ b/apps/lol_analytics_web/lib/lol_analytics_web/components/champion_components/summoner_spells.ex @@ -5,7 +5,7 @@ defmodule LolAnalyticsWeb.ChampionComponents.SummonerSpells do def summoner_spells(assigns) do ~H"""
- <%= for spell <- assigns.spells.summoner_spells do %> + <%= for spell <- assigns.spells do %>
diff --git a/apps/lol_analytics_web/lib/lol_analytics_web/live/champion_live/show.ex b/apps/lol_analytics_web/lib/lol_analytics_web/live/champion_live/show.ex index 2e66c53..87f7b59 100644 --- a/apps/lol_analytics_web/lib/lol_analytics_web/live/champion_live/show.ex +++ b/apps/lol_analytics_web/lib/lol_analytics_web/live/champion_live/show.ex @@ -4,6 +4,7 @@ defmodule LoLAnalyticsWeb.ChampionLive.Show do import LolAnalyticsWeb.ChampionComponents.SummonerSpells import LolAnalyticsWeb.ChampionComponents.ChampionAvatar import LolAnalyticsWeb.ChampionComponents.Items + import LolAnalyticsWeb.Loader alias LolAnalyticsWeb.ChampionComponents.SummonerSpells.ShowMapper @@ -18,34 +19,72 @@ defmodule LoLAnalyticsWeb.ChampionLive.Show do socket |> assign(:page_title, page_title(socket.assigns.live_action)) |> assign(:champion, load_champion_info(id)) - |> assign(:summoner_spells, %{ - summoner_spells: load_summoner_spells(id, team_position) - }) + |> load_summoner_spells(id, team_position) |> load_items(id, team_position, patch)} end - defp load_summoner_spells(champion_id, team_position) do - LolAnalytics.Facts.ChampionPickedSummonerSpell.Repo.get_champion_picked_summoners( - champion_id, - team_position + defp load_summoner_spells(socket, champion_id, team_position) do + socket + |> assign(:summoner_spells, %{status: :loading}) + |> start_async( + :get_summoners, + fn -> + LolAnalytics.Facts.ChampionPickedSummonerSpell.Repo.get_champion_picked_summoners( + champion_id, + team_position + ) + |> ShowMapper.map_spells() + end ) - |> ShowMapper.map_spells() end defp load_items(socket, champion_id, team_position, patch) do - items = - LolAnalytics.Facts.ChampionPickedItem.Repo.get_champion_picked_items( - champion_id, - team_position, - patch - ) - - all_items_mapped = items |> ShowMapper.map_items() |> Enum.take(30) - boots = items |> ShowMapper.extract_boots() - socket - |> assign(:items, all_items_mapped) - |> assign(:boots, boots) + |> assign(:items, %{status: :loading}) + |> assign(:boots, %{status: :loading}) + |> start_async( + :get_items, + fn -> + items = + LolAnalytics.Facts.ChampionPickedItem.Repo.get_champion_picked_items( + champion_id, + team_position, + patch + ) + + popular_items = items |> ShowMapper.map_items() |> Enum.take(30) + + boots = items |> ShowMapper.extract_boots() + + %{boots: boots, popular: popular_items} + end + ) + end + + def handle_async(:get_items, {:ok, %{popular: popular, boots: boots}} = result, socket) do + IO.inspect(result) + + socket = + socket + |> assign(:items, %{ + status: :data, + data: %{ + popular: popular, + boots: boots + } + }) + + {:noreply, socket} + end + + def handle_async(:get_summoners, {:ok, summoner_spells}, socket) do + socket = + assign(socket, :summoner_spells, %{ + status: :data, + data: summoner_spells + }) + + {:noreply, socket} end defp load_champion_info(champion_id) do @@ -55,4 +94,55 @@ defmodule LoLAnalyticsWeb.ChampionLive.Show do defp page_title(:show), do: "Show Champion" defp page_title(:edit), do: "Edit Champion" + + def render_summoner_spells(assigns) do + end + + def render_items(assigns) do + case assigns.items do + %{status: :loading} -> + ~H""" + <.loader /> + """ + + %{status: :data, data: data} -> + ~H""" + <%= if Enum.count(data.boots) > 0 do %> +

Items

+ +

Boots

+ +
+ + <.items items={data.boots} /> + +
+ <% end %> + +

Popular items

+ +
+ + <.items items={data.popular} /> + """ + end + end + + def render_summoners(assigns) do + case assigns.summoner_pells do + %{status: :loading} -> + ~H""" + <.loader /> + """ + + %{status: :data, data: data} -> + ~H""" +

Summoner spells

+ +
+ + <.summoner_spells spells={data} /> + """ + end + end end diff --git a/apps/lol_analytics_web/lib/lol_analytics_web/live/champion_live/show.html.heex b/apps/lol_analytics_web/lib/lol_analytics_web/live/champion_live/show.html.heex index 95b235f..9a345df 100644 --- a/apps/lol_analytics_web/lib/lol_analytics_web/live/champion_live/show.html.heex +++ b/apps/lol_analytics_web/lib/lol_analytics_web/live/champion_live/show.html.heex @@ -11,26 +11,8 @@
-

Summoner spells

- -
- -<.summoner_spells spells={@summoner_spells} /> +<.render_summoners summoner_pells={@summoner_spells} />
-

Items

- -

Boots

- -
- -<.items items={@boots} /> - -
- -

Popular items

- -
- -<.items items={@items} /> \ No newline at end of file +<.render_items items={@items} /> \ No newline at end of file