Add "Save and continue" button to Translation form
This commit is contained in:
@ -33,7 +33,12 @@ defmodule OutlookWeb.TranslationLive.FormComponent do
|
|||||||
<.input field={{f, :unauthorized}} type="checkbox" label="unauthorized" />
|
<.input field={{f, :unauthorized}} type="checkbox" label="unauthorized" />
|
||||||
</div>
|
</div>
|
||||||
<:actions>
|
<: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>
|
</:actions>
|
||||||
</.simple_form>
|
</.simple_form>
|
||||||
<.tunit_editor current_tunit={@current_tunit} target={@myself} />
|
<.tunit_editor current_tunit={@current_tunit} target={@myself} />
|
||||||
@ -92,12 +97,17 @@ defmodule OutlookWeb.TranslationLive.FormComponent do
|
|||||||
{:noreply, assign(socket, :changeset, changeset)}
|
{:noreply, assign(socket, :changeset, changeset)}
|
||||||
end
|
end
|
||||||
|
|
||||||
def handle_event("save", %{"translation" => translation_params}, socket) do
|
def handle_event("save", %{"translation" => translation_params} = params, socket) do
|
||||||
socket = socket
|
socket = socket
|
||||||
|> update_translation_with_current_tunit(socket.assigns.current_tunit.status)
|
|> update_translation_with_current_tunit(socket.assigns.current_tunit.status)
|
||||||
translation_params = translation_params
|
params = %{params |
|
||||||
|> Map.put("content", socket.assigns.translation_content)
|
"translation" => Map.put(
|
||||||
save_translation(socket, socket.assigns.action, translation_params)
|
translation_params,
|
||||||
|
"content",
|
||||||
|
socket.assigns.translation_content
|
||||||
|
)
|
||||||
|
}
|
||||||
|
save_translation(socket, socket.assigns.action, params)
|
||||||
end
|
end
|
||||||
|
|
||||||
def handle_event("tunit_status", %{"status" => status}, socket) do
|
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))
|
|> Map.put(socket.assigns.current_tunit.nid, socket.assigns.current_tunit))
|
||||||
end
|
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
|
case Translations.update_translation(socket.assigns.translation, translation_params) do
|
||||||
{:ok, _translation} ->
|
{:ok, _translation} ->
|
||||||
{:noreply,
|
{:noreply,
|
||||||
socket
|
socket
|
||||||
|> put_flash(:info, "Translation updated successfully")
|
|> put_flash(:info, "Translation updated successfully")
|
||||||
|> push_navigate(to: socket.assigns.navigate)}
|
|> continue_edit(params)}
|
||||||
|
|
||||||
{:error, %Ecto.Changeset{} = changeset} ->
|
{:error, %Ecto.Changeset{} = changeset} ->
|
||||||
{:noreply, assign(socket, :changeset, changeset)}
|
{:noreply, assign(socket, :changeset, changeset)}
|
||||||
end
|
end
|
||||||
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
|
case Translations.create_translation(translation_params) do
|
||||||
{:ok, _translation} ->
|
{:ok, _translation} ->
|
||||||
{:noreply,
|
{:noreply,
|
||||||
socket
|
socket
|
||||||
|> put_flash(:info, "Translation created successfully")
|
|> put_flash(:info, "Translation created successfully")
|
||||||
|> push_navigate(to: socket.assigns.navigate)}
|
|> continue_edit(params)}
|
||||||
|
|
||||||
{:error, %Ecto.Changeset{} = changeset} ->
|
{:error, %Ecto.Changeset{} = changeset} ->
|
||||||
{:noreply, assign(socket, changeset: changeset)}
|
{:noreply, assign(socket, changeset: changeset)}
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user