From d1f57d75146636ef368c781af2b1f2cfacffdfa4 Mon Sep 17 00:00:00 2001 From: Chris McCord Date: Thu, 18 Nov 2021 11:50:15 -0500 Subject: [PATCH] Fix bad url recovery --- lib/live_beats_web/live/song_live/index.ex | 49 ++++++++++++---------- lib/live_beats_web/router.ex | 2 +- 2 files changed, 28 insertions(+), 23 deletions(-) diff --git a/lib/live_beats_web/live/song_live/index.ex b/lib/live_beats_web/live/song_live/index.ex index c6d54bf..84d5026 100644 --- a/lib/live_beats_web/live/song_live/index.ex +++ b/lib/live_beats_web/live/song_live/index.ex @@ -32,7 +32,7 @@ defmodule LiveBeatsWeb.SongLive.Index do <% end %> <%= 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}/>Upload Songs <% end %> @@ -106,7 +106,8 @@ defmodule LiveBeatsWeb.SongLive.Index do end 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 def handle_event("play_or_pause", %{"id" => id}, socket) do @@ -196,27 +197,17 @@ defmodule LiveBeatsWeb.SongLive.Index do 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 - socket - |> assign(:page_title, "Add Songs") - |> assign(:song, %MediaLibrary.Song{}) + if socket.assigns.owns_profile? do + socket + |> 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 defp apply_action(socket, :index, _params) do @@ -225,6 +216,19 @@ defmodule LiveBeatsWeb.SongLive.Index do |> assign(:song, nil) 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 assign(socket, songs: MediaLibrary.list_profile_songs(socket.assigns.profile, 50)) end @@ -235,6 +239,7 @@ defmodule LiveBeatsWeb.SongLive.Index do end defp url_text(nil), do: "" + defp url_text(url_str) do uri = URI.parse(url_str) uri.host <> uri.path diff --git a/lib/live_beats_web/router.ex b/lib/live_beats_web/router.ex index b9d0e83..8929233 100644 --- a/lib/live_beats_web/router.ex +++ b/lib/live_beats_web/router.ex @@ -54,7 +54,7 @@ defmodule LiveBeatsWeb.Router do live_session :authenticated, 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/settings", SettingsLive, :edit end