defmodule OutlookWeb.TranslationLive.FormComponent do
use OutlookWeb, :live_component
alias Outlook.{Translations,InternalTree}
alias Outlook.InternalTree.{TranslationUnit,Html}
@impl true
def render(assigns) do
~H"""
<.header>
<%= @title %>
<:subtitle>Use this form to manage translation records in your database.
<:actions>
<.button phx-click={JS.set_attribute({"value", "false"}, to: "#continue_edit") |> JS.set_attribute({"value", "false"}, to: "#publish")}
id="save-button" phx-disable-with="Saving...">Save Translation
<.button phx-click={JS.set_attribute({"value", "false"}, to: "#continue_edit") |> JS.set_attribute({"value", "true"}, to: "#publish")}
id="save-publish-button" phx-disable-with="Saving...">Save and Publish
<.button phx-click={JS.set_attribute({"value", "true"}, to: "#continue_edit") |> JS.set_attribute({"value", "false"}, to: "#publish")}
id="save-edit-button" phx-disable-with="Saving...">Save and Edit
<.tunit_editor current_tunit={@current_tunit} target={@myself} />
<.button phx-disable-with="Translating..." phx-click="translate-deepl" phx-target={@myself}
data-confirm-not="Are you sure? All previously translated text will be lost.">Translate with Deepl
<.render_doc tree={@article_tree} />
"""
end
@impl true
def update(%{translation: translation} = assigns, socket) do
changeset = Translations.change_translation(translation)
{:ok,
socket
|> assign(assigns)
|> assign(:current_tunit, %TranslationUnit{status: nil})
|> assign(:tunit_ids, InternalTree.get_tunit_ids(translation.article.content))
|> assign(:changeset, changeset)
|> assign_article_tree(translation)
|> assign(:deepl_progress, nil)}
end
def update(%{progress: progress}, socket) do
{:ok, socket |> assign(deepl_progress: progress)}
end
def update(%{deepl_translation: translation}, socket) do
tunit_keys = Map.keys(socket.assigns.translation_content)
case Outlook.Translators.process_translation_result(translation, tunit_keys, socket.assigns.current_user) do
{:ok, new_translation_content} ->
{:ok, socket
|> assign(translation_content: new_translation_content)
|> update_current_tunit(socket.assigns.current_tunit.status)}
{:error, message} ->
{:ok, socket |> put_flash(:error, message)}
end
end
@impl true
def handle_event("translate-deepl", _, socket) do
Outlook.Translators.translate(socket.assigns.translation, socket.assigns.current_user)
{:noreply, socket}
end
def handle_event("validate", %{"translation" => translation_params}, socket) do
changeset =
socket.assigns.translation
|> Translations.change_translation(translation_params)
|> Map.put(:action, :validate)
{:noreply, assign(socket, :changeset, changeset)}
end
def handle_event("save", %{"translation" => translation_params} = params, socket) do
socket = socket
|> update_translation_with_current_tunit(socket.assigns.current_tunit.status)
params = %{params |
"translation" => Map.put(
translation_params,
"content",
socket.assigns.translation_content
)
|> publish(params, socket)
}
save_translation(socket, socket.assigns.action, params)
end
def handle_event("tunit_status", %{"status" => status}, socket) do
tunit = %TranslationUnit{socket.assigns.current_tunit | status: String.to_atom(status)}
{:noreply, socket |> assign(:current_tunit, tunit)}
end
def handle_event("select_tunit_by_nid", %{"nid" => nid}, socket) do
{:noreply, change_tunit(socket, nid)}
end
def handle_event("select_next_tunit", _, socket) do
{:noreply, select_next_tunit(socket, &Kernel.+/2)}
end
def handle_event("select_previous_tunit", _, socket) do
{:noreply, select_next_tunit(socket, &Kernel.-/2)}
end
defp select_next_tunit(socket, direction) do
tunit_ids = socket.assigns.tunit_ids
current_tunit_nid = socket.assigns.current_tunit.status && socket.assigns.current_tunit.nid || List.last(tunit_ids)
index_current = Enum.find_index(tunit_ids, fn nid -> nid == current_tunit_nid end)
index_next = direction.(index_current, 1) |> Integer.mod(length(tunit_ids))
next_nid = Enum.at(tunit_ids, index_next)
change_tunit(socket, next_nid)
end
defp change_tunit(socket, nid) do
fun = fn n -> n.nid == nid && "yes" || "no" end
socket
|> assign(:article_tree, InternalTree.garnish(socket.assigns.article_tree, %{tunits: %{current: fun}}))
|> update_translation_with_current_tunit(socket.assigns.current_tunit.status)
|> assign(:current_tunit, socket.assigns.translation_content[nid])
end
@doc "updating on browser events"
def handle_event("update_current_tunit", %{"content" => content}, socket) do
tunit = %TranslationUnit{socket.assigns.current_tunit | content: content}
{:noreply, socket |> assign(:current_tunit, tunit)}
end
# updating after Deepl translation
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.nid])
end
defp assign_article_tree(socket, translation) do
socket
|> assign(
:article_tree,
InternalTree.add_phx_click_event(translation.article.content,
nodes: :tunits,
click: "select_tunit_by_nid",
target: socket.assigns.myself)
|> InternalTree.garnish(%{tunits: %{class: "tunit"}}))
end
defp update_translation_with_current_tunit(socket, nil), do: socket
defp update_translation_with_current_tunit(socket, _) do
socket
|> assign(:translation_content,
socket.assigns.translation_content
|> Map.put(socket.assigns.current_tunit.nid, socket.assigns.current_tunit))
end
defp save_translation(socket, :edit, %{"translation" => translation_params} = params) do
case Translations.update_translation(socket.assigns.translation, translation_params) do
{:ok, _translation} ->
{:noreply,
socket
|> put_flash(:info, "Translation updated successfully")
|> continue_edit(:edit, params)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, :changeset, changeset)}
end
end
defp save_translation(socket, :new, %{"translation" => translation_params} = params) do
case Translations.create_translation(translation_params) do
{:ok, translation} ->
{:noreply,
socket
|> put_flash(:info, "Translation created successfully")
|> continue_edit(:new, Map.put(params,"id", translation.id))}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, changeset: changeset)}
end
end
defp continue_edit(socket, :edit, %{"continue_edit" => "true"}) do
socket
|> assign(:translation, Translations.get_translation!(socket.assigns.translation.id))
end
defp continue_edit(socket, :new, %{"continue_edit" => "true"} = params) do
socket |> push_patch(to: ~p(/translations/#{params["id"]}/edit))
end
defp continue_edit(socket, _, %{"continue_edit" => "false"}) do
socket |> push_navigate(to: socket.assigns.navigate)
end
defp publish(translation_params, %{"publish" => "true"}, socket) do
translation_params
|> Map.put("public_content",
InternalTree.render_public_content(
socket.assigns.translation.article.content,
socket.assigns.translation_content,
socket.assigns.translation.language
)
)
end
defp publish(translation_params, %{"publish" => "false"}, _) do
translation_params
end
end