Better rule deletion UI
This commit is contained in:
parent
dbbae6a1a6
commit
72e3230504
|
@ -7,7 +7,6 @@ defmodule Frenzy.Rule do
|
|||
field :property, :string
|
||||
field :param, :string
|
||||
field :weight, :integer
|
||||
field :delete, :boolean, virtual: true
|
||||
|
||||
belongs_to :filter, Frenzy.Filter
|
||||
|
||||
|
@ -17,16 +16,7 @@ defmodule Frenzy.Rule do
|
|||
@doc false
|
||||
def changeset(rule, attrs) do
|
||||
rule
|
||||
|> cast(attrs, [:mode, :property, :param, :weight, :delete])
|
||||
|> set_delete_action()
|
||||
|> cast(attrs, [:mode, :property, :param, :weight])
|
||||
|> validate_required([:mode, :property, :param, :weight])
|
||||
end
|
||||
|
||||
def set_delete_action(changeset) do
|
||||
if get_change(changeset, :delete) do
|
||||
%{changeset | action: :delete}
|
||||
else
|
||||
changeset
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -33,4 +33,10 @@ defmodule FrenzyWeb.FilterController do
|
|||
|
||||
redirect(conn, to: Routes.filter_path(Endpoint, :edit, id))
|
||||
end
|
||||
|
||||
def remove_rule(conn, %{"id" => id, "rule_id" => rule_id}) do
|
||||
rule = Repo.get(Rule, rule_id)
|
||||
Repo.delete(rule)
|
||||
redirect(conn, to: Routes.filter_path(Endpoint, :edit, id))
|
||||
end
|
||||
end
|
||||
|
|
|
@ -27,6 +27,7 @@ defmodule FrenzyWeb.Router do
|
|||
|
||||
resources "/filters", FilterController, only: [:edit, :update]
|
||||
post "/filters/:id/add_rule", FilterController, :add_rule
|
||||
post "/filters/:id/remove_rule", FilterController, :remove_rule
|
||||
|
||||
resources "/items", ItemController, only: [:show]
|
||||
post "/items/:id/read", ItemController, :read
|
||||
|
|
|
@ -15,7 +15,6 @@
|
|||
<th>Mode</th>
|
||||
<th>Param</th>
|
||||
<th>Weight</th>
|
||||
<th>Delete?</th>
|
||||
</tr>
|
||||
<%= inputs_for form, :rules, fn p -> %>
|
||||
<tr>
|
||||
|
@ -32,7 +31,14 @@
|
|||
<%= number_input p, :weight %>
|
||||
</td>
|
||||
<td>
|
||||
<%= checkbox p, :delete %>
|
||||
<%# when nesting form tags, the first nested one seems to get removed %>
|
||||
<%# not the first nested one in each row, but the first nested one overall %>
|
||||
<%# so we include a dummy form with display: none that gets stripped from the first row %>
|
||||
<%# but is present and invisible in subsequent rows %>
|
||||
<form style="display: none;"></form>
|
||||
<%= form_tag Routes.filter_path(@conn, :remove_rule, @changeset.data.id, [rule_id: p.data.id]), method: :post do %>
|
||||
<%= submit "Delete" %>
|
||||
<% end %>
|
||||
</td>
|
||||
</tr>
|
||||
<% end %>
|
||||
|
|
Loading…
Reference in New Issue