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

60 lines
1.7 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-05 01:59:40 -05:00
import CanneryWeb.AmmoGroupLive.AmmoGroupCard
alias Cannery.{Containers, Repo}
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
def handle_params(%{"id" => id}, _, socket) do
2022-01-31 21:56:02 -05:00
socket =
socket
|> assign(
page_title: page_title(socket.assigns.live_action),
2022-02-05 01:59:40 -05:00
container: Containers.get_container!(id) |> Repo.preload(:ammo_groups)
2022-01-31 21:56:02 -05:00
)
{:noreply, socket}
end
@impl true
def handle_event("delete", _, socket) do
2022-02-07 23:58:29 -05:00
socket =
socket.assigns.container
2022-02-08 22:10:48 -05:00
|> Containers.delete_container(socket.assigns.current_user)
2022-02-07 23:58:29 -05:00
|> case do
{:ok, container} ->
socket
2022-02-09 00:49:47 -05:00
|> put_flash(
:info,
dgettext("prompts", "%{name} has been deleted", name: container.name)
)
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
socket
|> put_flash(
:error,
dgettext("errors", "Could not delete container: %{error}", error: ammo_groups_error)
)
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")
2021-09-02 23:31:14 -04:00
end