mirror of
https://github.com/TheRealOwenRees/chess-endgame-trainer.git
synced 2026-11-18 11:42:07 +00:00
#11: endgames can carry multiple themes (endgame tags) chosen from a searchable, autocompleting picker with inline creation. Tags are staged in the form and saved atomically, so they work on both New and Edit. Tag names are trimmed and case-insensitively unique per category. Admin Show and Index display themes, and the seeds ship a starter vocabulary. The theme picker has its own form: LiveView requires phx-change inputs to live inside a form, and a separate theme form keeps Enter from submitting the endgame form.
71 lines
2.2 KiB
Elixir
71 lines
2.2 KiB
Elixir
defmodule ChesstrainerWeb.EndgameLive.Show do
|
|
use ChesstrainerWeb, :live_view
|
|
|
|
alias Chesstrainer.Endgames
|
|
alias Chesstrainer.Game
|
|
alias Chesstrainer.Repo
|
|
alias Chesstrainer.Tags
|
|
|
|
import ChesstrainerWeb.Board.BoardComponent
|
|
|
|
@impl true
|
|
def render(assigns) do
|
|
game =
|
|
case Game.game_from_fen(assigns.endgame.fen, :endgame) do
|
|
{:ok, g} -> g
|
|
{:error, :invalid_fen, g} -> g
|
|
end
|
|
|
|
# TODO once we return {:error, :invalid_fen, game}, then put flash to show it is invalid
|
|
# write tests for this
|
|
|
|
assigns =
|
|
assigns
|
|
|> assign(:game, game)
|
|
|> assign(:game_type, :endgame)
|
|
|
|
~H"""
|
|
<Layouts.app flash={@flash}>
|
|
<.header>
|
|
Endgame {@endgame.id}
|
|
<:subtitle>This is a endgame record from your database.</:subtitle>
|
|
<:actions>
|
|
<.button navigate={~p"/admin/endgames"}>
|
|
<.icon name="hero-arrow-left" />
|
|
</.button>
|
|
<.button navigate={~p"/endgames/#{@endgame}/play"}>
|
|
<.icon name="hero-play" /> Play (opens trainer)
|
|
</.button>
|
|
<.button variant="primary" navigate={~p"/admin/endgames/#{@endgame}/edit?return_to=show"}>
|
|
<.icon name="hero-pencil-square" /> Edit endgame
|
|
</.button>
|
|
</:actions>
|
|
</.header>
|
|
|
|
<.board game={@game} myself="board" />
|
|
|
|
<.list>
|
|
<:item title="Fen">{@endgame.fen}</:item>
|
|
<:item title="Key">{@endgame.key}</:item>
|
|
<:item title="Color">{@endgame.color}</:item>
|
|
<:item title="Rating">{@endgame.rating}</:item>
|
|
<:item title="Message">{@endgame.message}</:item>
|
|
<:item title="Notes">{@endgame.notes}</:item>
|
|
<:item title="Result">{@endgame.result}</:item>
|
|
<:item title="Themes">{Enum.join(Tags.names(@endgame.tags), ", ")}</:item>
|
|
<:item title="Attempted">{@endgame.times_attempted}</:item>
|
|
<:item title="Solved">{@endgame.times_solved}</:item>
|
|
</.list>
|
|
</Layouts.app>
|
|
"""
|
|
end
|
|
|
|
@impl true
|
|
def mount(%{"id" => id}, _session, socket) do
|
|
{:ok,
|
|
socket
|
|
|> assign(:page_title, "Show Endgame")
|
|
|> assign(:endgame, Endgames.get_endgame!(id) |> Repo.preload(:tags))}
|
|
end
|
|
end
|