Skip to content

Commit

Permalink
fix crash from switching between Work edit and view modes (#807)
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat authored Apr 20, 2024
1 parent b97a9e6 commit 03299a3
Showing 1 changed file with 25 additions and 21 deletions.
46 changes: 25 additions & 21 deletions lib/banchan_web/live/work_live/work.ex
Original file line number Diff line number Diff line change
Expand Up @@ -316,29 +316,33 @@ defmodule BanchanWeb.WorkLive.Work do
end

def handle_info({:updated_uploads, _, uploads}, socket) do
uploads_param =
uploads
|> Enum.with_index()
|> Enum.map(fn {{ty, data}, index} ->
if ty == :live do
%WorkUpload{}
|> WorkUpload.changeset(%{"index" => index, "ref" => data.ref, "comment" => ""})
else
data
|> WorkUpload.changeset(%{"index" => index})
end
end)
if is_nil(socket.assigns.changeset) do
{:noreply, socket}
else
uploads_param =
uploads
|> Enum.with_index()
|> Enum.map(fn {{ty, data}, index} ->
if ty == :live do
%WorkUpload{}
|> WorkUpload.changeset(%{"index" => index, "ref" => data.ref, "comment" => ""})
else
data
|> WorkUpload.changeset(%{"index" => index})
end
end)

changeset =
Work.changeset(
socket.assigns.work,
socket.assigns.changeset.params |> Map.put("uploads", uploads_param)
)
|> Map.put(:action, if(is_nil(socket.assigns.work.id), do: :insert, else: :update))
changeset =
Work.changeset(
socket.assigns.work,
socket.assigns.changeset.params |> Map.put("uploads", uploads_param)
)
|> Map.put(:action, if(is_nil(socket.assigns.work.id), do: :insert, else: :update))

{:noreply,
socket
|> assign(work_uploads: uploads, changeset: changeset)}
{:noreply,
socket
|> assign(work_uploads: uploads, changeset: changeset)}
end
end

@impl true
Expand Down

0 comments on commit 03299a3

Please sign in to comment.