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 duplicate action #92

Merged
Merged
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
2 changes: 1 addition & 1 deletion demo/lib/demo/tag.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ defmodule Demo.Tag do
timestamps()
end

@required_fields ~w[name inserted_at]a
@required_fields ~w[name]a

def update_changeset(category, attrs, _metadata \\ []) do
category
Expand Down
72 changes: 72 additions & 0 deletions demo/lib/demo_web/item_actions/duplicate_tag.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
defmodule DemoWeb.ItemActions.DuplicateTag do
@moduledoc false

use BackpexWeb, :item_action

alias Demo.Repo

@impl Backpex.ItemAction
def icon(assigns) do
~H"""
<Heroicons.document_duplicate class="h-5 w-5 cursor-pointer transition duration-75 hover:scale-110 hover:text-green-600" />
"""
end

@impl Backpex.ItemAction
def fields do
[
name: %{
module: Backpex.Fields.Text,
label: "Name",
searchable: true,
placeholder: "Tag name"
}
]

# DemoWeb.TagLive.fields()
end

@impl Backpex.ItemAction
def label(_assigns), do: "Duplicate"

@impl Backpex.ItemAction
def confirm(_assigns), do: "Please complete the form to duplicate the item."

@impl Backpex.ItemAction
def confirm_label(assigns), do: "Duplicate"

@impl Backpex.ItemAction
def cancel_label(assigns), do: "Cancel"

@impl Backpex.ItemAction
def changeset(item, change, target, assigns) do
Demo.Tag.create_changeset(item, change)
end

@impl Backpex.ItemAction
def init_change(assigns) do
[item | _other] = assigns.selected_items

item
end

@impl Backpex.ItemAction
def handle(socket, [item | _items] = items, params) do
result =
%Demo.Tag{}
|> Demo.Tag.create_changeset(params, [target: nil, assigns: socket.assigns])
|> Repo.insert()

socket =
case result do
{:ok, created} ->
put_flash(socket, :info, "Item has been duplicated.")

_error ->
put_flash(socket, :error, "Error while duplicating item.")
end


{:noreply, socket}
end
end
16 changes: 15 additions & 1 deletion demo/lib/demo_web/live/tag_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,22 @@ defmodule DemoWeb.TagLive do
},
inserted_at: %{
module: Backpex.Fields.DateTime,
label: "Date"
label: "Inserted At",
only: [:show, :index]
}
]
end

@impl Backpex.LiveResource
def item_actions(default_actions) do
Enum.concat(
[
duplicate: %{
module: DemoWeb.ItemActions.DuplicateTag,
only: [:row]
}
],
default_actions
)
end
end
14 changes: 7 additions & 7 deletions lib/backpex/item_actions/item_action.ex
Original file line number Diff line number Diff line change
Expand Up @@ -197,13 +197,6 @@ defmodule Backpex.ItemAction do
quote do
@before_compile Backpex.ItemAction
@behaviour Backpex.ItemAction

@impl Backpex.ItemAction
def init_change(_assigns) do
types = Backpex.Field.changeset_types(fields())

{%{}, types}
end
end
end

Expand All @@ -225,6 +218,13 @@ defmodule Backpex.ItemAction do
init_change(assigns)
|> Ecto.Changeset.change()
end

@impl Backpex.ItemAction
def init_change(_assigns) do
types = Backpex.Field.changeset_types(fields())

{%{}, types}
end
end
end
end
Loading