add small board to show fen string on endgame add

This commit is contained in:
2026-11-06 21:19:25 +01:00
parent c71f465487
commit f9340fb57b
4 changed files with 106 additions and 31 deletions
@@ -8,7 +8,15 @@ defmodule ChesstrainerWeb.Board.BoardComponent do
attr :game, :map, required: true
attr :myself, :any, required: true
attr :myself, :any, default: nil
attr :interactive, :boolean,
default: true,
doc: "emit square-click events (only useful inside a board LiveView)"
attr :class, :string,
default: "w-[388px] mt-6",
doc: "sizing and spacing for the board wrapper"
def board(assigns) do
files_list = [:a, :b, :c, :d, :e, :f, :g, :h]
@@ -32,7 +40,7 @@ defmodule ChesstrainerWeb.Board.BoardComponent do
)
~H"""
<div class="w-[388px] m-auto left-0 right-0 mt-6">
<div class={["m-auto left-0 right-0", @class]}>
<div class="border border-2 border-zinc-700 grid grid-rows-8 grid-cols-8">
<%= for rank <- @ranks do %>
<%= for file <- @files do %>
@@ -45,6 +53,7 @@ defmodule ChesstrainerWeb.Board.BoardComponent do
files_list={@files_list}
move_from_square={@game.move_from_square}
myself={@myself}
interactive={@interactive}
/>
<% end %>
<% end %>
@@ -10,7 +10,8 @@ defmodule ChesstrainerWeb.Board.SquareComponent do
attr :piece, :any, default: nil
attr :files_list, :list, required: true
attr :move_from_square, :any, default: nil
attr :myself, :any, required: true
attr :myself, :any, default: nil
attr :interactive, :boolean, default: true
def square(assigns) do
current_square = {assigns.file, assigns.rank}
@@ -22,20 +23,20 @@ defmodule ChesstrainerWeb.Board.SquareComponent do
~H"""
<div
class={[
"w-12 h-12 flex items-center justify-center",
"w-full aspect-square flex items-center justify-center",
background(
Enum.find_index(@files_list, fn f -> f == @file end),
@rank - 1
),
highlight_square(@highlight_square)
]}
phx-click="square-click"
phx-value-file={@file}
phx-value-rank={@rank}
phx-value-type="move"
phx-target={@myself}
phx-click={@interactive && "square-click"}
phx-value-file={@interactive && @file}
phx-value-rank={@interactive && @rank}
phx-value-type={@interactive && "move"}
phx-target={@interactive && @myself}
>
<.piece piece={@piece} class="w-10 h-10" />
<.piece piece={@piece} class="w-[83%] h-[83%]" />
</div>
"""
end
+55 -21
View File
@@ -7,6 +7,8 @@ defmodule ChesstrainerWeb.EndgameLive.Form do
alias Chesstrainer.Endgames
alias Chesstrainer.Endgames.Endgame
import ChesstrainerWeb.Board.BoardComponent
@impl true
def render(assigns) do
~H"""
@@ -73,26 +75,36 @@ defmodule ChesstrainerWeb.EndgameLive.Form do
</div>
<.form for={@form} id="endgame-form" phx-change="validate" phx-submit="save">
<.input field={@form[:fen]} type="text" label="Fen" disabled={@live_action == :edit} />
<.input field={@form[:color]} type="text" label="Color" disabled />
<.input
field={@form[:rating]}
type="number"
value={rating_display(@form[:rating])}
label="Rating"
/>
<.input field={@form[:message]} type="textarea" label="Message" />
<.input field={@form[:notes]} type="textarea" label="Notes" />
<.input
field={@form[:result]}
type="select"
options={[
{"Win", :win},
{"Draw", :draw},
{"Loss", :loss}
]}
label="Result"
/>
<div class="flex flex-col gap-6 sm:flex-row sm:items-start">
<div class="min-w-0 flex-1">
<.input field={@form[:fen]} type="text" label="Fen" disabled={@live_action == :edit} />
<.input field={@form[:color]} type="text" label="Color" disabled />
<.input
field={@form[:rating]}
type="number"
value={rating_display(@form[:rating])}
label="Rating"
/>
<.input field={@form[:message]} type="textarea" label="Message" />
<.input field={@form[:notes]} type="textarea" label="Notes" />
<.input
field={@form[:result]}
type="select"
options={[
{"Win", :win},
{"Draw", :draw},
{"Loss", :loss}
]}
label="Result"
/>
</div>
<div :if={@preview_game} id="endgame-preview-board" class="sm:w-64 sm:shrink-0">
<span class="mb-1 block text-sm font-medium text-base-content">Current position</span>
<.board game={@preview_game} class="w-full mt-2" interactive={false} />
</div>
</div>
<footer>
<.button phx-disable-with="Saving..." variant="primary">Save Endgame</.button>
<.button navigate={return_path(@return_to, @endgame)}>Cancel</.button>
@@ -119,6 +131,7 @@ defmodule ChesstrainerWeb.EndgameLive.Form do
socket
|> assign(:page_title, "Edit Endgame")
|> assign(:endgame, endgame)
|> assign(:preview_game, preview_game(endgame.fen))
|> assign(:selected_themes, endgame.themes)
|> assign(:theme_query, "")
|> assign(:theme_results, [])
@@ -132,6 +145,7 @@ defmodule ChesstrainerWeb.EndgameLive.Form do
socket
|> assign(:page_title, "New Endgame")
|> assign(:endgame, endgame)
|> assign(:preview_game, preview_game(endgame.fen))
|> assign(:selected_themes, [])
|> assign(:theme_query, "")
|> assign(:theme_results, [])
@@ -171,7 +185,11 @@ defmodule ChesstrainerWeb.EndgameLive.Form do
|> Endgames.change_endgame(endgame_params)
|> Ecto.Changeset.put_change(:color, color)
{:noreply, assign(socket, form: to_form(changeset, action: :validate))}
{:noreply,
assign(socket,
form: to_form(changeset, action: :validate),
preview_game: preview_game(fen)
)}
end
def handle_event("save", %{"endgame" => endgame_params}, socket) do
@@ -257,6 +275,22 @@ defmodule ChesstrainerWeb.EndgameLive.Form do
defp rating_display(%Phoenix.HTML.FormField{value: value}), do: value
# 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
rescue
MatchError -> nil
end
defp preview_game(_fen), do: nil
defp return_path("index", _endgame), do: ~p"/admin/endgames"
defp return_path("show", endgame), do: ~p"/admin/endgames/#{endgame.id}"
end
@@ -46,6 +46,37 @@ defmodule ChesstrainerWeb.EndgameLive.FormTest do
end
end
describe "position preview" do
test "shows the board once the FEN is valid", %{conn: conn} do
{:ok, view, _html} = live(conn, ~p"/admin/endgames/new")
refute has_element?(view, "#endgame-preview-board")
view
|> form("#endgame-form", endgame: %{fen: @fen})
|> render_change()
assert has_element?(view, "#endgame-preview-board")
assert has_element?(view, "#endgame-preview-board img[src='/images/pieces/white/rook.svg']")
end
test "hides the board when the FEN is invalid", %{conn: conn} do
{:ok, view, _html} = live(conn, ~p"/admin/endgames/new")
view
|> form("#endgame-form", endgame: %{fen: @fen})
|> render_change()
assert has_element?(view, "#endgame-preview-board")
view
|> form("#endgame-form", endgame: %{fen: "not a fen"})
|> render_change()
refute has_element?(view, "#endgame-preview-board")
end
end
test "creates an endgame with staged themes", %{conn: conn} do
theme = theme_fixture(%{name: "Lucena"})