Better rule deletion UI

This commit is contained in:
Shadowfacts 2019-03-14 22:32:20 -04:00
parent dbbae6a1a6
commit 72e3230504
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
4 changed files with 16 additions and 13 deletions

View File

@ -7,7 +7,6 @@ defmodule Frenzy.Rule do
field :property, :string field :property, :string
field :param, :string field :param, :string
field :weight, :integer field :weight, :integer
field :delete, :boolean, virtual: true
belongs_to :filter, Frenzy.Filter belongs_to :filter, Frenzy.Filter
@ -17,16 +16,7 @@ defmodule Frenzy.Rule do
@doc false @doc false
def changeset(rule, attrs) do def changeset(rule, attrs) do
rule rule
|> cast(attrs, [:mode, :property, :param, :weight, :delete]) |> cast(attrs, [:mode, :property, :param, :weight])
|> set_delete_action()
|> validate_required([:mode, :property, :param, :weight]) |> validate_required([:mode, :property, :param, :weight])
end end
def set_delete_action(changeset) do
if get_change(changeset, :delete) do
%{changeset | action: :delete}
else
changeset
end
end
end end

View File

@ -33,4 +33,10 @@ defmodule FrenzyWeb.FilterController do
redirect(conn, to: Routes.filter_path(Endpoint, :edit, id)) redirect(conn, to: Routes.filter_path(Endpoint, :edit, id))
end 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 end

View File

@ -27,6 +27,7 @@ defmodule FrenzyWeb.Router do
resources "/filters", FilterController, only: [:edit, :update] resources "/filters", FilterController, only: [:edit, :update]
post "/filters/:id/add_rule", FilterController, :add_rule post "/filters/:id/add_rule", FilterController, :add_rule
post "/filters/:id/remove_rule", FilterController, :remove_rule
resources "/items", ItemController, only: [:show] resources "/items", ItemController, only: [:show]
post "/items/:id/read", ItemController, :read post "/items/:id/read", ItemController, :read

View File

@ -15,7 +15,6 @@
<th>Mode</th> <th>Mode</th>
<th>Param</th> <th>Param</th>
<th>Weight</th> <th>Weight</th>
<th>Delete?</th>
</tr> </tr>
<%= inputs_for form, :rules, fn p -> %> <%= inputs_for form, :rules, fn p -> %>
<tr> <tr>
@ -32,7 +31,14 @@
<%= number_input p, :weight %> <%= number_input p, :weight %>
</td> </td>
<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> </td>
</tr> </tr>
<% end %> <% end %>