Fix issues with changed default :current_tunit

This commit is contained in:
Thelonius Kort
2023-01-11 11:34:02 +01:00
parent 24e58c7d96
commit e0cd51937a

View File

@ -71,7 +71,7 @@ defmodule OutlookWeb.TranslationLive.FormComponent do
{:ok, new_translation_content} -> {:ok, new_translation_content} ->
{:ok, socket {:ok, socket
|> assign(translation_content: new_translation_content) |> assign(translation_content: new_translation_content)
|> update_current_tunit()} |> update_current_tunit(socket.assigns.current_tunit.status)}
{:error, message} -> {:error, message} ->
{:ok, socket |> put_flash(:error, message)} {:ok, socket |> put_flash(:error, message)}
end end
@ -94,7 +94,7 @@ defmodule OutlookWeb.TranslationLive.FormComponent do
def handle_event("save", %{"translation" => translation_params}, socket) do def handle_event("save", %{"translation" => translation_params}, socket) do
socket = socket socket = socket
|> update_translation_with_current_tunit() |> update_translation_with_current_tunit(socket.assigns.current_tunit.status)
translation_params = translation_params translation_params = translation_params
|> Map.put("content", socket.assigns.translation_content) |> Map.put("content", socket.assigns.translation_content)
save_translation(socket, socket.assigns.action, translation_params) 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 def handle_event("select_current_tunit", %{"uuid" => uuid}, socket) do
{:noreply, {:noreply,
socket socket
|> update_translation_with_current_tunit |> update_translation_with_current_tunit(socket.assigns.current_tunit.status)
|> assign(:current_tunit, socket.assigns.translation_content[uuid])} |> assign(:current_tunit, socket.assigns.translation_content[uuid])}
end end
@ -120,12 +120,12 @@ defmodule OutlookWeb.TranslationLive.FormComponent do
# updating after Deepl translation # 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, assign(socket,
:current_tunit, :current_tunit,
socket.assigns.translation_content[socket.assigns.current_tunit.uuid]) socket.assigns.translation_content[socket.assigns.current_tunit.uuid])
end end
defp update_current_tunit(socket), do: socket
defp assign_article_tree(socket, translation) do defp assign_article_tree(socket, translation) do
socket socket
@ -135,15 +135,12 @@ defmodule OutlookWeb.TranslationLive.FormComponent do
%{tunits: %{phx: %{click: "select_current_tunit", target: socket.assigns.myself}}})) %{tunits: %{phx: %{click: "select_current_tunit", target: socket.assigns.myself}}}))
end end
defp update_translation_with_current_tunit(socket) do defp update_translation_with_current_tunit(socket, nil), do: socket
translation_content = if socket.assigns.current_tunit do defp update_translation_with_current_tunit(socket, _) do
socket.assigns.translation_content
|> Map.put(socket.assigns.current_tunit.uuid, socket.assigns.current_tunit)
else
socket.assigns.translation_content
end
socket 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 end
defp save_translation(socket, :edit, translation_params) do defp save_translation(socket, :edit, translation_params) do