From c41d082ff80fdd761902ce122a5e10ad6834d76b Mon Sep 17 00:00:00 2001 From: mrdotb Date: Thu, 21 Mar 2024 11:22:13 +0100 Subject: [PATCH] Summoner form fix search bug Improve ux and error message --- .../admin_live/summoner_form_component.ex | 278 +++--------------- .../summoner_form_component.html.heex | 244 +++++++++++++++ 2 files changed, 279 insertions(+), 243 deletions(-) create mode 100644 lib/lor_web/live/admin_live/summoner_form_component.html.heex diff --git a/lib/lor_web/live/admin_live/summoner_form_component.ex b/lib/lor_web/live/admin_live/summoner_form_component.ex index 39ec106..41bfc7e 100644 --- a/lib/lor_web/live/admin_live/summoner_form_component.ex +++ b/lib/lor_web/live/admin_live/summoner_form_component.ex @@ -8,8 +8,9 @@ defmodule LorWeb.AdminLive.SummonerFormComponent do socket |> assign(assigns) |> assign(:account_request, AsyncResult.ok(nil)) - |> assign(:form, to_form(%{}, as: "search")) + |> assign(:form, to_form(%{})) |> assign(:game_name, "") + |> assign(:platform_form, to_form(%{})) |> assign(:platform_id, nil) |> assign(:platform_ids, Lor.Lol.PlatformIds.values()) |> assign(:search, "") @@ -22,16 +23,35 @@ defmodule LorWeb.AdminLive.SummonerFormComponent do end @impl true + def handle_event("change-platform", %{"platform_id" => platform_id}, socket) do + platform_id = get_platform_id(platform_id) + + socket = + socket + |> assign(:platform_id, platform_id) + |> assign(:platform_form, to_form(%{"platform_id" => platform_id})) + + {:noreply, socket} + end + + def handle_event("validate", %{"game_name" => game_name, "tag_line" => tag_line}, socket) do + socket = assign(socket, :form, to_form(%{"game_name" => game_name, "tag_line" => tag_line})) + {:noreply, socket} + end + + def handle_event("validate", %{"search" => search}, socket) do + socket = assign(socket, :form, to_form(%{"search" => search})) + {:noreply, socket} + end + def handle_event( "search", - %{"search" => %{"platform_id" => platform_id} = params}, + params, socket ) do - platform_id = get_platform_id(platform_id) - socket = socket - |> assign(:platform_id, platform_id) + |> assign(:form, to_form(params)) |> assign(:search, params["search"]) |> assign(:game_name, params["game_name"]) |> assign(:tag_line, params["tag_line"]) @@ -64,8 +84,8 @@ defmodule LorWeb.AdminLive.SummonerFormComponent do Lor.Lol.Summoner.create_from_api(platform_id, summoner_data, account_data, player_id) do put_flash!(socket, :success, "Summoner created and attached to the player successfully!") else - {:error, %{errors: [%{field: :account_id}]}} -> - put_flash!(socket, :error, "The summoner already exist attach it using local") + {:error, %{errors: [%{field: field}]}} when field in ~w(account_id riot_id)a -> + put_flash!(socket, :error, "The summoner already exist attach it using local tab") _error -> put_flash!(socket, :error, "Unknow error") @@ -85,8 +105,8 @@ defmodule LorWeb.AdminLive.SummonerFormComponent do Lor.Lol.Summoner.create_from_api(platform_id, summoner_data, account_data, player_id) do put_flash!(socket, :success, "Summoner created and attached to the player successfully!") else - {:error, %{errors: [%{field: :account_id}]}} -> - put_flash!(socket, :error, "The summoner already exist attach it using local") + {:error, %{errors: [%{field: field}]}} when field in ~w(account_id riot_id)a -> + put_flash!(socket, :error, "The summoner already exist attach it using local tab") _error -> put_flash!(socket, :error, "Unknow error") @@ -183,243 +203,15 @@ defmodule LorWeb.AdminLive.SummonerFormComponent do |> Lor.Lol.load!(:player) end - defp show_local(id) do - JS.hide(to: "#tabpanel-summoner-api-#{id}") - |> JS.hide(to: "#tabpanel-account-api-#{id}") - |> JS.show(to: "#tabpanel-local-#{id}") - |> JS.push("state", value: %{state: "local"}) + defp show_local(_id) do + JS.push("state", value: %{state: "local"}) end - defp show_summoner_api(id) do - JS.hide(to: "#tabpanel-local-#{id}") - |> JS.hide(to: "#tabpanel-account-api-#{id}") - |> JS.show(to: "#tabpanel-summoner-api#{id}") - |> JS.push("state", value: %{state: "summoner-api"}) + defp show_summoner_api(_id) do + JS.push("state", value: %{state: "summoner-api"}) end - defp show_account_api(id) do - JS.hide(to: "#tabpanel-local-#{id}") - |> JS.hide(to: "#tabpanel-summoner-api-#{id}") - |> JS.show(to: "#tabpanel-account-api#{id}") - |> JS.push("state", value: %{state: "account-api"}) - end - - @impl true - def render(assigns) do - ~H""" -
- - - <.icon name="hero-circle-stack" class="w-6 h-6 mr-2" /> - Local - - - <.icon name="hero-globe-alt" class="w-6 h-6 mr-2" /> - Riot Summoner Name - - - <.icon name="hero-globe-alt" class="w-6 h-6 mr-2" /> - Riot Account - - - - <.form for={@form} phx-change="search" phx-submit="search" phx-target={@myself}> - - - - -
- - - -
- - -
-
-
- <.icon name="hero-exclamation-circle" class="w-12 h-12" /> -

Could not find any Summoners locally.

-
-
- - 0}> - - Name - Riot ID - Current Player - Actions - - - - - <%= summoner.name %> - - - <%= summoner.riot_id %> - - - <%= if(summoner.player) do %> - <%= summoner.player.official_name %> - <% end %> - - - - Attach - - - - -
- -
- <.async_result :let={request} assign={@summoner_request}> - <:loading> -
- -
- - - <:failed :let={_reason}>Could not find summoner name on riot api. - - - - Name - Level - Last update - Actions - - - - - <%= request["name"] %> - - - <%= request["summonerLevel"] %> - - - <%= Lor.TimeHelpers.unix_timestamp_to_datetime(request["revisionDate"]) %> - - - - Create and Attach - - - - - -
- -
- <.async_result :let={request} assign={@account_request}> - <:loading> -
- -
- - - <:failed :let={_reason}>Could not find this account on riot api. - - - - Name - Tag line - Puuid - Actions - - - - <%= request["gameName"] %> - - - <%= request["tagLine"] %> - - - <%= request["puuid"] %> - - - - Create and Attach - - - - - -
-
- """ + defp show_account_api(_id) do + JS.push("state", value: %{state: "account-api"}) end end diff --git a/lib/lor_web/live/admin_live/summoner_form_component.html.heex b/lib/lor_web/live/admin_live/summoner_form_component.html.heex new file mode 100644 index 0000000..5b19143 --- /dev/null +++ b/lib/lor_web/live/admin_live/summoner_form_component.html.heex @@ -0,0 +1,244 @@ +
+ + + <.icon name="hero-circle-stack" class="w-6 h-6 mr-2" /> + Local + + + <.icon name="hero-globe-alt" class="w-6 h-6 mr-2" /> + Riot Summoner Name + + + <.icon name="hero-globe-alt" class="w-6 h-6 mr-2" /> + Riot Account + + + +
+
+ <.form for={@platform_form} phx-change="change-platform" phx-target={@myself}> + + + + <.form for={@form} phx-change="validate" phx-submit="search" phx-target={@myself}> + + +
+ + + +
+ + + +
+ +
+
+
+ <.icon name="hero-exclamation-circle" class="w-12 h-12" /> +

Could not find any Summoners locally.

+
+
+ + 0}> + + Name + Riot ID + Current Player + Actions + + + + + <%= summoner.name %> + + + <%= summoner.riot_id %> + + + <%= if(summoner.player) do %> + <%= summoner.player.official_name %> + <% end %> + + + + Attach + + + + +
+ +
+ <.async_result :let={request} assign={@summoner_request}> + <:loading> +
+ +
+ + + <:failed :let={_reason}> +
+ <.icon name="hero-exclamation-circle" class="w-12 h-12" /> +

Could not find summoner name on riot api.

+
+ + + + + Name + Level + Last update + Actions + + + + + <%= request["name"] %> + + + <%= request["summonerLevel"] %> + + + <%= Lor.TimeHelpers.unix_timestamp_to_datetime(request["revisionDate"]) %> + + + + Create and Attach + + + + + +
+ +
+ <.async_result :let={request} assign={@account_request}> + <:loading> +
+ +
+ + + <:failed :let={_reason}> +
+ <.icon name="hero-exclamation-circle" class="w-12 h-12" /> +

Could not find this account on riot api.

+
+ + + + + Name + Tag line + Puuid + Actions + + + + <%= request["gameName"] %> + + + <%= request["tagLine"] %> + + + <%= request["puuid"] %> + + + + Create and Attach + + + + + +
+
+