Skip to content

Commit

Permalink
Merge pull request #573 from podlove/feature/update-episode
Browse files Browse the repository at this point in the history
feature: Reload episodes after creating a new or updating an existing…
  • Loading branch information
electronicbites authored Oct 1, 2024
2 parents dbf2427 + 92c5f5f commit 1cf5944
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/radiator/podcast.ex
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,23 @@ defmodule Radiator.Podcast do
Show.changeset(show, attrs)
end

@doc """
A forced reload of preloaded associations.
Usefull when only the associations have changed and Show does need to be reloaded.
## Examples
iex> reload_assoc(%Show{}, :episodes)
%Show{}
iex> reload_assoc(%Show{}, (from e in Episode, where: e.show_id == ^show_id, order_by: [desc: e.number]))
%Show{}
"""
def reload_assoc(%Show{} = show, fields_or_query) do
Repo.preload(show, fields_or_query, force: true)
end

@doc """
Returns the list of episodes.
Expand Down
8 changes: 8 additions & 0 deletions lib/radiator_web/live/episode_live/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,11 @@ defmodule RadiatorWeb.EpisodeLive.Index do
"episode_id" => episode.id
})

show = Podcast.reload_assoc(socket.assigns.show, [:episodes])

socket
|> assign(:show, show)
|> assign(:episodes, show.episodes)
|> put_flash(:info, "Episode created")
|> push_patch(to: ~p"/admin/podcast/#{socket.assigns.show}/#{episode}")
|> reply(:noreply)
Expand All @@ -133,7 +137,11 @@ defmodule RadiatorWeb.EpisodeLive.Index do
defp save_episode(socket, :edit, params) do
case Podcast.update_episode(socket.assigns.episode, params) do
{:ok, episode} ->
show = Podcast.reload_assoc(socket.assigns.show, [:episodes])

socket
|> assign(:show, show)
|> assign(:episodes, show.episodes)
|> put_flash(:info, "Episode updated")
|> push_patch(to: ~p"/admin/podcast/#{socket.assigns.show}/#{episode}")
|> reply(:noreply)
Expand Down

0 comments on commit 1cf5944

Please sign in to comment.