Fix bad url recovery

This commit is contained in:
Chris McCord 2021-11-18 11:50:15 -05:00
parent 0ef460675c
commit d1f57d7514
2 changed files with 28 additions and 23 deletions

View file

@ -32,7 +32,7 @@ defmodule LiveBeatsWeb.SongLive.Index do
</.button> </.button>
<% end %> <% end %>
<%= if @owns_profile? do %> <%= if @owns_profile? do %>
<.button primary patch_to={Routes.song_index_path(@socket, :new)}> <.button primary patch_to={Routes.song_index_path(@socket, :new, @current_user.username)}>
<.icon name={:upload}/><span class="ml-2">Upload Songs</span> <.icon name={:upload}/><span class="ml-2">Upload Songs</span>
</.button> </.button>
<% end %> <% end %>
@ -106,7 +106,8 @@ defmodule LiveBeatsWeb.SongLive.Index do
end end
def handle_params(params, _url, socket) do def handle_params(params, _url, socket) do
{:noreply, socket |> apply_action(socket.assigns.live_action, params) |> maybe_show_modal()} LayoutComponent.hide_modal()
{:noreply, socket |> apply_action(socket.assigns.live_action, params)}
end end
def handle_event("play_or_pause", %{"id" => id}, socket) do def handle_event("play_or_pause", %{"id" => id}, socket) do
@ -196,27 +197,17 @@ defmodule LiveBeatsWeb.SongLive.Index do
end end
end end
defp maybe_show_modal(socket) do
if socket.assigns.live_action in [:new] do
LayoutComponent.show_modal(UploadFormComponent, %{
id: :new,
confirm: {"Save", type: "submit", form: "song-form"},
patch_to: profile_path(socket.assigns.current_user),
song: socket.assigns.song,
title: socket.assigns.page_title,
current_user: socket.assigns.current_user
})
else
LayoutComponent.hide_modal()
end
socket
end
defp apply_action(socket, :new, _params) do defp apply_action(socket, :new, _params) do
socket if socket.assigns.owns_profile? do
|> assign(:page_title, "Add Songs") socket
|> assign(:song, %MediaLibrary.Song{}) |> assign(:page_title, "Add Songs")
|> assign(:song, %MediaLibrary.Song{})
|> show_upload_modal()
else
socket
|> put_flash(:error, "You can't do that")
|> redirect(to: profile_path(socket.assigns.current_user))
end
end end
defp apply_action(socket, :index, _params) do defp apply_action(socket, :index, _params) do
@ -225,6 +216,19 @@ defmodule LiveBeatsWeb.SongLive.Index do
|> assign(:song, nil) |> assign(:song, nil)
end end
defp show_upload_modal(socket) do
LayoutComponent.show_modal(UploadFormComponent, %{
id: :new,
confirm: {"Save", type: "submit", form: "song-form"},
patch_to: profile_path(socket.assigns.current_user),
song: socket.assigns.song,
title: socket.assigns.page_title,
current_user: socket.assigns.current_user
})
socket
end
defp list_songs(socket) do defp list_songs(socket) do
assign(socket, songs: MediaLibrary.list_profile_songs(socket.assigns.profile, 50)) assign(socket, songs: MediaLibrary.list_profile_songs(socket.assigns.profile, 50))
end end
@ -235,6 +239,7 @@ defmodule LiveBeatsWeb.SongLive.Index do
end end
defp url_text(nil), do: "" defp url_text(nil), do: ""
defp url_text(url_str) do defp url_text(url_str) do
uri = URI.parse(url_str) uri = URI.parse(url_str)
uri.host <> uri.path uri.host <> uri.path

View file

@ -54,7 +54,7 @@ defmodule LiveBeatsWeb.Router do
live_session :authenticated, live_session :authenticated,
on_mount: [{LiveBeatsWeb.UserAuth, :ensure_authenticated}, LiveBeatsWeb.Nav] do on_mount: [{LiveBeatsWeb.UserAuth, :ensure_authenticated}, LiveBeatsWeb.Nav] do
live "/songs/new", SongLive.Index, :new live "/:profile_username/songs/new", SongLive.Index, :new
live "/:profile_username", SongLive.Index, :index live "/:profile_username", SongLive.Index, :index
live "/profile/settings", SettingsLive, :edit live "/profile/settings", SettingsLive, :edit
end end