forked from shibao/cannery
improve containers
This commit is contained in:
@ -14,18 +14,25 @@ defmodule CanneryWeb.ContainerLive.FormComponent do
|
||||
Socket.t()
|
||||
) :: {:ok, Socket.t()}
|
||||
def update(%{container: container} = assigns, socket) do
|
||||
assigns = assigns |> Map.put(:changeset, container |> Containers.change_container())
|
||||
{:ok, socket |> assign(assigns)}
|
||||
{:ok, socket |> assign(assigns) |> assign(:changeset, Containers.change_container(container))}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event("validate", %{"container" => container_params}, socket) do
|
||||
changeset = socket.assigns.container |> Containers.change_container(container_params)
|
||||
def handle_event(
|
||||
"validate",
|
||||
%{"container" => container_params},
|
||||
%{assigns: %{container: container}} = socket
|
||||
) do
|
||||
changeset = container |> Containers.change_container(container_params)
|
||||
{:noreply, socket |> assign(:changeset, changeset)}
|
||||
end
|
||||
|
||||
def handle_event("save", %{"container" => container_params}, socket) do
|
||||
save_container(socket, socket.assigns.action, container_params)
|
||||
def handle_event(
|
||||
"save",
|
||||
%{"container" => container_params},
|
||||
%{assigns: %{action: action}} = socket
|
||||
) do
|
||||
save_container(socket, action, container_params)
|
||||
end
|
||||
|
||||
@impl true
|
||||
@ -89,36 +96,40 @@ defmodule CanneryWeb.ContainerLive.FormComponent do
|
||||
"""
|
||||
end
|
||||
|
||||
defp save_container(socket, :edit, container_params) do
|
||||
Containers.update_container(
|
||||
socket.assigns.container,
|
||||
socket.assigns.current_user,
|
||||
container_params
|
||||
)
|
||||
|> case do
|
||||
{:ok, _container} ->
|
||||
{:noreply,
|
||||
socket
|
||||
|> put_flash(:info, dgettext("prompts", "Container updated successfully"))
|
||||
|> push_redirect(to: socket.assigns.return_to)}
|
||||
defp save_container(
|
||||
%{assigns: %{container: container, current_user: current_user, return_to: return_to}} =
|
||||
socket,
|
||||
:edit,
|
||||
container_params
|
||||
) do
|
||||
socket =
|
||||
case Containers.update_container(container, current_user, container_params) do
|
||||
{:ok, %{name: container_name}} ->
|
||||
prompt = dgettext("prompts", "%{name} updated successfully", name: container_name)
|
||||
socket |> put_flash(:info, prompt) |> push_redirect(to: return_to)
|
||||
|
||||
{:error, %Changeset{} = changeset} ->
|
||||
{:noreply, socket |> assign(:changeset, changeset)}
|
||||
end
|
||||
{:error, %Changeset{} = changeset} ->
|
||||
socket |> assign(:changeset, changeset)
|
||||
end
|
||||
|
||||
{:noreply, socket}
|
||||
end
|
||||
|
||||
defp save_container(socket, :new, container_params) do
|
||||
container_params
|
||||
|> Containers.create_container(socket.assigns.current_user)
|
||||
|> case do
|
||||
{:ok, _container} ->
|
||||
{:noreply,
|
||||
socket
|
||||
|> put_flash(:info, dgettext("prompts", "Container created successfully"))
|
||||
|> push_redirect(to: socket.assigns.return_to)}
|
||||
defp save_container(
|
||||
%{assigns: %{current_user: current_user, return_to: return_to}} = socket,
|
||||
:new,
|
||||
container_params
|
||||
) do
|
||||
socket =
|
||||
case Containers.create_container(container_params, current_user) do
|
||||
{:ok, %{name: container_name}} ->
|
||||
prompt = dgettext("prompts", "%{name} created successfully", name: container_name)
|
||||
socket |> put_flash(:info, prompt) |> push_redirect(to: return_to)
|
||||
|
||||
{:error, %Changeset{} = changeset} ->
|
||||
{:noreply, socket |> assign(changeset: changeset)}
|
||||
end
|
||||
{:error, %Changeset{} = changeset} ->
|
||||
socket |> assign(changeset: changeset)
|
||||
end
|
||||
|
||||
{:noreply, socket}
|
||||
end
|
||||
end
|
||||
|
@ -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
|
||||
|
@ -6,6 +6,7 @@ defmodule CanneryWeb.ContainerLive.Show do
|
||||
use CanneryWeb, :live_view
|
||||
import CanneryWeb.AmmoGroupLive.AmmoGroupCard
|
||||
alias Cannery.{Containers, Repo}
|
||||
alias Ecto.Changeset
|
||||
|
||||
@impl true
|
||||
def mount(_params, session, socket) do
|
||||
@ -13,39 +14,47 @@ defmodule CanneryWeb.ContainerLive.Show do
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_params(%{"id" => id}, _, socket) do
|
||||
def handle_params(
|
||||
%{"id" => id},
|
||||
_,
|
||||
%{assigns: %{current_user: current_user, live_action: live_action}} = socket
|
||||
) do
|
||||
socket =
|
||||
socket
|
||||
|> assign(
|
||||
page_title: page_title(socket.assigns.live_action),
|
||||
container: Containers.get_container!(id) |> Repo.preload(:ammo_groups)
|
||||
page_title: page_title(live_action),
|
||||
container: Containers.get_container!(id, current_user) |> Repo.preload(:ammo_groups)
|
||||
)
|
||||
|
||||
{:noreply, socket}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event("delete", _, socket) do
|
||||
def handle_event(
|
||||
"delete",
|
||||
_,
|
||||
%{assigns: %{container: container, current_user: current_user}} = socket
|
||||
) do
|
||||
socket =
|
||||
socket.assigns.container
|
||||
|> Containers.delete_container(socket.assigns.current_user)
|
||||
Containers.delete_container(container, current_user)
|
||||
|> case do
|
||||
{:ok, container} ->
|
||||
{:ok, %{name: container_name}} ->
|
||||
prompt = dgettext("prompts", "%{name} has been deleted", name: container_name)
|
||||
|
||||
socket
|
||||
|> put_flash(
|
||||
:info,
|
||||
dgettext("prompts", "%{name} has been deleted", name: container.name)
|
||||
)
|
||||
|> put_flash(:info, prompt)
|
||||
|> 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,
|
||||
dgettext("errors", "Could not delete container: %{error}", error: ammo_groups_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())
|
||||
|
Reference in New Issue
Block a user