cannery/lib/cannery_web/live/pack_live/form_component.ex

188 lines
5.2 KiB
Elixir
Raw Normal View History

2023-03-29 22:54:55 -04:00
defmodule CanneryWeb.PackLive.FormComponent do
2022-01-22 21:40:29 -05:00
@moduledoc """
2023-03-29 22:54:55 -04:00
Livecomponent that can update or create an Cannery.Ammo.Pack
2022-01-22 21:40:29 -05:00
"""
2021-09-02 23:31:14 -04:00
use CanneryWeb, :live_component
2023-03-30 21:53:52 -04:00
alias Cannery.Ammo.{Pack, Type}
2022-02-11 00:34:29 -05:00
alias Cannery.{Accounts.User, Ammo, Containers, Containers.Container}
2022-02-05 01:59:40 -05:00
alias Ecto.Changeset
2022-02-10 23:36:31 -05:00
alias Phoenix.LiveView.Socket
2021-09-02 23:31:14 -04:00
@impl true
@spec mount(Socket.t()) :: {:ok, Socket.t()}
def mount(socket) do
{:ok, socket |> assign(:class, :all)}
end
2022-02-24 00:16:23 -05:00
2021-09-02 23:31:14 -04:00
@impl true
2022-02-10 23:36:31 -05:00
@spec update(
2023-03-29 22:54:55 -04:00
%{:pack => Pack.t(), :current_user => User.t(), optional(any) => any},
2022-02-10 23:36:31 -05:00
Socket.t()
) :: {:ok, Socket.t()}
2023-03-29 22:54:55 -04:00
def update(%{pack: _pack} = assigns, socket) do
socket |> assign(assigns) |> update()
end
2022-02-05 01:59:40 -05:00
2022-02-10 23:36:31 -05:00
@spec update(Socket.t()) :: {:ok, Socket.t()}
2022-07-04 20:39:21 -04:00
def update(%{assigns: %{current_user: current_user}} = socket) do
2024-07-28 13:17:33 -04:00
socket =
2022-02-23 19:51:07 -05:00
socket
2023-06-03 20:14:20 -04:00
|> assign(:types, Ammo.list_types(current_user))
2022-02-23 19:51:07 -05:00
|> assign_new(:containers, fn -> Containers.list_containers(current_user) end)
2024-07-28 13:17:33 -04:00
{:ok, socket |> assign_changeset(%{})}
2021-09-02 23:31:14 -04:00
end
@impl true
2023-03-29 22:54:55 -04:00
def handle_event("validate", %{"pack" => pack_params}, socket) do
matched_class =
case pack_params["class"] do
"rifle" -> :rifle
"shotgun" -> :shotgun
"pistol" -> :pistol
_other -> :all
end
socket =
socket
|> assign_changeset(pack_params, :validate)
|> assign(:class, matched_class)
{:noreply, socket}
2022-07-04 20:39:21 -04:00
end
def handle_event(
2022-07-04 20:39:21 -04:00
"save",
2023-03-29 22:54:55 -04:00
%{"pack" => pack_params},
2022-07-04 20:39:21 -04:00
%{assigns: %{action: action}} = socket
) do
2023-03-29 22:54:55 -04:00
save_pack(socket, action, pack_params)
2022-07-04 20:39:21 -04:00
end
# HTML Helpers
2022-07-04 20:39:21 -04:00
@spec container_options([Container.t()]) :: [{String.t(), Container.id()}]
defp container_options(containers) do
containers |> Enum.map(fn %{id: id, name: name} -> {name, id} end)
end
@spec type_options([Type.t()], Type.class() | :all) ::
[{String.t(), Type.id()}]
defp type_options(types, :all) do
2023-03-30 21:53:52 -04:00
types |> Enum.map(fn %{id: id, name: name} -> {name, id} end)
2022-07-04 20:39:21 -04:00
end
defp type_options(types, selected_class) do
types
|> Enum.filter(fn %{class: class} -> class == selected_class end)
|> Enum.map(fn %{id: id, name: name} -> {name, id} end)
end
2022-07-04 20:39:21 -04:00
# Save Helpers
defp assign_changeset(
2023-03-29 22:54:55 -04:00
%{assigns: %{action: action, pack: pack, current_user: user}} = socket,
pack_params,
changeset_action \\ nil
2022-07-04 20:39:21 -04:00
) do
default_action =
case action do
create when create in [:new, :clone] -> :insert
:edit -> :update
2022-03-04 23:16:37 -05:00
end
2022-07-04 20:06:41 -04:00
changeset =
case default_action do
:insert ->
2023-03-30 21:53:52 -04:00
type = maybe_get_type(pack_params, user)
2023-03-29 22:54:55 -04:00
container = maybe_get_container(pack_params, user)
2023-03-30 21:53:52 -04:00
pack |> Pack.create_changeset(type, container, user, pack_params)
2022-07-04 20:06:41 -04:00
:update ->
2023-03-29 22:54:55 -04:00
pack |> Pack.update_changeset(pack_params, user)
2022-07-04 20:06:41 -04:00
end
2022-03-04 23:16:37 -05:00
changeset =
2024-07-28 13:17:33 -04:00
if changeset_action do
case changeset |> Changeset.apply_action(changeset_action) do
{:ok, _data} -> changeset
{:error, changeset} -> changeset
end
else
changeset
2022-03-04 23:16:37 -05:00
end
2022-07-04 20:39:21 -04:00
socket |> assign(:changeset, changeset)
2021-09-02 23:31:14 -04:00
end
defp maybe_get_container(%{"container_id" => container_id}, user)
when is_binary(container_id) do
container_id |> Containers.get_container!(user)
end
defp maybe_get_container(_params_not_found, _user), do: nil
2023-03-30 21:53:52 -04:00
defp maybe_get_type(%{"type_id" => type_id}, user)
when is_binary(type_id) do
type_id |> Ammo.get_type!(user)
end
2023-03-30 21:53:52 -04:00
defp maybe_get_type(_params_not_found, _user), do: nil
2023-03-29 22:54:55 -04:00
defp save_pack(
%{assigns: %{pack: pack, current_user: current_user, return_to: return_to}} = socket,
:edit,
2023-03-29 22:54:55 -04:00
pack_params
) do
socket =
2023-03-29 22:54:55 -04:00
case Ammo.update_pack(pack, pack_params, current_user) do
{:ok, _pack} ->
2022-07-01 00:23:04 -04:00
prompt = dgettext("prompts", "Ammo updated successfully")
socket |> put_flash(:info, prompt) |> push_navigate(to: return_to)
{:error, %Changeset{} = changeset} ->
socket |> assign(:changeset, changeset)
end
{:noreply, socket}
2021-09-02 23:31:14 -04:00
end
2023-03-29 22:54:55 -04:00
defp save_pack(
%{assigns: %{changeset: changeset, current_user: current_user, return_to: return_to}} =
socket,
2022-11-10 18:21:29 -05:00
action,
2023-03-29 22:54:55 -04:00
%{"multiplier" => multiplier_str} = pack_params
2022-11-10 18:21:29 -05:00
)
when action in [:new, :clone] do
socket =
with {multiplier, _remainder} <- multiplier_str |> Integer.parse(),
{:ok, {count, _packs}} <- Ammo.create_packs(pack_params, multiplier, current_user) do
2022-02-24 00:16:23 -05:00
prompt =
dngettext(
"prompts",
2022-07-01 00:23:04 -04:00
"Ammo added successfully",
"Ammo added successfully",
2022-02-24 00:16:23 -05:00
count
)
socket |> put_flash(:info, prompt) |> push_navigate(to: return_to)
else
{:error, %Changeset{} = changeset} ->
socket |> assign(changeset: changeset)
2022-02-24 00:16:23 -05:00
:error ->
error_msg = dgettext("errors", "Could not parse number of copies")
{:error, changeset} =
changeset
|> Changeset.add_error(:multiplier, error_msg)
|> Changeset.apply_action(:insert)
socket |> assign(:changeset, changeset)
end
{:noreply, socket}
2022-02-24 00:16:23 -05:00
end
2021-09-02 23:31:14 -04:00
end