Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add init_params/1 callback for item actions #673

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/backpex/item_actions/item_action.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ defmodule Backpex.ItemAction do
| Ecto.Changeset.t()
| {Ecto.Changeset.data(), Ecto.Changeset.types()}

@doc """
The initial params for the changeset. The result is passed as the second parameter to `c:changeset/3` the first time it is called.

This function is optional and can be used to set an initial change, e.g. to pre-populate the form with values.
If this function is not provided, an empty map will be used.
"""
@callback init_params(assigns :: map()) :: map()

@doc """
The changeset to be used in the resource action. It may be used to validate form inputs.

Expand Down Expand Up @@ -117,6 +125,9 @@ defmodule Backpex.ItemAction do

{%{}, types}
end

@impl Backpex.ItemAction
def init_params(_assigns), do: %{}
end
end
end
3 changes: 2 additions & 1 deletion lib/backpex/live_resource.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1174,12 +1174,13 @@ defmodule Backpex.LiveResource do

defp open_action_confirm_modal(socket, action, key) do
base_item = action.module.base_item(socket.assigns)
init_params = action.module.init_params(socket.assigns)

changeset_function = &action.module.changeset/3

metadata = Resource.build_changeset_metadata(socket.assigns)

changeset = changeset_function.(base_item, %{}, metadata)
changeset = changeset_function.(base_item, init_params, metadata)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pehbehbeh We should combine this with the default values users can define for fields. Currently, we do not use these values in Item Actions (we do in Resource Actions: see https://github.com/naymspace/backpex/blob/develop/lib/backpex/live_resource.ex#L794)

What are your thoughts on using the init_params if defined and otherwise the default values (same for resource actions)?

Something like this:

init_params = action.module.init_params(socket.assigns) || build_default_params

If both init_params and "default" field values are not present an empty map is being used.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds reasonable!


socket =
socket
Expand Down