From 8bb4aab49cd5ba25b55652d6ca6366c929c4de4d Mon Sep 17 00:00:00 2001 From: shibao Date: Fri, 4 Mar 2022 23:16:37 -0500 Subject: [PATCH] Fix ammo group create form error bug --- CHANGELOG.md | 1 + lib/cannery/ammo.ex | 38 +++++++++---------- .../live/ammo_group_live/form_component.ex | 22 ++++++++--- .../ammo_group_live/form_component.html.heex | 1 + 4 files changed, 36 insertions(+), 26 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3bf1be64..2c70e27f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/cannery/ammo.ex b/lib/cannery/ammo.ex index 094307da..4b9f39ac 100644 --- a/lib/cannery/ammo.ex +++ b/lib/cannery/ammo.ex @@ -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,26 +361,24 @@ defmodule Cannery.Ammo do end) if changesets |> Enum.all?(fn %{valid?: valid} -> valid end) do - Multi.new() - |> Multi.insert_all( - :create_ammo_groups, - AmmoGroup, - changesets - |> Enum.map(fn changeset -> - changeset - |> Map.get(:changes) - |> Map.merge(%{inserted_at: now, updated_at: now}) - 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 + {count, inserted_ammo_groups} = + Repo.insert_all( + AmmoGroup, + changesets + |> Enum.map(fn changeset -> + changeset + |> Map.get(:changes) + |> Map.merge(%{inserted_at: now, updated_at: now}) + end), + returning: true + ) + + {: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 diff --git a/lib/cannery_web/live/ammo_group_live/form_component.ex b/lib/cannery_web/live/ammo_group_live/form_component.ex index a87be408..213cef2a 100644 --- a/lib/cannery_web/live/ammo_group_live/form_component.ex +++ b/lib/cannery_web/live/ammo_group_live/form_component.ex @@ -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 diff --git a/lib/cannery_web/live/ammo_group_live/form_component.html.heex b/lib/cannery_web/live/ammo_group_live/form_component.html.heex index 3fe8bf2d..86b4cd13 100644 --- a/lib/cannery_web/live/ammo_group_live/form_component.html.heex +++ b/lib/cannery_web/live/ammo_group_live/form_component.html.heex @@ -69,6 +69,7 @@ ) %> <%= error_tag(f, :multiplier, "col-span-3 text-center") %> + <% :edit -> %> <%= submit(dgettext("actions", "Save"), phx_disable_with: dgettext("prompts", "Saving..."),