Update Deepl translation

Now getting auth-key from db.
This commit is contained in:
Thelonius Kort
2023-01-09 21:29:19 +01:00
parent 3cd9d983fe
commit 71e6a8da60
4 changed files with 111 additions and 82 deletions

View File

@ -63,9 +63,21 @@ defmodule OutlookWeb.TranslationLive.FormComponent 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) do
{:ok, new_translation_content} ->
{:ok, socket
|> assign(translation_content: new_translation_content)
|> update_current_tunit()}
{:error, message} ->
{:ok, socket |> put_flash(:error, message)}
end
end
@impl true
def handle_event("translate-deepl", _, socket) do
Task.start_link(Outlook.Translators.Deepl, :test, [self()])
Outlook.Translators.translate(socket.assigns.translation, socket.assigns.current_user)
{:noreply, socket}
end
@ -98,11 +110,21 @@ defmodule OutlookWeb.TranslationLive.FormComponent do
|> assign(:current_tunit, socket.assigns.translation_content[uuid])}
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) when is_struct(socket.assigns.current_tunit) do
assign(socket,
:current_tunit,
socket.assigns.translation_content[socket.assigns.current_tunit.uuid])
end
defp update_current_tunit(socket), do: socket
defp update_translation_with_current_tunit(socket) do
translation_content = if socket.assigns.current_tunit do
socket.assigns.translation_content

View File

@ -17,6 +17,7 @@ defmodule OutlookWeb.TranslationLive.NewEdit do
action={@live_action}
translation={@translation}
translation_content={@translation_content}
current_user={@current_user}
navigate={~p"/translations"}
/>
"""
@ -56,4 +57,9 @@ defmodule OutlookWeb.TranslationLive.NewEdit do
send_update(self(), FormComponent, progress: payload.progress, id: @form_cmpnt_id)
{:noreply, socket}
end
def handle_info({:translation, payload}, socket) do
send_update(self(), FormComponent, deepl_translation: payload, id: @form_cmpnt_id)
{:noreply, socket}
end
end