-
Notifications
You must be signed in to change notification settings - Fork 37
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
Support ash destroy action #666
base: develop
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,8 @@ defmodule Backpex.ItemAction do | |
@callback fields() :: list() | ||
|
||
@doc """ | ||
TODO: rename to `init_schema` | ||
Flo0807 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Initial change. The result will be passed to `c:changeset/3` in order to generate a changeset. | ||
|
||
This function is optional and can be used to use changesets with schemas in item actions. If this function | ||
|
@@ -111,10 +113,28 @@ defmodule Backpex.ItemAction do | |
|
||
@impl Backpex.ItemAction | ||
def init_change(_assigns) do | ||
types = Backpex.Field.changeset_types(fields()) | ||
types = fields() |> Backpex.Field.changeset_types() | ||
|
||
{%{}, types} | ||
end | ||
end | ||
end | ||
|
||
@doc """ | ||
Checks whether item action has confirmation modal. | ||
""" | ||
def has_confirm_modal?(item_action) do | ||
module = Map.fetch!(item_action, :module) | ||
|
||
function_exported?(module, :confirm, 1) | ||
end | ||
|
||
@doc """ | ||
Checks whether item action has form. | ||
""" | ||
def has_form?(item_action) do | ||
module = Map.fetch!(item_action, :module) | ||
|
||
module.fields() != [] | ||
end | ||
Comment on lines
+123
to
+139
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thought about moving this functions in the item action use block so we can call |
||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,7 +72,9 @@ defmodule Backpex.FormComponent do | |
end | ||
|
||
def handle_event("validate", %{"change" => change, "_target" => target}, %{assigns: %{action_type: :item}} = socket) do | ||
%{assigns: %{item_action_types: item_action_types, live_resource: live_resource, fields: fields} = assigns} = socket | ||
%{assigns: %{init_schema: init_schema, fields: fields} = assigns} = socket | ||
|
||
changeset_function = &assigns.action_to_confirm.module.changeset/3 | ||
|
||
target = Enum.at(target, 1) | ||
|
||
|
@@ -81,9 +83,12 @@ defmodule Backpex.FormComponent do | |
|> drop_readonly_changes(fields, assigns) | ||
|> put_upload_change(socket, :validate) | ||
|
||
metadata = Resource.build_changeset_metadata(socket.assigns, target) | ||
|
||
changeset = | ||
item_action_types | ||
|> Resource.change(change, fields, assigns, live_resource, target: target) | ||
init_schema | ||
|> changeset_function.(change, metadata) | ||
|> Map.put(:action, :validate) | ||
Comment on lines
+88
to
+90
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Still not sure where to place the code that calls the changeset function and if we should move it to another module 🤔 I also did not include special calls like |
||
|
||
form = Phoenix.Component.to_form(changeset, as: :change) | ||
|
||
|
@@ -332,19 +337,25 @@ defmodule Backpex.FormComponent do | |
%{ | ||
assigns: | ||
%{ | ||
live_resource: live_resource, | ||
selected_items: selected_items, | ||
action_to_confirm: action_to_confirm, | ||
return_to: return_to, | ||
item_action_types: item_action_types, | ||
fields: fields | ||
} = assigns | ||
} = socket | ||
|
||
result = | ||
item_action_types | ||
|> Backpex.Resource.change(params, fields, assigns, live_resource) | ||
|> Ecto.Changeset.apply_action(:insert) | ||
if Backpex.ItemAction.has_form?(action_to_confirm) do | ||
changeset_function = &action_to_confirm.module.changeset/3 | ||
|
||
metadata = Resource.build_changeset_metadata(assigns) | ||
|
||
assigns.init_schema | ||
|> changeset_function.(params, metadata) | ||
|> Map.put(:action, :insert) | ||
|> Ecto.Changeset.apply_action(:insert) | ||
else | ||
{:ok, %{}} | ||
end | ||
|
||
case result do | ||
{:ok, data} -> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How do we want to catch errors? Currently, the item action, e.g., Delete, uses
try rescue
and pattern matches on the error. I don't like this, as the item action itself should not figure out why the deletion failed. In the case of Ash, the bulk delete might contain errors (in the result struct), even if it did not raise an error. I suggest moving the error handling to the adapter. @pehbehbeh