improve containers

This commit is contained in:
2022-02-11 00:33:51 -05:00
parent 845ebc3bc6
commit 76d594ca0f
4 changed files with 130 additions and 128 deletions

View File

@ -6,6 +6,7 @@ defmodule CanneryWeb.ContainerLive.Index do
use CanneryWeb, :live_view
import CanneryWeb.ContainerLive.ContainerCard
alias Cannery.{Containers, Containers.Container}
alias Ecto.Changeset
@impl true
def mount(_params, session, socket) do
@ -13,30 +14,26 @@ defmodule CanneryWeb.ContainerLive.Index do
end
@impl true
def handle_params(params, _url, socket) do
{:noreply, apply_action(socket, socket.assigns.live_action, params)}
def handle_params(params, _url, %{assigns: %{live_action: live_action}} = socket) do
{:noreply, apply_action(socket, live_action, params)}
end
defp apply_action(socket, :edit, %{"id" => id}) do
defp apply_action(%{assigns: %{current_user: current_user}} = socket, :edit, %{"id" => id}) do
socket
|> assign(:page_title, gettext("Edit Container"))
|> assign(:container, Containers.get_container!(id))
|> assign(:container, Containers.get_container!(id, current_user))
end
defp apply_action(socket, :new, _params) do
socket
|> assign(:page_title, gettext("New Container"))
|> assign(:container, %Container{})
socket |> assign(:page_title, gettext("New Container")) |> assign(:container, %Container{})
end
defp apply_action(socket, :index, _params) do
socket
|> assign(:page_title, gettext("Listing Containers"))
|> assign(:container, nil)
socket |> assign(:page_title, gettext("Listing Containers")) |> assign(:container, nil)
end
@impl true
def handle_event("delete", %{"id" => id}, socket) do
def handle_event("delete", %{"id" => id}, %{assigns: %{current_user: current_user}} = socket) do
socket =
socket.assigns.containers
|> Enum.find(fn %{id: container_id} -> id == container_id end)
@ -45,27 +42,23 @@ defmodule CanneryWeb.ContainerLive.Index do
socket |> put_flash(:error, dgettext("errors", "Could not find that container"))
container ->
container
|> Containers.delete_container(socket.assigns.current_user)
|> case do
{:ok, container} ->
socket
|> put_flash(
:info,
dgettext("prompts", "%{name} has been deleted", name: container.name)
)
|> display_containers()
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()
{:error, %{action: :delete, errors: [ammo_groups: _error], valid?: false} = changeset} ->
ammo_groups_error = changeset |> changeset_errors(:ammo_groups) |> Enum.join(", ")
socket
|> put_flash(
:error,
dgettext("errors", "Could not delete container: %{error}",
prompt =
dgettext(
"errors",
"Could not delete %{name}: %{error}",
name: changeset |> Changeset.get_field(:name, "container"),
error: ammo_groups_error
)
)
socket |> put_flash(:error, prompt)
{:error, changeset} ->
socket |> put_flash(:error, changeset |> changeset_errors())
@ -76,7 +69,6 @@ defmodule CanneryWeb.ContainerLive.Index do
end
defp display_containers(%{assigns: %{current_user: current_user}} = socket) do
containers = Containers.list_containers(current_user)
socket |> assign(containers: containers)
socket |> assign(containers: Containers.list_containers(current_user))
end
end