add deletion check for containers
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
@ -5,8 +5,7 @@ defmodule CanneryWeb.ContainerLive.Index do
|
||||
|
||||
use CanneryWeb, :live_view
|
||||
import CanneryWeb.ContainerLive.ContainerCard
|
||||
alias Cannery.Containers
|
||||
alias Cannery.Containers.Container
|
||||
alias Cannery.{Containers, Containers.Container}
|
||||
|
||||
@impl true
|
||||
def mount(_params, session, socket) do
|
||||
@ -38,8 +37,32 @@ defmodule CanneryWeb.ContainerLive.Index do
|
||||
|
||||
@impl true
|
||||
def handle_event("delete", %{"id" => id}, socket) do
|
||||
Containers.get_container!(id) |> Containers.delete_container!()
|
||||
{:noreply, socket |> display_containers()}
|
||||
socket =
|
||||
socket.assigns.containers
|
||||
|> Enum.find(fn %{id: container_id} -> id == container_id end)
|
||||
|> case do
|
||||
nil ->
|
||||
socket |> put_flash(:error, "Could not find that container")
|
||||
|
||||
container ->
|
||||
container
|
||||
|> Containers.delete_container()
|
||||
|> case do
|
||||
{:ok, container} ->
|
||||
socket
|
||||
|> put_flash(:info, "#{container.name} has been deleted")
|
||||
|> 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, "Could not delete container: #{ammo_groups_error}")
|
||||
|
||||
{:error, changeset} ->
|
||||
socket |> put_flash(:error, changeset |> changeset_errors())
|
||||
end
|
||||
end
|
||||
|
||||
{:noreply, socket}
|
||||
end
|
||||
|
||||
defp display_containers(%{assigns: %{current_user: current_user}} = socket) do
|
||||
|
@ -26,8 +26,24 @@ defmodule CanneryWeb.ContainerLive.Show do
|
||||
|
||||
@impl true
|
||||
def handle_event("delete", _, socket) do
|
||||
socket.assigns.container |> Containers.delete_container!()
|
||||
{:noreply, socket |> push_redirect(to: Routes.container_index_path(socket, :index))}
|
||||
socket =
|
||||
socket.assigns.container
|
||||
|> Containers.delete_container()
|
||||
|> case do
|
||||
{:ok, container} ->
|
||||
socket
|
||||
|> put_flash(:info, "#{container.name} has been deleted")
|
||||
|> 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(", ")
|
||||
socket |> put_flash(:error, "Could not delete container: #{ammo_groups_error}")
|
||||
|
||||
{:error, changeset} ->
|
||||
socket |> put_flash(:error, changeset |> changeset_errors())
|
||||
end
|
||||
|
||||
{:noreply, socket}
|
||||
end
|
||||
|
||||
defp page_title(:show), do: "Show Container"
|
||||
|
Reference in New Issue
Block a user