mirror of
https://github.com/TheRealOwenRees/chess-endgame-trainer.git
synced 2026-11-18 11:42:07 +00:00
Add admin bulk FEN importer (#156)
* change position of theme picker * Add admin bulk FEN importer Paste FENs, resolve results from the local Syzygy tablebase only, preview each line's status, and bulk insert with themes and a batch rating.
This commit is contained in:
@@ -8,6 +8,7 @@ defmodule ChesstrainerWeb.EndgameLive.Form do
|
||||
alias Chesstrainer.Endgames.Endgame
|
||||
|
||||
import ChesstrainerWeb.Board.BoardComponent
|
||||
import ChesstrainerWeb.ThemePicker
|
||||
|
||||
@impl true
|
||||
def render(assigns) do
|
||||
@@ -20,61 +21,12 @@ defmodule ChesstrainerWeb.EndgameLive.Form do
|
||||
<:subtitle>Use this form to manage endgame records in your database.</:subtitle>
|
||||
</.header>
|
||||
|
||||
<div class="mb-6">
|
||||
<span class="mb-1 block text-sm font-medium text-base-content">Themes</span>
|
||||
|
||||
<div class="flex flex-wrap gap-2 mb-2">
|
||||
<.badge :for={theme <- @selected_themes} color="primary">
|
||||
{theme.name}
|
||||
<button
|
||||
type="button"
|
||||
phx-click="remove_theme"
|
||||
phx-value-id={theme.id}
|
||||
aria-label={"Remove #{theme.name}"}
|
||||
>
|
||||
×
|
||||
</button>
|
||||
</.badge>
|
||||
</div>
|
||||
|
||||
<form
|
||||
id="theme-form"
|
||||
phx-change="search_themes"
|
||||
phx-submit="create_theme_from_search"
|
||||
>
|
||||
<.input
|
||||
name="theme_search"
|
||||
value={@theme_query}
|
||||
phx-debounce="200"
|
||||
autocomplete="off"
|
||||
placeholder="Search or create a theme…"
|
||||
/>
|
||||
|
||||
<ul>
|
||||
<li :for={theme <- @theme_results}>
|
||||
<button
|
||||
type="button"
|
||||
phx-click="add_theme"
|
||||
phx-value-id={theme.id}
|
||||
class="block w-full rounded px-3 py-2 text-left text-sm hover:bg-base-200"
|
||||
>
|
||||
{theme.name}
|
||||
</button>
|
||||
</li>
|
||||
|
||||
<li :if={@can_create}>
|
||||
<button
|
||||
type="button"
|
||||
phx-click="create_theme"
|
||||
phx-value-name={@theme_query}
|
||||
class="block w-full rounded px-3 py-2 text-left text-sm hover:bg-base-200"
|
||||
>
|
||||
Create theme "{String.trim(@theme_query)}"
|
||||
</button>
|
||||
</li>
|
||||
</ul>
|
||||
</form>
|
||||
</div>
|
||||
<.theme_picker
|
||||
selected_themes={@selected_themes}
|
||||
theme_query={@theme_query}
|
||||
theme_results={@theme_results}
|
||||
can_create={@can_create}
|
||||
/>
|
||||
|
||||
<.form for={@form} id="endgame-form" phx-change="validate" phx-submit="save">
|
||||
<div class="flex flex-col gap-6 sm:flex-row sm:items-start">
|
||||
@@ -87,6 +39,7 @@ defmodule ChesstrainerWeb.EndgameLive.Form do
|
||||
value={rating_display(@form[:rating])}
|
||||
label="Rating"
|
||||
/>
|
||||
|
||||
<.input field={@form[:message]} type="textarea" label="Message" />
|
||||
<.input field={@form[:notes]} type="textarea" label="Notes" />
|
||||
<.input
|
||||
@@ -231,46 +184,6 @@ defmodule ChesstrainerWeb.EndgameLive.Form do
|
||||
end
|
||||
end
|
||||
|
||||
defp assign_search(socket, query) do
|
||||
trimmed = String.trim(query)
|
||||
results = Themes.search(query, "endgame")
|
||||
|
||||
can_create =
|
||||
trimmed != "" and
|
||||
not Enum.any?(results, &(String.downcase(&1.name) == String.downcase(trimmed)))
|
||||
|
||||
socket
|
||||
|> assign(:theme_query, query)
|
||||
|> assign(:theme_results, results)
|
||||
|> assign(:can_create, can_create)
|
||||
end
|
||||
|
||||
defp create_theme(socket, name) do
|
||||
if String.trim(name) == "" do
|
||||
socket
|
||||
else
|
||||
case Themes.get_or_create(name, "endgame") do
|
||||
{:ok, theme} -> socket |> add_selected_theme(theme) |> clear_search()
|
||||
{:error, _reason} -> put_flash(socket, :error, "Could not create that theme.")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp clear_search(socket) do
|
||||
socket
|
||||
|> assign(:theme_query, "")
|
||||
|> assign(:theme_results, [])
|
||||
|> assign(:can_create, false)
|
||||
end
|
||||
|
||||
defp add_selected_theme(socket, theme) do
|
||||
if Enum.any?(socket.assigns.selected_themes, &(&1.id == theme.id)) do
|
||||
socket
|
||||
else
|
||||
assign(socket, :selected_themes, socket.assigns.selected_themes ++ [theme])
|
||||
end
|
||||
end
|
||||
|
||||
# Ratings are stored as floats (Glicko); the admin form presents them as whole numbers.
|
||||
defp rating_display(%Phoenix.HTML.FormField{value: value}) when is_float(value),
|
||||
do: round(value)
|
||||
@@ -280,13 +193,8 @@ defmodule ChesstrainerWeb.EndgameLive.Form do
|
||||
# Shows the position as the admin types a FEN; hidden while the FEN is invalid or blank.
|
||||
# Parses directly rather than through Game.game_from_fen/2 so keystrokes never probe a tablebase.
|
||||
defp preview_game(fen) when is_binary(fen) do
|
||||
case Chex.Parser.FEN.parse(fen) do
|
||||
{:ok, game} ->
|
||||
%{board: game.board, orientation: game.active_color, move_from_square: nil}
|
||||
|
||||
_ ->
|
||||
nil
|
||||
end
|
||||
{:ok, game} = Chex.Parser.FEN.parse(fen)
|
||||
%{board: game.board, orientation: game.active_color, move_from_square: nil}
|
||||
rescue
|
||||
MatchError -> nil
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user