2022-02-15 18:20:12 -05:00
|
|
|
defmodule CanneryWeb.Components.MoveAmmoGroupComponent do
|
|
|
|
@moduledoc """
|
|
|
|
Livecomponent that can move an ammo group to another container
|
|
|
|
"""
|
|
|
|
|
|
|
|
use CanneryWeb, :live_component
|
2022-03-04 20:53:18 -05:00
|
|
|
alias Cannery.{Accounts.User, Ammo, Ammo.AmmoGroup, Containers, Containers.Container}
|
2022-03-04 00:06:08 -05:00
|
|
|
alias CanneryWeb.Endpoint
|
2022-02-15 18:20:12 -05:00
|
|
|
alias Phoenix.LiveView.Socket
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
@spec update(
|
|
|
|
%{
|
|
|
|
required(:current_user) => User.t(),
|
|
|
|
required(:ammo_group) => AmmoGroup.t(),
|
|
|
|
optional(any()) => any()
|
|
|
|
},
|
|
|
|
Socket.t()
|
|
|
|
) :: {:ok, Socket.t()}
|
|
|
|
def update(
|
|
|
|
%{ammo_group: %{container_id: container_id} = ammo_group, current_user: current_user} =
|
|
|
|
assigns,
|
|
|
|
socket
|
|
|
|
) do
|
2022-11-10 21:45:50 -05:00
|
|
|
changeset = ammo_group |> AmmoGroup.update_changeset(%{}, current_user)
|
2022-02-15 18:20:12 -05:00
|
|
|
|
|
|
|
containers =
|
|
|
|
Containers.list_containers(current_user)
|
|
|
|
|> Enum.reject(fn %{id: id} -> id == container_id end)
|
|
|
|
|
2022-03-04 20:53:18 -05:00
|
|
|
socket =
|
|
|
|
socket
|
|
|
|
|> assign(assigns)
|
|
|
|
|> assign(changeset: changeset, containers: containers)
|
|
|
|
|
|
|
|
{:ok, socket}
|
2022-02-15 18:20:12 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def handle_event(
|
|
|
|
"move",
|
|
|
|
%{"container_id" => container_id},
|
|
|
|
%{assigns: %{ammo_group: ammo_group, current_user: current_user, return_to: return_to}} =
|
|
|
|
socket
|
|
|
|
) do
|
|
|
|
%{name: container_name} = Containers.get_container!(container_id, current_user)
|
|
|
|
|
|
|
|
socket =
|
|
|
|
ammo_group
|
|
|
|
|> Ammo.update_ammo_group(%{"container_id" => container_id}, current_user)
|
|
|
|
|> case do
|
|
|
|
{:ok, _ammo_group} ->
|
|
|
|
prompt = dgettext("prompts", "Ammo moved to %{name} successfully", name: container_name)
|
|
|
|
|
2022-11-07 22:36:38 -05:00
|
|
|
socket |> put_flash(:info, prompt) |> push_navigate(to: return_to)
|
2022-02-15 18:20:12 -05:00
|
|
|
|
|
|
|
{:error, %Ecto.Changeset{} = changeset} ->
|
|
|
|
socket |> assign(changeset: changeset)
|
|
|
|
end
|
|
|
|
|
|
|
|
{:noreply, socket}
|
|
|
|
end
|
2022-03-04 20:53:18 -05:00
|
|
|
|
|
|
|
@impl true
|
|
|
|
def render(%{containers: containers} = assigns) do
|
|
|
|
columns = [
|
|
|
|
%{label: gettext("Container"), key: "name"},
|
|
|
|
%{label: gettext("Type"), key: "type"},
|
|
|
|
%{label: gettext("Location"), key: "location"},
|
2022-03-04 21:56:48 -05:00
|
|
|
%{label: nil, key: "actions", sortable: false}
|
2022-03-04 20:53:18 -05:00
|
|
|
]
|
|
|
|
|
|
|
|
rows = containers |> get_rows_for_containers(assigns, columns)
|
|
|
|
|
|
|
|
assigns = assigns |> Map.merge(%{columns: columns, rows: rows})
|
|
|
|
|
|
|
|
~H"""
|
|
|
|
<div class="w-full flex flex-col space-y-8 justify-center items-center">
|
|
|
|
<h2 class="mb-8 text-center title text-xl text-primary-600">
|
2022-11-09 18:45:28 -05:00
|
|
|
<%= dgettext("actions", "Move ammo") %>
|
2022-03-04 20:53:18 -05:00
|
|
|
</h2>
|
|
|
|
|
|
|
|
<%= if @containers |> Enum.empty?() do %>
|
|
|
|
<h2 class="title text-xl text-primary-600">
|
|
|
|
<%= gettext("No other containers") %>
|
|
|
|
<%= display_emoji("😔") %>
|
|
|
|
</h2>
|
|
|
|
|
2022-11-09 23:47:11 -05:00
|
|
|
<.link navigate={Routes.container_index_path(Endpoint, :new)} class="btn btn-primary">
|
2022-11-07 22:36:38 -05:00
|
|
|
<%= dgettext("actions", "Add another container!") %>
|
|
|
|
</.link>
|
2022-03-04 20:53:18 -05:00
|
|
|
<% else %>
|
|
|
|
<.live_component
|
|
|
|
module={CanneryWeb.Components.TableComponent}
|
|
|
|
id="move_ammo_group_table"
|
|
|
|
columns={@columns}
|
|
|
|
rows={@rows}
|
|
|
|
/>
|
|
|
|
<% end %>
|
|
|
|
</div>
|
|
|
|
"""
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec get_rows_for_containers([Container.t()], map(), [map()]) :: [map()]
|
|
|
|
defp get_rows_for_containers(containers, assigns, columns) do
|
|
|
|
containers
|
|
|
|
|> Enum.map(fn container ->
|
|
|
|
columns
|
2022-11-12 13:52:24 -05:00
|
|
|
|> Map.new(fn %{key: key} -> {key, get_row_value_by_key(key, container, assigns)} end)
|
2022-03-04 20:53:18 -05:00
|
|
|
end)
|
|
|
|
end
|
2022-03-04 22:27:09 -05:00
|
|
|
|
|
|
|
@spec get_row_value_by_key(String.t(), Container.t(), map()) :: any()
|
|
|
|
defp get_row_value_by_key("actions", container, assigns) do
|
|
|
|
assigns = assigns |> Map.put(:container, container)
|
|
|
|
|
|
|
|
~H"""
|
|
|
|
<div class="px-4 py-2 space-x-4 flex justify-center items-center">
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
class="btn btn-primary"
|
|
|
|
phx-click="move"
|
|
|
|
phx-target={@myself}
|
2022-11-07 22:36:38 -05:00
|
|
|
phx-value-container_id={@container.id}
|
2022-03-04 22:27:09 -05:00
|
|
|
>
|
|
|
|
<%= dgettext("actions", "Select") %>
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
"""
|
|
|
|
end
|
|
|
|
|
|
|
|
defp get_row_value_by_key(key, container, _assigns),
|
|
|
|
do: container |> Map.get(key |> String.to_existing_atom())
|
2022-02-15 18:20:12 -05:00
|
|
|
end
|