142 lines
5.0 KiB
Elixir
142 lines
5.0 KiB
Elixir
defmodule OutlookWeb.TranslationLive.FormComponent do
|
|
use OutlookWeb, :live_component
|
|
|
|
alias Outlook.{Translations,InternalTree}
|
|
alias Outlook.InternalTree.TranslationUnit
|
|
|
|
@impl true
|
|
def render(assigns) do
|
|
~H"""
|
|
<div class="flex">
|
|
<div>
|
|
<.header>
|
|
<%= @title %>
|
|
<:subtitle>Use this form to manage translation records in your database.</:subtitle>
|
|
</.header>
|
|
|
|
<.simple_form
|
|
:let={f}
|
|
for={@changeset}
|
|
id="translation-form"
|
|
phx-target={@myself}
|
|
phx-change="validate"
|
|
phx-submit="save"
|
|
>
|
|
<.input field={{f, :article_id}} type="hidden" />
|
|
<.input field={{f, :lang}} type="select" label="lang"
|
|
options={Application.get_env(:outlook,:deepl)[:target_langs]} />
|
|
<.input field={{f, :title}} type="text" label="title" />
|
|
<.input field={{f, :teaser}} type="textarea" label="teaser" class="h-28" />
|
|
<%!-- <.input field={{f, :content}} type="text" label="content" /> --%>
|
|
<.input field={{f, :date}} type="datetime-local" label="date" />
|
|
<.input field={{f, :public}} type="checkbox" label="public" />
|
|
<.input field={{f, :unauthorized}} type="checkbox" label="unauthorized" />
|
|
<:actions>
|
|
<.button phx-disable-with="Saving...">Save Translation</.button>
|
|
</:actions>
|
|
</.simple_form>
|
|
<.tunit_editor current_tunit={@current_tunit} target={@myself} />
|
|
</div>
|
|
<div class="article">
|
|
<%= InternalTree.render_html_preview(@translation.article.content, @myself) |> raw %>
|
|
<.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</.button>
|
|
<progress :if={@deepl_progress} max="100" value={@deepl_progress} />
|
|
</div>
|
|
</div>
|
|
"""
|
|
end
|
|
|
|
@impl true
|
|
def update(%{translation: translation} = assigns, socket) do
|
|
changeset = Translations.change_translation(translation)
|
|
|
|
{:ok,
|
|
socket
|
|
|> assign(assigns)
|
|
|> assign(:changeset, changeset)
|
|
|> assign(:deepl_progress, nil)}
|
|
end
|
|
|
|
def update(%{progress: progress}, socket) do
|
|
{:ok, socket |> assign(deepl_progress: progress)}
|
|
end
|
|
|
|
@impl true
|
|
def handle_event("translate-deepl", _, socket) do
|
|
Task.start_link(Outlook.Translators.Deepl, :test, [self()])
|
|
{: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}, socket) do
|
|
socket = socket
|
|
|> update_translation_with_current_tunit()
|
|
translation_params = translation_params
|
|
|> Map.put("content", socket.assigns.translation_content)
|
|
save_translation(socket, socket.assigns.action, translation_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_current_tunit", %{"uuid" => uuid}, socket) do
|
|
{:noreply,
|
|
socket
|
|
|> update_translation_with_current_tunit
|
|
|> assign(:current_tunit, socket.assigns.translation_content[uuid])}
|
|
end
|
|
|
|
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
|
|
|
|
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
|
|
socket
|
|
|> assign(:translation_content, translation_content)
|
|
end
|
|
|
|
defp save_translation(socket, :edit, translation_params) do
|
|
case Translations.update_translation(socket.assigns.translation, translation_params) do
|
|
{:ok, _translation} ->
|
|
{:noreply,
|
|
socket
|
|
|> put_flash(:info, "Translation updated successfully")
|
|
|> push_navigate(to: socket.assigns.navigate)}
|
|
|
|
{:error, %Ecto.Changeset{} = changeset} ->
|
|
{:noreply, assign(socket, :changeset, changeset)}
|
|
end
|
|
end
|
|
|
|
defp save_translation(socket, :new, translation_params) do
|
|
case Translations.create_translation(translation_params) do
|
|
{:ok, _translation} ->
|
|
{:noreply,
|
|
socket
|
|
|> put_flash(:info, "Translation created successfully")
|
|
|> push_navigate(to: socket.assigns.navigate)}
|
|
|
|
{:error, %Ecto.Changeset{} = changeset} ->
|
|
{:noreply, assign(socket, changeset: changeset)}
|
|
end
|
|
end
|
|
end
|