mirror of
https://github.com/TheRealOwenRees/chess-endgame-trainer.git
synced 2026-11-18 03:32:06 +00:00
Add public codes for player URLs and admin endgame search
Player-facing endgame links now use a 10-character Crockford base32 public code instead of the database UUID; admin routes keep the UUID. Existing rows are backfilled by migration, and the admin Show page displays and copies the code. The admin endgame index gains its own search box: exact database id, exact public code, or a FEN / FEN fragment. A single match opens the endgame; multiple matches list below; nothing found says so. Closes #44 Closes #45
This commit is contained in:
@@ -256,5 +256,5 @@ defmodule ChesstrainerWeb.EndgameLive.Form do
|
||||
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}"
|
||||
defp return_path("show", endgame), do: ~p"/admin/endgames/#{endgame.id}"
|
||||
end
|
||||
|
||||
@@ -18,7 +18,63 @@ defmodule ChesstrainerWeb.EndgameLive.Index do
|
||||
</:actions>
|
||||
</.header>
|
||||
|
||||
<.form
|
||||
for={@search_form}
|
||||
id="endgame-search-form"
|
||||
phx-submit="endgame_search"
|
||||
class="mb-6 flex items-end gap-2"
|
||||
>
|
||||
<div class="flex-1">
|
||||
<.input
|
||||
field={@search_form[:query]}
|
||||
type="text"
|
||||
label="Find by ID or FEN"
|
||||
autocomplete="off"
|
||||
placeholder="Public code, database id, or FEN…"
|
||||
class="input input-bordered w-full"
|
||||
/>
|
||||
</div>
|
||||
<.button variant="primary">Find</.button>
|
||||
</.form>
|
||||
|
||||
<div :if={@search_query} id="endgame-search-results" class="space-y-3">
|
||||
<div class="flex items-center justify-between">
|
||||
<p class="text-sm text-base-content/70">Results for “{@search_query}”</p>
|
||||
<button type="button" id="clear-endgame-search" phx-click="clear_search" class="link">
|
||||
Clear
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<p :if={@search_results == []} class="text-base-content/70">
|
||||
No endgame matches “{@search_query}”.
|
||||
</p>
|
||||
|
||||
<table :if={@search_results != []} class="table table-zebra">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Key</th>
|
||||
<th>Color</th>
|
||||
<th>Result</th>
|
||||
<th>Rating</th>
|
||||
<th><span class="sr-only">Actions</span></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr :for={endgame <- @search_results} id={"search-result-" <> endgame.id}>
|
||||
<td>{endgame.key}</td>
|
||||
<td>{endgame.color}</td>
|
||||
<td>{endgame.result}</td>
|
||||
<td>{round(endgame.rating)}</td>
|
||||
<td class="w-0 font-semibold">
|
||||
<.link navigate={~p"/admin/endgames/#{endgame.id}"}>Show</.link>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<.filter_bar
|
||||
:if={!@search_query}
|
||||
selected_themes={@selected_themes}
|
||||
theme_query={@theme_query}
|
||||
theme_results={@theme_results}
|
||||
@@ -26,7 +82,7 @@ defmodule ChesstrainerWeb.EndgameLive.Index do
|
||||
selected_key={@selected_key}
|
||||
/>
|
||||
|
||||
<.grouped_list :if={@groups != []} groups={@groups}>
|
||||
<.grouped_list :if={!@search_query and @groups != []} groups={@groups}>
|
||||
<:header>
|
||||
<tr>
|
||||
<th>Key</th>
|
||||
@@ -40,13 +96,22 @@ defmodule ChesstrainerWeb.EndgameLive.Index do
|
||||
|
||||
<:row :let={endgame}>
|
||||
<tr id={"endgame-" <> endgame.id}>
|
||||
<td phx-click={JS.navigate(~p"/admin/endgames/#{endgame}")} class="hover:cursor-pointer">
|
||||
<td
|
||||
phx-click={JS.navigate(~p"/admin/endgames/#{endgame.id}")}
|
||||
class="hover:cursor-pointer"
|
||||
>
|
||||
{endgame.key}
|
||||
</td>
|
||||
<td phx-click={JS.navigate(~p"/admin/endgames/#{endgame}")} class="hover:cursor-pointer">
|
||||
<td
|
||||
phx-click={JS.navigate(~p"/admin/endgames/#{endgame.id}")}
|
||||
class="hover:cursor-pointer"
|
||||
>
|
||||
{endgame.color}
|
||||
</td>
|
||||
<td phx-click={JS.navigate(~p"/admin/endgames/#{endgame}")} class="hover:cursor-pointer">
|
||||
<td
|
||||
phx-click={JS.navigate(~p"/admin/endgames/#{endgame.id}")}
|
||||
class="hover:cursor-pointer"
|
||||
>
|
||||
{endgame.result}
|
||||
</td>
|
||||
<td>
|
||||
@@ -57,14 +122,17 @@ defmodule ChesstrainerWeb.EndgameLive.Index do
|
||||
</span>
|
||||
<span :if={endgame.themes == []} class="text-base-content/50">None</span>
|
||||
</td>
|
||||
<td phx-click={JS.navigate(~p"/admin/endgames/#{endgame}")} class="hover:cursor-pointer">
|
||||
<td
|
||||
phx-click={JS.navigate(~p"/admin/endgames/#{endgame.id}")}
|
||||
class="hover:cursor-pointer"
|
||||
>
|
||||
{round(endgame.rating)}
|
||||
</td>
|
||||
<td class="w-0 font-semibold">
|
||||
<div class="flex gap-4">
|
||||
<.link navigate={~p"/admin/endgames/#{endgame}"}>Show</.link>
|
||||
<.link navigate={~p"/endgames/#{endgame}/play"}>Play</.link>
|
||||
<.link navigate={~p"/admin/endgames/#{endgame}/edit"}>Edit</.link>
|
||||
<.link navigate={~p"/admin/endgames/#{endgame.id}"}>Show</.link>
|
||||
<.link navigate={~p"/endgames/#{endgame.public_code}/play"}>Play</.link>
|
||||
<.link navigate={~p"/admin/endgames/#{endgame.id}/edit"}>Edit</.link>
|
||||
<.link
|
||||
phx-click={JS.push("delete", value: %{id: endgame.id})}
|
||||
data-confirm="Are you sure?"
|
||||
@@ -77,7 +145,7 @@ defmodule ChesstrainerWeb.EndgameLive.Index do
|
||||
</:row>
|
||||
</.grouped_list>
|
||||
|
||||
<p :if={@groups == []} class="text-base-content/70">
|
||||
<p :if={!@search_query and @groups == []} class="text-base-content/70">
|
||||
No endgames match these filters.
|
||||
</p>
|
||||
</Layouts.app>
|
||||
@@ -89,10 +157,37 @@ defmodule ChesstrainerWeb.EndgameLive.Index do
|
||||
{:ok,
|
||||
socket
|
||||
|> assign(:page_title, "Listing Endgames")
|
||||
|> assign(:search_form, to_form(%{"query" => ""}))
|
||||
|> assign(:search_query, nil)
|
||||
|> assign(:search_results, [])
|
||||
|> init_state()}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event("endgame_search", %{"query" => query}, socket) do
|
||||
query = String.trim(query)
|
||||
|
||||
case {query, Endgames.search(query)} do
|
||||
{"", _results} ->
|
||||
{:noreply, clear_search(socket)}
|
||||
|
||||
{_query, [endgame]} ->
|
||||
{:noreply, push_navigate(socket, to: ~p"/admin/endgames/#{endgame.id}")}
|
||||
|
||||
{query, results} ->
|
||||
{:noreply,
|
||||
assign(socket,
|
||||
search_form: to_form(%{"query" => query}),
|
||||
search_query: query,
|
||||
search_results: results
|
||||
)}
|
||||
end
|
||||
end
|
||||
|
||||
def handle_event("clear_search", _params, socket) do
|
||||
{:noreply, clear_search(socket)}
|
||||
end
|
||||
|
||||
def handle_event("delete", %{"id" => id}, socket) do
|
||||
endgame = Endgames.get_endgame!(id)
|
||||
{:ok, _} = Endgames.delete_endgame(endgame)
|
||||
@@ -103,4 +198,12 @@ defmodule ChesstrainerWeb.EndgameLive.Index do
|
||||
def handle_event(event, params, socket) do
|
||||
handle_filter_event(event, params, socket)
|
||||
end
|
||||
|
||||
defp clear_search(socket) do
|
||||
assign(socket,
|
||||
search_form: to_form(%{"query" => ""}),
|
||||
search_query: nil,
|
||||
search_results: []
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -178,8 +178,8 @@ defmodule ChesstrainerWeb.EndgameLive.Play do
|
||||
end
|
||||
|
||||
@impl true
|
||||
def mount(%{"id" => id}, _session, socket) do
|
||||
endgame = Endgames.get_endgame!(id)
|
||||
def mount(%{"public_code" => public_code}, _session, socket) do
|
||||
endgame = Endgames.get_endgame_by_public_code!(public_code)
|
||||
|
||||
socket =
|
||||
socket
|
||||
|
||||
@@ -32,14 +32,23 @@ defmodule ChesstrainerWeb.EndgameLive.PlayerIndex do
|
||||
</:header>
|
||||
|
||||
<:row :let={endgame}>
|
||||
<tr id={"endgame-" <> endgame.id}>
|
||||
<td phx-click={JS.navigate(~p"/endgames/#{endgame}/play")} class="hover:cursor-pointer">
|
||||
<tr id={"endgame-" <> endgame.public_code}>
|
||||
<td
|
||||
phx-click={JS.navigate(~p"/endgames/#{endgame.public_code}/play")}
|
||||
class="hover:cursor-pointer"
|
||||
>
|
||||
{endgame.color}
|
||||
</td>
|
||||
<td phx-click={JS.navigate(~p"/endgames/#{endgame}/play")} class="hover:cursor-pointer">
|
||||
<td
|
||||
phx-click={JS.navigate(~p"/endgames/#{endgame.public_code}/play")}
|
||||
class="hover:cursor-pointer"
|
||||
>
|
||||
{endgame.result}
|
||||
</td>
|
||||
<td phx-click={JS.navigate(~p"/endgames/#{endgame}/play")} class="hover:cursor-pointer">
|
||||
<td
|
||||
phx-click={JS.navigate(~p"/endgames/#{endgame.public_code}/play")}
|
||||
class="hover:cursor-pointer"
|
||||
>
|
||||
{round(endgame.rating)}
|
||||
</td>
|
||||
<td>
|
||||
@@ -51,7 +60,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayerIndex do
|
||||
<span :if={endgame.themes == []} class="text-base-content/50">None</span>
|
||||
</td>
|
||||
<td class="w-0 font-semibold">
|
||||
<.link navigate={~p"/endgames/#{endgame}/play"}>Play</.link>
|
||||
<.link navigate={~p"/endgames/#{endgame.public_code}/play"}>Play</.link>
|
||||
</td>
|
||||
</tr>
|
||||
</:row>
|
||||
|
||||
@@ -33,10 +33,10 @@ defmodule ChesstrainerWeb.EndgameLive.Show do
|
||||
<.button navigate={~p"/admin/endgames"}>
|
||||
<.icon name="hero-arrow-left" />
|
||||
</.button>
|
||||
<.button navigate={~p"/endgames/#{@endgame}/play"}>
|
||||
<.button navigate={~p"/endgames/#{@endgame.public_code}/play"}>
|
||||
<.icon name="hero-play" /> Play (opens trainer)
|
||||
</.button>
|
||||
<.button variant="primary" navigate={~p"/admin/endgames/#{@endgame}/edit?return_to=show"}>
|
||||
<.button variant="primary" navigate={~p"/admin/endgames/#{@endgame.id}/edit?return_to=show"}>
|
||||
<.icon name="hero-pencil-square" /> Edit endgame
|
||||
</.button>
|
||||
</:actions>
|
||||
@@ -47,6 +47,19 @@ defmodule ChesstrainerWeb.EndgameLive.Show do
|
||||
<.list>
|
||||
<:item title="Fen">{@endgame.fen}</:item>
|
||||
<:item title="Key">{@endgame.key}</:item>
|
||||
<:item title="Public code">
|
||||
<span id="public-code" class="select-all">{@endgame.public_code}</span>
|
||||
<button
|
||||
id="copy-public-code"
|
||||
type="button"
|
||||
phx-hook=".CopyPublicCode"
|
||||
data-code={@endgame.public_code}
|
||||
class="ml-2 inline-flex cursor-pointer items-center"
|
||||
title="Copy public code"
|
||||
>
|
||||
<.icon name="hero-document-duplicate" class="w-3.5 h-3.5" />
|
||||
</button>
|
||||
</:item>
|
||||
<:item title="Color">{@endgame.color}</:item>
|
||||
<:item title="Rating">{round(@endgame.rating)}</:item>
|
||||
<:item title="Message">{@endgame.message}</:item>
|
||||
@@ -56,6 +69,16 @@ defmodule ChesstrainerWeb.EndgameLive.Show do
|
||||
<:item title="Attempted">{@endgame.times_attempted}</:item>
|
||||
<:item title="Solved">{@endgame.times_solved}</:item>
|
||||
</.list>
|
||||
|
||||
<script :type={Phoenix.LiveView.ColocatedHook} name=".CopyPublicCode">
|
||||
export default {
|
||||
mounted() {
|
||||
this.el.addEventListener("click", () => {
|
||||
navigator.clipboard.writeText(this.el.dataset.code)
|
||||
})
|
||||
}
|
||||
}
|
||||
</script>
|
||||
</Layouts.app>
|
||||
"""
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user