re-integrate redirect

This commit is contained in:
2023-02-17 15:05:27 +01:00
parent be71f04838
commit c70586864b
3 changed files with 32 additions and 1 deletions

View File

@ -6,4 +6,28 @@ defmodule SomethingErlangWeb.PageController do
# so skip the default app layout.
render(conn, :home)
end
def to_forum_path(conn, %{"forum_path" => path} = _params) do
{redirect_good, thread, page} =
case {
Regex.run(~r{threadid=(\d+)}, path),
Regex.run(~r{pagenumber=(\d+)}, path)
} do
{[_, thread], nil} -> {:ok, thread, 1}
{[_, thread], [_, page]} -> {:ok, thread, page}
_ -> {:error, nil, nil}
end
if redirect_good == :ok 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
params |> IO.inspect()
render(conn, :home)
end
end

View File

@ -1,3 +1,8 @@
<.form :let={f} for={@conn} action={~p"/"}>
<input type="url" name="forum_path" />
<input type="submit" class="btn btn-sm" value="Redirect">
</.form>
<pre>
<%= inspect(@current_user) %>
<%= inspect(@current_user) %>
</pre>

View File

@ -21,10 +21,12 @@ defmodule SomethingErlangWeb.Router do
pipe_through [:browser]
get "/", PageController, :home
post "/", PageController, :to_forum_path
live_session :user_browsing,
on_mount: [{SomethingErlangWeb.UserAuth, :mount_current_user}] do
live "/thread", ThreadLive
live "/thread/:id", ThreadLive
end
end