From e0cd51937a25ef67b6fecd4b40eb8bfa3f24acc2 Mon Sep 17 00:00:00 2001 From: Thelonius Kort Date: Wed, 11 Jan 2023 11:34:02 +0100 Subject: [PATCH] Fix issues with changed default :current_tunit --- .../live/translation_live/form_component.ex | 23 ++++++++----------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/lib/outlook_web/live/translation_live/form_component.ex b/lib/outlook_web/live/translation_live/form_component.ex index e659484..e80a596 100644 --- a/lib/outlook_web/live/translation_live/form_component.ex +++ b/lib/outlook_web/live/translation_live/form_component.ex @@ -71,7 +71,7 @@ defmodule OutlookWeb.TranslationLive.FormComponent do {:ok, new_translation_content} -> {:ok, socket |> assign(translation_content: new_translation_content) - |> update_current_tunit()} + |> update_current_tunit(socket.assigns.current_tunit.status)} {:error, message} -> {:ok, socket |> put_flash(:error, message)} end @@ -94,7 +94,7 @@ defmodule OutlookWeb.TranslationLive.FormComponent do def handle_event("save", %{"translation" => translation_params}, socket) do socket = socket - |> update_translation_with_current_tunit() + |> update_translation_with_current_tunit(socket.assigns.current_tunit.status) translation_params = translation_params |> Map.put("content", socket.assigns.translation_content) save_translation(socket, socket.assigns.action, translation_params) @@ -108,7 +108,7 @@ defmodule OutlookWeb.TranslationLive.FormComponent do def handle_event("select_current_tunit", %{"uuid" => uuid}, socket) do {:noreply, socket - |> update_translation_with_current_tunit + |> update_translation_with_current_tunit(socket.assigns.current_tunit.status) |> assign(:current_tunit, socket.assigns.translation_content[uuid])} end @@ -120,12 +120,12 @@ defmodule OutlookWeb.TranslationLive.FormComponent do # updating after Deepl translation - defp update_current_tunit(socket) when is_struct(socket.assigns.current_tunit) do + defp update_current_tunit(socket, nil), do: socket + defp update_current_tunit(socket, _) do assign(socket, :current_tunit, socket.assigns.translation_content[socket.assigns.current_tunit.uuid]) end - defp update_current_tunit(socket), do: socket defp assign_article_tree(socket, translation) do socket @@ -135,15 +135,12 @@ defmodule OutlookWeb.TranslationLive.FormComponent do %{tunits: %{phx: %{click: "select_current_tunit", target: socket.assigns.myself}}})) end - defp update_translation_with_current_tunit(socket) do - translation_content = if socket.assigns.current_tunit do - socket.assigns.translation_content - |> Map.put(socket.assigns.current_tunit.uuid, socket.assigns.current_tunit) - else - socket.assigns.translation_content - end + defp update_translation_with_current_tunit(socket, nil), do: socket + defp update_translation_with_current_tunit(socket, _) do socket - |> assign(:translation_content, translation_content) + |> assign(:translation_content, + socket.assigns.translation_content + |> Map.put(socket.assigns.current_tunit.uuid, socket.assigns.current_tunit)) end defp save_translation(socket, :edit, translation_params) do