add and delete tags to/from containers
This commit is contained in:
@ -4,9 +4,11 @@ defmodule CanneryWeb.ContainerLive.Show do
|
||||
"""
|
||||
|
||||
use CanneryWeb, :live_view
|
||||
import CanneryWeb.Components.AmmoGroupCard
|
||||
alias Cannery.{Containers, Repo}
|
||||
import CanneryWeb.Components.{AmmoGroupCard, TagCard}
|
||||
alias Cannery.{Containers, Repo, Tags}
|
||||
alias CanneryWeb.Endpoint
|
||||
alias Ecto.Changeset
|
||||
alias Phoenix.LiveView.Socket
|
||||
|
||||
@impl true
|
||||
def mount(_params, session, socket) do
|
||||
@ -19,19 +21,39 @@ defmodule CanneryWeb.ContainerLive.Show do
|
||||
_,
|
||||
%{assigns: %{current_user: current_user, live_action: live_action}} = socket
|
||||
) do
|
||||
{:noreply,
|
||||
socket |> assign(page_title: page_title(live_action)) |> render_container(id, current_user)}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event(
|
||||
"delete_tag",
|
||||
%{"tag-id" => tag_id},
|
||||
%{assigns: %{container: container, current_user: current_user}} = socket
|
||||
) do
|
||||
socket =
|
||||
socket
|
||||
|> assign(
|
||||
page_title: page_title(live_action),
|
||||
container: Containers.get_container!(id, current_user) |> Repo.preload(:ammo_groups)
|
||||
)
|
||||
case Tags.get_tag(tag_id, current_user) do
|
||||
{:ok, tag} ->
|
||||
_count = Containers.remove_tag!(container, tag, current_user)
|
||||
|
||||
prompt =
|
||||
dgettext("prompts", "%{tag_name} has been removed from %{container_name}",
|
||||
tag_name: tag.name,
|
||||
container_name: container.name
|
||||
)
|
||||
|
||||
socket |> put_flash(:info, prompt) |> render_container(container.id, current_user)
|
||||
|
||||
{:error, error_string} ->
|
||||
socket |> put_flash(:error, error_string)
|
||||
end
|
||||
|
||||
{:noreply, socket}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event(
|
||||
"delete",
|
||||
"delete_container",
|
||||
_,
|
||||
%{assigns: %{container: container, current_user: current_user}} = socket
|
||||
) do
|
||||
@ -65,4 +87,14 @@ defmodule CanneryWeb.ContainerLive.Show do
|
||||
|
||||
defp page_title(:show), do: gettext("Show Container")
|
||||
defp page_title(:edit), do: gettext("Edit Container")
|
||||
defp page_title(:add_tag), do: gettext("Add Tag to Container")
|
||||
|
||||
@spec render_container(Socket.t(), Container.id(), User.t()) :: Socket.t()
|
||||
defp render_container(socket, id, current_user) do
|
||||
container =
|
||||
Containers.get_container!(id, current_user)
|
||||
|> Repo.preload([:ammo_groups, :tags], force: true)
|
||||
|
||||
socket |> assign(container: container)
|
||||
end
|
||||
end
|
||||
|
Reference in New Issue
Block a user