2021-09-02 23:31:14 -04:00
|
|
|
defmodule CanneryWeb.AmmoGroupLive.FormComponent do
|
2022-01-22 21:40:29 -05:00
|
|
|
@moduledoc """
|
|
|
|
Livecomponent that can update or create an Cannery.Ammo.AmmoGroup
|
|
|
|
"""
|
|
|
|
|
2021-09-02 23:31:14 -04:00
|
|
|
use CanneryWeb, :live_component
|
2022-02-11 00:34:29 -05:00
|
|
|
alias Cannery.Ammo.{AmmoGroup, AmmoType}
|
|
|
|
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
|
|
|
|
2022-02-24 00:16:23 -05:00
|
|
|
@ammo_group_create_limit 10_000
|
|
|
|
|
2021-09-02 23:31:14 -04:00
|
|
|
@impl true
|
2022-02-10 23:36:31 -05:00
|
|
|
@spec update(
|
|
|
|
%{:ammo_group => AmmoGroup.t(), :current_user => User.t(), optional(any) => any},
|
|
|
|
Socket.t()
|
|
|
|
) :: {:ok, Socket.t()}
|
2022-02-10 00:57:56 -05:00
|
|
|
def update(%{ammo_group: _ammo_group} = 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
|
2022-07-04 21:41:07 -04:00
|
|
|
%{assigns: %{ammo_types: ammo_types, containers: containers}} =
|
|
|
|
socket =
|
2022-02-23 19:51:07 -05:00
|
|
|
socket
|
2022-02-24 00:16:23 -05:00
|
|
|
|> assign(:ammo_group_create_limit, @ammo_group_create_limit)
|
2022-02-23 19:51:07 -05:00
|
|
|
|> assign(:ammo_types, Ammo.list_ammo_types(current_user))
|
|
|
|
|> assign_new(:containers, fn -> Containers.list_containers(current_user) end)
|
|
|
|
|
2022-07-04 21:22:59 -04:00
|
|
|
params =
|
|
|
|
if ammo_types |> List.first() |> is_nil(),
|
|
|
|
do: %{},
|
|
|
|
else: %{} |> Map.put("ammo_type_id", ammo_types |> List.first() |> Map.get(:id))
|
|
|
|
|
|
|
|
params =
|
|
|
|
if containers |> List.first() |> is_nil(),
|
|
|
|
do: params,
|
|
|
|
else: params |> Map.put("container_id", containers |> List.first() |> Map.get(:id))
|
|
|
|
|
|
|
|
{:ok, socket |> assign_changeset(params)}
|
2021-09-02 23:31:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
2022-07-04 20:39:21 -04:00
|
|
|
def handle_event("validate", %{"ammo_group" => ammo_group_params}, socket) do
|
|
|
|
{:noreply, socket |> assign_changeset(ammo_group_params)}
|
|
|
|
end
|
|
|
|
|
2022-02-10 00:57:56 -05:00
|
|
|
def handle_event(
|
2022-07-04 20:39:21 -04:00
|
|
|
"save",
|
2022-02-10 00:57:56 -05:00
|
|
|
%{"ammo_group" => ammo_group_params},
|
2022-07-04 20:39:21 -04:00
|
|
|
%{assigns: %{action: action}} = socket
|
2022-02-10 00:57:56 -05:00
|
|
|
) do
|
2022-07-04 20:39:21 -04:00
|
|
|
save_ammo_group(socket, action, ammo_group_params)
|
|
|
|
end
|
|
|
|
|
|
|
|
# HTML Helpers
|
|
|
|
@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 ammo_type_options([AmmoType.t()]) :: [{String.t(), AmmoType.id()}]
|
|
|
|
defp ammo_type_options(ammo_types) do
|
|
|
|
ammo_types |> Enum.map(fn %{id: id, name: name} -> {name, id} end)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Save Helpers
|
|
|
|
|
|
|
|
defp assign_changeset(
|
|
|
|
%{assigns: %{action: action, ammo_group: ammo_group, current_user: user}} = socket,
|
|
|
|
ammo_group_params
|
|
|
|
) do
|
2022-03-04 23:16:37 -05:00
|
|
|
changeset_action =
|
|
|
|
case action do
|
|
|
|
:new -> :insert
|
|
|
|
:edit -> :update
|
|
|
|
end
|
|
|
|
|
2022-07-04 20:06:41 -04:00
|
|
|
changeset =
|
|
|
|
case action do
|
|
|
|
:new ->
|
|
|
|
ammo_type =
|
|
|
|
if ammo_group_params |> Map.has_key?("ammo_type_id"),
|
|
|
|
do: ammo_group_params |> Map.get("ammo_type_id") |> Ammo.get_ammo_type!(user),
|
|
|
|
else: nil
|
|
|
|
|
|
|
|
container =
|
|
|
|
if ammo_group_params |> Map.has_key?("container_id"),
|
|
|
|
do: ammo_group_params |> Map.get("container_id") |> Containers.get_container!(user),
|
|
|
|
else: nil
|
|
|
|
|
|
|
|
ammo_group |> AmmoGroup.create_changeset(ammo_type, container, user, ammo_group_params)
|
|
|
|
|
|
|
|
:edit ->
|
|
|
|
ammo_group |> AmmoGroup.update_changeset(ammo_group_params)
|
|
|
|
end
|
2022-03-04 23:16:37 -05:00
|
|
|
|
|
|
|
changeset =
|
|
|
|
case changeset |> Changeset.apply_action(changeset_action) do
|
|
|
|
{:ok, _data} -> changeset
|
|
|
|
{:error, changeset} -> changeset
|
|
|
|
end
|
|
|
|
|
2022-07-04 20:39:21 -04:00
|
|
|
socket |> assign(:changeset, changeset)
|
2021-09-02 23:31:14 -04:00
|
|
|
end
|
|
|
|
|
2022-02-10 00:57:56 -05:00
|
|
|
defp save_ammo_group(
|
|
|
|
%{assigns: %{ammo_group: ammo_group, current_user: current_user, return_to: return_to}} =
|
|
|
|
socket,
|
|
|
|
:edit,
|
|
|
|
ammo_group_params
|
|
|
|
) do
|
|
|
|
socket =
|
|
|
|
case Ammo.update_ammo_group(ammo_group, ammo_group_params, current_user) do
|
|
|
|
{:ok, _ammo_group} ->
|
2022-07-01 00:23:04 -04:00
|
|
|
prompt = dgettext("prompts", "Ammo updated successfully")
|
2022-02-10 00:57:56 -05:00
|
|
|
socket |> put_flash(:info, prompt) |> push_redirect(to: return_to)
|
|
|
|
|
|
|
|
{:error, %Changeset{} = changeset} ->
|
|
|
|
socket |> assign(:changeset, changeset)
|
|
|
|
end
|
|
|
|
|
|
|
|
{:noreply, socket}
|
2021-09-02 23:31:14 -04:00
|
|
|
end
|
|
|
|
|
2022-02-10 00:57:56 -05:00
|
|
|
defp save_ammo_group(
|
2022-02-24 00:16:23 -05:00
|
|
|
%{assigns: %{changeset: changeset}} = socket,
|
2022-02-10 00:57:56 -05:00
|
|
|
:new,
|
2022-02-24 00:16:23 -05:00
|
|
|
%{"multiplier" => multiplier_str} = ammo_group_params
|
2022-02-10 00:57:56 -05:00
|
|
|
) do
|
|
|
|
socket =
|
2022-02-24 00:16:23 -05:00
|
|
|
case multiplier_str |> Integer.parse() do
|
|
|
|
{multiplier, _remainder}
|
|
|
|
when multiplier >= 1 and multiplier <= @ammo_group_create_limit ->
|
|
|
|
socket |> create_multiple(ammo_group_params, multiplier)
|
2022-02-10 00:57:56 -05:00
|
|
|
|
2022-02-24 00:16:23 -05:00
|
|
|
{multiplier, _remainder} ->
|
|
|
|
error_msg =
|
|
|
|
dgettext(
|
|
|
|
"errors",
|
|
|
|
"Invalid number of copies, must be between 1 and %{max}. Was %{multiplier}",
|
|
|
|
max: @ammo_group_create_limit,
|
|
|
|
multiplier: multiplier
|
|
|
|
)
|
|
|
|
|
|
|
|
{:error, changeset} =
|
|
|
|
changeset
|
|
|
|
|> Changeset.add_error(:multiplier, error_msg)
|
|
|
|
|> Changeset.apply_action(:insert)
|
|
|
|
|
|
|
|
socket |> assign(:changeset, changeset)
|
|
|
|
|
|
|
|
: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)
|
2022-02-10 00:57:56 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
{:noreply, socket}
|
2021-09-02 23:31:14 -04:00
|
|
|
end
|
2022-02-24 00:16:23 -05:00
|
|
|
|
|
|
|
defp create_multiple(
|
|
|
|
%{assigns: %{current_user: current_user, return_to: return_to}} = socket,
|
|
|
|
ammo_group_params,
|
|
|
|
multiplier
|
|
|
|
) do
|
|
|
|
case Ammo.create_ammo_groups(ammo_group_params, multiplier, current_user) do
|
|
|
|
{:ok, {count, _ammo_groups}} ->
|
|
|
|
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_redirect(to: return_to)
|
|
|
|
|
|
|
|
{:error, %Changeset{} = changeset} ->
|
|
|
|
socket |> assign(changeset: changeset)
|
|
|
|
end
|
|
|
|
end
|
2021-09-02 23:31:14 -04:00
|
|
|
end
|