defmodule OutlookWeb.TranslationLive.NewEdit do use OutlookWeb, :live_view alias Outlook.{Articles,Translations} alias Outlook.Translations.{Translation,Basic} alias OutlookWeb.TranslationLive.FormComponent @form_cmpnt_id "formkomponente" @impl true def render(assigns) do ~H""" <.live_component module={OutlookWeb.TranslationLive.FormComponent} id={@form_cmpnt_id} title="New Translation" action={@live_action} translation={@translation} translation_content={@translation_content} navigate={~p"/translations"} /> """ end @impl true def mount(%{"article_id" => article_id} = _params, _session, socket) when socket.assigns.live_action == :new do socket = socket |> assign_new(:translation, fn -> %Translation{ article_id: article_id, article: get_article(article_id) } end) |> common_assigns() {:ok, assign_new(socket, :translation_content, fn -> Basic.internal_tree_to_tunit_map(socket.assigns.translation.article.content) end)} end def mount(%{"id" => id} = _params, _session, socket) when socket.assigns.live_action == :edit do socket = socket |> assign_new(:translation, fn -> Translations.get_translation!(id) end) |> common_assigns() {:ok, assign_new(socket, :translation_content, fn -> socket.assigns.translation.content end)} end defp get_article(article_id) do Articles.get_article!(article_id) end defp common_assigns(socket) do assign(socket, form_cmpnt_id: @form_cmpnt_id) end @impl true def handle_info({:progress, payload}, socket) do send_update(self(), FormComponent, progress: payload.progress, id: @form_cmpnt_id) {:noreply, socket} end end