fixed form

This commit is contained in:
2024-06-02 15:23:32 +02:00
parent 31c126a394
commit 6f088f86a1
2 changed files with 46 additions and 6 deletions

View File

@ -1,9 +1,46 @@
defmodule SomethingErlangWeb.PageController do
use SomethingErlangWeb, :controller
def home(conn, _params) do
def home(conn, params) do
# The home page is often custom made,
# so skip the default app layout.
render(conn, :home, layout: false)
conn = assign(conn, :params, params)
render(conn, :home)
end
def to_forum_path(conn, %{"forum_path" => path} = _params) do
with {:ok, thread, page} <- resolve_url(path) do
redirect(conn, to: ~p"/thread/#{thread}?page=#{page}")
else
_ ->
put_flash(conn, :error, "Could not resolve URL")
render(conn, :home)
end
end
def to_forum_path(conn, _params) do
render(conn, :home)
end
defp resolve_url(url) do
threadid =
case Regex.run(~r{threadid=(\d+)}, url) do
[_, threadid] -> {:ok, threadid}
nil -> {:error, :nothreadid}
end
page =
case Regex.run(~r{pagenumber=(\d+)}, url) do
[_, page] -> {:ok, page}
nil -> {:ok, 1}
end
with {:ok, threadid} <- threadid,
{:ok, page} <- page do
{:ok, threadid, page}
else
_ ->
{:error, :cantparseurl}
end
end
end

View File

@ -1,8 +1,11 @@
<.flash_group flash={@flash} />
<.form :let={f} for={@conn} action={~p"/"}>
<input type="url" name="forum_path" />
<input type="submit" class="btn btn-sm" value="Redirect" />
</.form>
<.simple_form for={@conn.params} action={~p"/"} phx-submit="save">
<.input field={@conn.params[:forum_path]} name="forum_path" value="" label="SA Url" />
<:actions>
<.button class="btn btn-sm">Redirect</.button>
</:actions>
</.simple_form>
<pre class="whitespace-pre-wrap w-full overflow-x-auto pb-8">
<%= inspect(@current_user) %>