Files
chess-endgame-trainer/lib/chesstrainer_web/live/endgame_live/form.ex
T
owenrees 93806b9ab2 landing page (#63)
* remove daisyui and rebuild styles

* very basic setup of new design - missing components etc

* basic navbar with reusable landing components

* basic puzzle of the day

* update todos

* update todos

* add curated landing puzzle cards with selectable board (#55)

* update with footer

* add button to solve today's puzzle

* Make landing page a dead view

Render the home page from PageController with a static board preview, link puzzle cards and CTAs to public-code play routes, count puzzles from the DB, and add a global footer.

* Serve a marketing-only JS bundle on the landing page

Build a small marketing entry point with phoenix_html and the plain-DOM navbar, select it on controller-rendered pages, and keep the full LiveView bundle for LiveView pages.

* Add landing page SEO metadata

Replace the Phoenix default title with a descriptive Lucid Chess title, and add description, canonical, Open Graph, and Twitter card tags to the landing page.

* Complete the hero copy block

Add the spark icon primitive and the Endgames, made clear eyebrow. The primary CTA keeps linking to the active puzzle rather than scrolling to the board.

* Add a persisted global daily puzzle

Assign one endgame per UTC day in a daily_puzzles table with a unique date, preferring endgames unused in the last 30 days and falling back to any. Concurrent first requests insert with on-conflict-ignore and read the winning row. The landing board now shows the daily puzzle.

* remove unused CSS, nav links, time pill

* allow devices to connect to dev server by changing IP address to 0.0.0.0

* Restore the colour-mode switch and add a dark palette

* Use brand colour tokens on the play page

* self host fonts  - closes issue #62

* begin splitting CSS into modules

* remove hover on hero chessboard, split more css modules

* remove interactivity from hero chessboard

* update todo
2026-11-05 13:52:14 +01:00

263 lines
8.0 KiB
Elixir
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
defmodule ChesstrainerWeb.EndgameLive.Form do
use ChesstrainerWeb, :live_view
alias Chesstrainer.Repo
alias Chesstrainer.Themes
alias Chesstrainer.FEN
alias Chesstrainer.Endgames
alias Chesstrainer.Endgames.Endgame
@impl true
def render(assigns) do
~H"""
<Layouts.app flash={@flash}>
<.header>
{@page_title}
<: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 class="mt-2 w-full rounded-md border border-base-300 bg-base-100 p-1 shadow-sm">
<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>
<.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"
/>
<footer>
<.button phx-disable-with="Saving..." variant="primary">Save Endgame</.button>
<.button navigate={return_path(@return_to, @endgame)}>Cancel</.button>
</footer>
</.form>
</Layouts.app>
"""
end
@impl true
def mount(params, _session, socket) do
{:ok,
socket
|> assign(:return_to, return_to(params["return_to"]))
|> apply_action(socket.assigns.live_action, params)}
end
defp return_to("show"), do: "show"
defp return_to(_), do: "index"
defp apply_action(socket, :edit, %{"id" => id}) do
endgame = Endgames.get_endgame!(id) |> Repo.preload(:themes)
socket
|> assign(:page_title, "Edit Endgame")
|> assign(:endgame, endgame)
|> assign(:selected_themes, endgame.themes)
|> assign(:theme_query, "")
|> assign(:theme_results, [])
|> assign(:can_create, false)
|> assign(:form, to_form(Endgames.change_endgame(endgame)))
end
defp apply_action(socket, :new, _params) do
endgame = %Endgame{}
socket
|> assign(:page_title, "New Endgame")
|> assign(:endgame, endgame)
|> assign(:selected_themes, [])
|> assign(:theme_query, "")
|> assign(:theme_results, [])
|> assign(:can_create, false)
|> assign(:form, to_form(Endgames.change_endgame(endgame)))
end
@impl true
def handle_event("search_themes", %{"theme_search" => query}, socket) do
{:noreply, assign_search(socket, query)}
end
def handle_event("add_theme", %{"id" => id}, socket) do
theme = Themes.get_theme!(id)
{:noreply, socket |> add_selected_theme(theme) |> clear_search()}
end
def handle_event("create_theme", %{"name" => name}, socket) do
{:noreply, create_theme(socket, name)}
end
def handle_event("create_theme_from_search", %{"theme_search" => name}, socket) do
{:noreply, create_theme(socket, name)}
end
def handle_event("remove_theme", %{"id" => id}, socket) do
selected = Enum.reject(socket.assigns.selected_themes, &(Integer.to_string(&1.id) == id))
{:noreply, assign(socket, :selected_themes, selected)}
end
def handle_event("validate", %{"endgame" => endgame_params}, socket) do
fen = endgame_params["fen"] || socket.assigns.endgame.fen
color = FEN.color_from_fen(fen)
changeset =
socket.assigns.endgame
|> Endgames.change_endgame(endgame_params)
|> Ecto.Changeset.put_change(:color, color)
{:noreply, assign(socket, form: to_form(changeset, action: :validate))}
end
def handle_event("save", %{"endgame" => endgame_params}, socket) do
save_endgame(socket, socket.assigns.live_action, endgame_params)
end
defp save_endgame(socket, :edit, endgame_params) do
case Endgames.update_endgame(
socket.assigns.endgame,
endgame_params,
socket.assigns.selected_themes
) do
{:ok, endgame} ->
{:noreply,
socket
|> put_flash(:info, "Endgame updated successfully")
|> push_navigate(to: return_path(socket.assigns.return_to, endgame))}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, form: to_form(changeset))}
end
end
defp save_endgame(socket, :new, endgame_params) do
color = FEN.color_from_fen(endgame_params["fen"])
params = Map.put(endgame_params, "color", color)
case Endgames.create_endgame(params, socket.assigns.selected_themes) do
{:ok, endgame} ->
{:noreply,
socket
|> put_flash(:info, "Endgame created successfully")
|> push_navigate(to: return_path(socket.assigns.return_to, endgame))}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, form: to_form(changeset))}
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)
defp rating_display(%Phoenix.HTML.FormField{value: value}), do: value
defp return_path("index", _endgame), do: ~p"/admin/endgames"
defp return_path("show", endgame), do: ~p"/admin/endgames/#{endgame.id}"
end