Fix ammo group create form error bug

This commit is contained in:
shibao 2022-03-04 23:16:37 -05:00
parent 9f2cc54738
commit 8bb4aab49c
4 changed files with 36 additions and 26 deletions

View File

@ -3,6 +3,7 @@
- Add link to changelog from version number
- Fix some elements flashing with black background
- Fix bug with moving ammo group to new container
- Fix bug with no error showing up for create ammo group form
# v0.3.0
- Fix ammo type counts not showing when count is 0

View File

@ -342,7 +342,7 @@ defmodule Cannery.Ammo do
"""
@spec create_ammo_groups(attrs :: map(), multiplier :: non_neg_integer(), User.t()) ::
{:ok, {count :: non_neg_integer(), [AmmoGroup.t()] | nil}}
| {:error, Changeset.t(AmmoGroup.new_ammo_group()) | nil}
| {:error, Changeset.t(AmmoGroup.new_ammo_group())}
def create_ammo_groups(
%{"ammo_type_id" => ammo_type_id, "container_id" => container_id} = attrs,
multiplier,
@ -361,9 +361,8 @@ defmodule Cannery.Ammo do
end)
if changesets |> Enum.all?(fn %{valid?: valid} -> valid end) do
Multi.new()
|> Multi.insert_all(
:create_ammo_groups,
{count, inserted_ammo_groups} =
Repo.insert_all(
AmmoGroup,
changesets
|> Enum.map(fn changeset ->
@ -373,14 +372,13 @@ defmodule Cannery.Ammo do
end),
returning: true
)
|> Repo.transaction()
|> case do
{:ok, %{create_ammo_groups: {count, ammo_groups}}} -> {:ok, {count, ammo_groups}}
{:error, :create_ammo_groups, changeset, _changes_so_far} -> {:error, changeset}
{:error, _other_transaction, _value, _changes_so_far} -> {:error, nil}
end
{:ok, {count, inserted_ammo_groups}}
else
{:error, changesets |> List.first()}
changesets
|> Enum.reject(fn %{valid?: valid} -> valid end)
|> List.first()
|> Changeset.apply_action(:insert)
end
end

View File

@ -36,10 +36,23 @@ defmodule CanneryWeb.AmmoGroupLive.FormComponent do
def handle_event(
"validate",
%{"ammo_group" => ammo_group_params},
%{assigns: %{ammo_group: ammo_group}} = socket
%{assigns: %{action: action, ammo_group: ammo_group}} = socket
) do
socket = socket |> assign(:changeset, ammo_group |> Ammo.change_ammo_group(ammo_group_params))
{:noreply, socket}
changeset_action =
case action do
:new -> :insert
:edit -> :update
end
changeset = ammo_group |> Ammo.change_ammo_group(ammo_group_params)
changeset =
case changeset |> Changeset.apply_action(changeset_action) do
{:ok, _data} -> changeset
{:error, changeset} -> changeset
end
{:noreply, socket |> assign(:changeset, changeset)}
end
def handle_event(
@ -142,9 +155,6 @@ defmodule CanneryWeb.AmmoGroupLive.FormComponent do
{:error, %Changeset{} = changeset} ->
socket |> assign(changeset: changeset)
{:error, nil} ->
socket
end
end
end

View File

@ -69,6 +69,7 @@
) %>
<%= error_tag(f, :multiplier, "col-span-3 text-center") %>
<% :edit -> %>
<%= submit(dgettext("actions", "Save"),
phx_disable_with: dgettext("prompts", "Saving..."),