From 03299a360f2d2d80ca015b28f67290b9c0dc7740 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kat=20March=C3=A1n?= Date: Sat, 20 Apr 2024 09:09:49 -0700 Subject: [PATCH] fix crash from switching between Work edit and view modes (#807) --- lib/banchan_web/live/work_live/work.ex | 46 ++++++++++++++------------ 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/lib/banchan_web/live/work_live/work.ex b/lib/banchan_web/live/work_live/work.ex index 657bfcf1..5f1d5ccd 100644 --- a/lib/banchan_web/live/work_live/work.ex +++ b/lib/banchan_web/live/work_live/work.ex @@ -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