Skip to content

Commit

Permalink
Merge pull request #635 from Elmseld/feature/330_add_save_and_continu…
Browse files Browse the repository at this point in the history
…e_btn

Add save & continue editing button
  • Loading branch information
Flo0807 authored Dec 4, 2024
2 parents 26d5633 + cf73cd0 commit 281d672
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -236,4 +236,7 @@ msgstr ""
msgid "To"
msgstr ""
msgid "Save & Continue editing"
msgstr ""
```
8 changes: 8 additions & 0 deletions guides/upgrading/v0.9.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,11 @@ Make sure to read the improved documentation for the `handle` functions to under

- `c:Backpex.ItemAction.handle/3`
- `c:Backpex.ResourceAction.handle/2`

## Translate new texts

The latest version of Backpex introduces the following texts

- "Save & Continue Editing"

Make sure you translate these texts in your translation files.
14 changes: 13 additions & 1 deletion lib/backpex/html/resource/form_component.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,24 @@
</button>
<% else %>
<.link navigate={@live_resource.return_to(@socket, assigns, @live_action, @item)}>
<button type="button" class="btn btn-neutral btn-outline">
<button type="button" class="btn btn-ghost">
<%= Backpex.translate("Cancel") %>
</button>
</.link>
<button
:if={@continue_label}
type="submit"
name="save-type"
value="continue"
class={["btn", if(form_errors?(@show_form_errors, @form), do: "btn-error", else: "btn-neutral btn-outline")]}
phx-disable-with={@continue_label <> "..."}
>
<%= @continue_label %>
</button>
<button
type="submit"
name="save-type"
value="save"
class={["btn", if(form_errors?(@show_form_errors, @form), do: "btn-error", else: "btn-primary")]}
phx-disable-with={@save_label <> "..."}
>
Expand Down
34 changes: 27 additions & 7 deletions lib/backpex/live_components/form_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ defmodule Backpex.FormComponent do
socket
|> assign(assigns)
|> assign_new(:action_type, fn -> nil end)
|> assign_new(:continue_label, fn -> nil end)
|> assign_new(:show_form_errors, fn -> false end)
|> update_assigns()
|> assign_form()
Expand Down Expand Up @@ -53,6 +54,7 @@ defmodule Backpex.FormComponent do
defp apply_action(socket, action) when action in [:edit, :new] do
socket
|> assign(:save_label, Backpex.translate("Save"))
|> assign(:continue_label, Backpex.translate("Save & Continue editing"))
end

defp apply_action(socket, :resource_action) do
Expand Down Expand Up @@ -166,15 +168,15 @@ defmodule Backpex.FormComponent do
handle_item_action(socket, key, change)
end

def handle_event("save", %{"change" => change}, socket) do
def handle_event("save", %{"change" => change, "save-type" => save_type}, socket) do
%{assigns: %{live_action: live_action, fields: fields} = assigns} = socket

change =
change
|> put_upload_change(socket, :insert)
|> drop_readonly_changes(fields, assigns)

handle_save(socket, live_action, change)
handle_save(socket, live_action, change, save_type)
end

def handle_event("save", %{"action-key" => key}, socket) do
Expand All @@ -197,7 +199,9 @@ defmodule Backpex.FormComponent do
{:noreply, socket}
end

defp handle_save(socket, :new, params) do
defp handle_save(socket, key, params, save_type \\ "save")

defp handle_save(socket, :new, params, save_type) do
%{assigns: %{live_resource: live_resource, fields: fields, item: item} = assigns} = socket

opts = [
Expand All @@ -212,7 +216,7 @@ defmodule Backpex.FormComponent do

case Resource.insert(item, params, fields, socket.assigns, live_resource, opts) do
{:ok, item} ->
return_to = live_resource.return_to(socket, socket.assigns, :new, item)
return_to = return_to_path(save_type, live_resource, socket, socket.assigns, item, :new)

socket =
socket
Expand All @@ -237,7 +241,7 @@ defmodule Backpex.FormComponent do
end
end

defp handle_save(socket, :edit, params) do
defp handle_save(socket, :edit, params, save_type) do
%{live_resource: live_resource, singular_name: singular_name, item: item, fields: fields} = socket.assigns

opts = [
Expand All @@ -252,7 +256,7 @@ defmodule Backpex.FormComponent do

case Resource.update(item, params, fields, socket.assigns, live_resource, opts) do
{:ok, item} ->
return_to = live_resource.return_to(socket, socket.assigns, :edit, item)
return_to = return_to_path(save_type, live_resource, socket, socket.assigns, item, :edit)
info_msg = Backpex.translate({"%{resource} has been edited successfully.", %{resource: singular_name}})

socket =
Expand All @@ -278,7 +282,7 @@ defmodule Backpex.FormComponent do
end
end

defp handle_save(socket, :resource_action, params) do
defp handle_save(socket, :resource_action, params, _save_type) do
%{
assigns:
%{
Expand Down Expand Up @@ -394,6 +398,22 @@ defmodule Backpex.FormComponent do
Map.drop(change, read_only)
end

defp return_to_path("continue", _live_resource, _socket, %{current_url: url}, item, :new) do
url
|> URI.parse()
|> Map.get(:path)
|> Path.dirname()
|> Kernel.<>("/#{item.id}/edit")
end

defp return_to_path("continue", _live_resource, _socket, %{current_url: url}, _item, :edit) do
URI.parse(url).path
end

defp return_to_path(_save_type, live_resource, socket, assigns, item, type) do
live_resource.return_to(socket, assigns, type, item)
end

defp put_upload_change(change, socket, action) do
Enum.reduce(socket.assigns.fields, change, fn
{_name, %{upload_key: upload_key} = field_options} = _field, acc ->
Expand Down

0 comments on commit 281d672

Please sign in to comment.