diff --git a/lib/phoenix_live_view/upload.ex b/lib/phoenix_live_view/upload.ex index fe37b9a301..9e889d2b04 100644 --- a/lib/phoenix_live_view/upload.ex +++ b/lib/phoenix_live_view/upload.ex @@ -87,12 +87,23 @@ defmodule Phoenix.LiveView.Upload do Cancels all uploads that exist. Returns the new socket with the cancelled upload configs. + + You can optionally pass the uploads name to only cancel uploads for that name """ def maybe_cancel_uploads(socket) do + maybe_cancel_uploads_with_custom_filter_fun(socket, & &1) + end + + def maybe_cancel_uploads(socket, name) do + maybe_cancel_uploads_with_custom_filter_fun(socket, &Map.take(&1, [name])) + end + + defp maybe_cancel_uploads_with_custom_filter_fun(socket, filter_fun) do uploads = socket.assigns[:uploads] || %{} uploads |> Map.delete(@refs_to_names) + |> filter_fun.() |> Enum.reduce({socket, []}, fn {name, conf}, {socket_acc, conf_acc} -> new_socket = Enum.reduce(conf.entries, socket_acc, fn entry, inner_acc ->