Add "Save and continue" button to Translation form

This commit is contained in:
Thelonius Kort
2023-01-12 20:59:14 +01:00
parent ecb1ef2eee
commit fd2eb5503c

View File

@ -33,7 +33,12 @@ defmodule OutlookWeb.TranslationLive.FormComponent do
<.input field={{f, :unauthorized}} type="checkbox" label="unauthorized" />
</div>
<:actions>
<.button phx-disable-with="Saving...">Save Translation</.button>
<.button phx-click={JS.set_attribute({"value", "false"}, to: "#continue_edit")}
phx-disable-with="Saving...">Save Translation</.button>
<input type="hidden" id="continue_edit" name="continue_edit"
value="false" />
<.button phx-click={JS.set_attribute({"value", "true"}, to: "#continue_edit")}
phx-disable-with="Saving...">Save and Edit</.button>
</:actions>
</.simple_form>
<.tunit_editor current_tunit={@current_tunit} target={@myself} />
@ -92,12 +97,17 @@ defmodule OutlookWeb.TranslationLive.FormComponent do
{:noreply, assign(socket, :changeset, changeset)}
end
def handle_event("save", %{"translation" => translation_params}, socket) do
def handle_event("save", %{"translation" => translation_params} = params, socket) do
socket = socket
|> update_translation_with_current_tunit(socket.assigns.current_tunit.status)
translation_params = translation_params
|> Map.put("content", socket.assigns.translation_content)
save_translation(socket, socket.assigns.action, translation_params)
params = %{params |
"translation" => Map.put(
translation_params,
"content",
socket.assigns.translation_content
)
}
save_translation(socket, socket.assigns.action, params)
end
def handle_event("tunit_status", %{"status" => status}, socket) do
@ -143,29 +153,36 @@ defmodule OutlookWeb.TranslationLive.FormComponent do
|> Map.put(socket.assigns.current_tunit.nid, socket.assigns.current_tunit))
end
defp save_translation(socket, :edit, translation_params) do
defp save_translation(socket, :edit, %{"translation" => translation_params} = 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)}
|> continue_edit(params)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, :changeset, changeset)}
end
end
defp save_translation(socket, :new, translation_params) do
defp save_translation(socket, :new, %{"translation" => translation_params} = 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)}
|> continue_edit(params)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, changeset: changeset)}
end
end
defp continue_edit(socket, %{"continue_edit" => "true"}) do
socket
end
defp continue_edit(socket, %{"continue_edit" => "false"}) do
socket |> push_navigate(to: socket.assigns.navigate)
end
end