Add creating and basic editing of translation

This commit is contained in:
Thelonius Kort
2023-01-04 14:42:13 +01:00
parent f4b5abef5a
commit d589d84b40
15 changed files with 235 additions and 47 deletions

View File

@ -1,36 +1,45 @@
defmodule OutlookWeb.TranslationLive.FormComponent do
use OutlookWeb, :live_component
alias Outlook.Translations
alias Outlook.{Translations,InternalTree}
alias Outlook.InternalTree.TranslationUnit
@impl true
def render(assigns) do
~H"""
<div>
<.header>
<%= @title %>
<:subtitle>Use this form to manage translation records in your database.</:subtitle>
</.header>
<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, :lang}} type="text" label="lang" />
<.input field={{f, :title}} type="text" label="title" />
<.input field={{f, :teaser}} type="text" label="teaser" />
<.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>
<.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 %>
</div>
</div>
"""
end
@ -56,9 +65,41 @@ defmodule OutlookWeb.TranslationLive.FormComponent do
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} ->