diff --git a/lib/outlook_web/live/translation_live/form_component.ex b/lib/outlook_web/live/translation_live/form_component.ex index eeb60e2..a654b05 100644 --- a/lib/outlook_web/live/translation_live/form_component.ex +++ b/lib/outlook_web/live/translation_live/form_component.ex @@ -164,7 +164,7 @@ defmodule OutlookWeb.TranslationLive.FormComponent do {:noreply, socket |> put_flash(:info, "Translation updated successfully") - |> continue_edit(params)} + |> continue_edit(:edit, params)} {:error, %Ecto.Changeset{} = changeset} -> {:noreply, assign(socket, :changeset, changeset)} @@ -173,22 +173,24 @@ defmodule OutlookWeb.TranslationLive.FormComponent do defp save_translation(socket, :new, %{"translation" => translation_params} = params) do case Translations.create_translation(translation_params) do - {:ok, _translation} -> + {:ok, translation} -> {:noreply, socket |> put_flash(:info, "Translation created successfully") - |> continue_edit(params)} + |> continue_edit(:new, Map.put(params,"id", translation.id))} {:error, %Ecto.Changeset{} = changeset} -> {:noreply, assign(socket, changeset: changeset)} end end - defp continue_edit(socket, %{"continue_edit" => "true"}) do + defp continue_edit(socket, :edit, %{"continue_edit" => "true"}) do socket - |> assign(:action, :edit) end - defp continue_edit(socket, %{"continue_edit" => "false"}) do + 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 diff --git a/lib/outlook_web/live/translation_live/new_edit.ex b/lib/outlook_web/live/translation_live/new_edit.ex index c092260..ecfdf12 100644 --- a/lib/outlook_web/live/translation_live/new_edit.ex +++ b/lib/outlook_web/live/translation_live/new_edit.ex @@ -24,7 +24,7 @@ defmodule OutlookWeb.TranslationLive.NewEdit do end @impl true - def mount(%{"article_id" => article_id} = _params, _session, socket) when socket.assigns.live_action == :new do + def handle_params(%{"article_id" => article_id} = _params, _session, socket) when socket.assigns.live_action == :new do socket = socket |> assign_new(:translation, fn -> %Translation{ @@ -33,15 +33,15 @@ defmodule OutlookWeb.TranslationLive.NewEdit do } end) |> common_assigns() - {:ok, assign_new(socket, :translation_content, fn -> + {:noreply, 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 + def handle_params(%{"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)} + |> assign(:translation, Translations.get_translation!(id)) + |> common_assigns() + {:noreply, assign(socket, :translation_content, socket.assigns.translation.content)} end defp get_article(article_id) do