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

129 lines
4.3 KiB
Elixir
Raw Normal View History

2021-09-02 23:31:14 -04:00
defmodule CanneryWeb.ContainerLive.Index do
2022-01-22 21:40:29 -05:00
@moduledoc """
Liveview for showing Cannery.Containers.Container index
"""
2021-09-02 23:31:14 -04:00
use CanneryWeb, :live_view
2022-02-18 22:56:46 -05:00
alias Cannery.{Containers, Containers.Container, Repo}
2022-02-11 00:33:51 -05:00
alias Ecto.Changeset
2021-09-02 23:31:14 -04:00
@impl true
2022-12-03 20:07:51 -05:00
def mount(%{"search" => search}, _session, socket) do
{:ok, socket |> assign(view_table: true, search: search) |> display_containers()}
end
def mount(_params, _session, socket) do
{:ok, socket |> assign(view_table: true, search: nil) |> display_containers()}
end
2021-09-02 23:31:14 -04:00
@impl true
2022-02-11 00:33:51 -05:00
def handle_params(params, _url, %{assigns: %{live_action: live_action}} = socket) do
{:noreply, apply_action(socket, live_action, params) |> display_containers()}
2021-09-02 23:31:14 -04:00
end
2022-02-11 00:33:51 -05:00
defp apply_action(%{assigns: %{current_user: current_user}} = socket, :edit, %{"id" => id}) do
2022-02-18 22:56:46 -05:00
%{name: container_name} =
container =
Containers.get_container!(id, current_user)
2022-12-03 20:07:51 -05:00
|> Repo.preload([:tags, :ammo_groups])
2022-02-18 22:56:46 -05:00
2021-09-02 23:31:14 -04:00
socket
2022-02-18 22:56:46 -05:00
|> assign(page_title: gettext("Edit %{name}", name: container_name), container: container)
2021-09-02 23:31:14 -04:00
end
defp apply_action(socket, :new, _params) do
2022-02-11 00:33:51 -05:00
socket |> assign(:page_title, gettext("New Container")) |> assign(:container, %Container{})
2021-09-02 23:31:14 -04:00
end
2022-11-10 19:02:35 -05:00
defp apply_action(%{assigns: %{current_user: current_user}} = socket, :clone, %{"id" => id}) do
container = Containers.get_container!(id, current_user)
socket
|> assign(page_title: gettext("New Container"), container: %{container | id: nil})
end
2021-09-02 23:31:14 -04:00
defp apply_action(socket, :index, _params) do
2022-02-18 22:56:46 -05:00
socket
2022-11-10 18:51:24 -05:00
|> assign(
page_title: gettext("Containers"),
2022-12-03 20:07:51 -05:00
container: nil,
search: nil
2022-11-10 18:51:24 -05:00
)
end
2022-12-03 20:07:51 -05:00
defp apply_action(socket, :search, %{"search" => search}) do
2022-11-10 18:51:24 -05:00
socket
|> assign(
page_title: gettext("Containers"),
container: nil,
2022-12-03 20:07:51 -05:00
search: search
2022-11-10 18:51:24 -05:00
)
2022-02-18 22:56:46 -05:00
end
defp apply_action(%{assigns: %{current_user: current_user}} = socket, :edit_tags, %{"id" => id}) do
%{name: container_name} =
container =
Containers.get_container!(id, current_user) |> Repo.preload([:tags, :ammo_groups])
page_title = gettext("Edit %{name} tags", name: container_name)
socket |> assign(page_title: page_title, container: container)
2021-09-02 23:31:14 -04:00
end
@impl true
2022-02-11 00:33:51 -05:00
def handle_event("delete", %{"id" => id}, %{assigns: %{current_user: current_user}} = socket) do
2022-02-07 23:58:29 -05:00
socket =
socket.assigns.containers
|> Enum.find(fn %{id: container_id} -> id == container_id end)
|> case do
nil ->
2022-02-09 00:49:47 -05:00
socket |> put_flash(:error, dgettext("errors", "Could not find that container"))
2022-02-07 23:58:29 -05:00
container ->
2022-02-11 00:33:51 -05:00
case Containers.delete_container(container, current_user) do
{:ok, %{name: container_name}} ->
prompt = dgettext("prompts", "%{name} has been deleted", name: container_name)
socket |> put_flash(:info, prompt) |> display_containers()
2022-02-07 23:58:29 -05:00
{: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"),
2022-02-09 00:49:47 -05:00
error: ammo_groups_error
)
2022-02-11 00:33:51 -05:00
socket |> put_flash(:error, prompt)
2022-02-07 23:58:29 -05:00
{:error, changeset} ->
socket |> put_flash(:error, changeset |> changeset_errors())
end
end
{:noreply, socket}
2021-09-02 23:31:14 -04:00
end
2022-11-10 18:51:24 -05:00
def handle_event("toggle_table", _params, %{assigns: %{view_table: view_table}} = socket) do
2022-12-03 20:07:51 -05:00
{:noreply, socket |> assign(:view_table, !view_table) |> display_containers()}
end
2022-11-10 18:51:24 -05:00
2022-12-03 20:07:51 -05:00
def handle_event("search", %{"search" => %{"search_term" => ""}}, socket) do
{:noreply, socket |> push_patch(to: Routes.container_index_path(Endpoint, :index))}
end
def handle_event("search", %{"search" => %{"search_term" => search_term}}, socket) do
{:noreply,
socket |> push_patch(to: Routes.container_index_path(Endpoint, :search, search_term))}
2022-11-10 18:51:24 -05:00
end
2022-12-03 20:07:51 -05:00
defp display_containers(%{assigns: %{search: search, current_user: current_user}} = socket) do
2022-02-18 22:56:46 -05:00
containers =
2022-12-03 20:20:11 -05:00
Containers.list_containers(search, current_user)
|> Repo.preload([:tags, :ammo_groups])
2022-11-10 19:02:35 -05:00
2022-12-03 20:20:11 -05:00
socket |> assign(:containers, containers)
2022-11-10 18:51:24 -05:00
end
2021-09-02 23:31:14 -04:00
end