cannery/lib/cannery_web/live/container_live/show.ex

101 lines
3.0 KiB
Elixir
Raw Normal View History

2021-09-02 23:31:14 -04:00
defmodule CanneryWeb.ContainerLive.Show do
2022-01-22 21:40:29 -05:00
@moduledoc """
Liveview for showing and editing a Cannery.Containers.Container
"""
2021-09-02 23:31:14 -04:00
use CanneryWeb, :live_view
2022-02-13 21:14:48 -05:00
import CanneryWeb.Components.{AmmoGroupCard, TagCard}
alias Cannery.{Containers, Repo, Tags}
alias CanneryWeb.Endpoint
2022-02-11 00:33:51 -05:00
alias Ecto.Changeset
2022-02-13 21:14:48 -05:00
alias Phoenix.LiveView.Socket
2021-09-02 23:31:14 -04:00
@impl true
2021-09-02 23:31:16 -04:00
def mount(_params, session, socket) do
{:ok, socket |> assign_defaults(session)}
2021-09-02 23:31:14 -04:00
end
@impl true
2022-02-11 00:33:51 -05:00
def handle_params(
%{"id" => id},
_,
%{assigns: %{current_user: current_user, live_action: live_action}} = socket
) do
2022-02-13 21:14:48 -05:00
{: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
2022-01-31 21:56:02 -05:00
socket =
2022-02-13 21:14:48 -05:00
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
2022-01-31 21:56:02 -05:00
{:noreply, socket}
end
@impl true
2022-02-11 00:33:51 -05:00
def handle_event(
2022-02-13 21:14:48 -05:00
"delete_container",
2022-02-11 00:33:51 -05:00
_,
%{assigns: %{container: container, current_user: current_user}} = socket
) do
2022-02-07 23:58:29 -05:00
socket =
2022-02-11 00:33:51 -05:00
Containers.delete_container(container, current_user)
2022-02-07 23:58:29 -05:00
|> case do
2022-02-11 00:33:51 -05:00
{:ok, %{name: container_name}} ->
prompt = dgettext("prompts", "%{name} has been deleted", name: container_name)
2022-02-07 23:58:29 -05:00
socket
2022-02-11 00:33:51 -05:00
|> put_flash(:info, prompt)
2022-02-07 23:58:29 -05:00
|> push_redirect(to: Routes.container_index_path(socket, :index))
{:error, %{action: :delete, errors: [ammo_groups: _error], valid?: false} = changeset} ->
ammo_groups_error = changeset |> changeset_errors(:ammo_groups) |> Enum.join(", ")
2022-02-09 00:49:47 -05:00
2022-02-11 00:33:51 -05:00
prompt =
dgettext("errors", "Could not delete %{name}: %{error}",
name: changeset |> Changeset.get_field(:name, "container"),
error: ammo_groups_error
)
socket |> put_flash(:error, prompt)
2022-02-07 23:58:29 -05:00
{:error, changeset} ->
socket |> put_flash(:error, changeset |> changeset_errors())
end
{:noreply, socket}
2021-09-02 23:31:14 -04:00
end
2022-02-09 00:49:47 -05:00
defp page_title(:show), do: gettext("Show Container")
defp page_title(:edit), do: gettext("Edit Container")
2022-02-13 21:14:48 -05:00
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
2021-09-02 23:31:14 -04:00
end