diff --git a/lib/cannery_web/components/move_ammo_group_component.ex b/lib/cannery_web/components/move_ammo_group_component.ex new file mode 100644 index 00000000..f186ebeb --- /dev/null +++ b/lib/cannery_web/components/move_ammo_group_component.ex @@ -0,0 +1,57 @@ +defmodule CanneryWeb.Components.MoveAmmoGroupComponent do + @moduledoc """ + Livecomponent that can move an ammo group to another container + """ + + use CanneryWeb, :live_component + alias Cannery.{Accounts.User, Ammo, Ammo.AmmoGroup, Containers} + 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 + changeset = Ammo.change_ammo_group(ammo_group) + + containers = + Containers.list_containers(current_user) + |> Enum.reject(fn %{id: id} -> id == container_id end) + + {:ok, socket |> assign(assigns) |> assign(changeset: changeset, containers: containers)} + 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) + + socket |> put_flash(:info, prompt) |> push_redirect(to: return_to) + + {:error, %Ecto.Changeset{} = changeset} -> + socket |> assign(changeset: changeset) + end + + {:noreply, socket} + end +end diff --git a/lib/cannery_web/components/move_ammo_group_component.html.heex b/lib/cannery_web/components/move_ammo_group_component.html.heex new file mode 100644 index 00000000..c30eb1e1 --- /dev/null +++ b/lib/cannery_web/components/move_ammo_group_component.html.heex @@ -0,0 +1,69 @@ +
+

+ <%= gettext("Move ammo") %> +

+ + <%= if @containers |> Enum.empty?() do %> +

+ <%= gettext("No other containers") %> 😔 +

+ + <%= live_patch(dgettext("actions", "Add another container!"), + to: Routes.container_index_path(@socket, :new), + class: "btn btn-primary" + ) %> + <% else %> +
+ + + + + + + + + + + + + + <%= for container <- @containers do %> + + + + + + + + + + <% end %> + +
+ <%= gettext("Container") %> + + <%= gettext("Type") %> + + <%= gettext("Location") %> +
+ <%= container.name %> + + <%= container.type %> + + <%= container.location %> + +
+ +
+
+
+ <% end %> +
diff --git a/lib/cannery_web/live/ammo_group_live/index.ex b/lib/cannery_web/live/ammo_group_live/index.ex index 4c1d3dc7..2281fcc9 100644 --- a/lib/cannery_web/live/ammo_group_live/index.ex +++ b/lib/cannery_web/live/ammo_group_live/index.ex @@ -17,6 +17,12 @@ defmodule CanneryWeb.AmmoGroupLive.Index do {:noreply, apply_action(socket, live_action, params)} end + defp apply_action(%{assigns: %{current_user: current_user}} = socket, :move, %{"id" => id}) do + socket + |> assign(:page_title, gettext("Move Ammo group")) + |> assign(:ammo_group, Ammo.get_ammo_group!(id, current_user)) + end + defp apply_action(%{assigns: %{current_user: current_user}} = socket, :edit, %{"id" => id}) do socket |> assign(:page_title, gettext("Edit Ammo group")) diff --git a/lib/cannery_web/live/ammo_group_live/index.html.heex b/lib/cannery_web/live/ammo_group_live/index.html.heex index 4bb5371b..c7ee2510 100644 --- a/lib/cannery_web/live/ammo_group_live/index.html.heex +++ b/lib/cannery_web/live/ammo_group_live/index.html.heex @@ -80,7 +80,12 @@ - <%= if ammo_group.container, do: ammo_group.container.name %> + <%= if ammo_group.container do %> + <%= live_patch(ammo_group.container.name, + to: Routes.ammo_group_index_path(@socket, :move, ammo_group), + class: "btn btn-primary" + ) %> + <% end %> @@ -112,30 +117,45 @@ <% end %> -<%= if @live_action in [:new, :edit] do %> - <.modal return_to={Routes.ammo_group_index_path(Endpoint, :index)}> - <.live_component - module={CanneryWeb.AmmoGroupLive.FormComponent} - id={@ammo_group.id || :new} - title={@page_title} - action={@live_action} - ammo_group={@ammo_group} - return_to={Routes.ammo_group_index_path(Endpoint, :index)} - current_user={@current_user} - /> - -<% end %> +<%= cond do %> + <% @live_action in [:new, :edit] -> %> + <.modal return_to={Routes.ammo_group_index_path(Endpoint, :index)}> + <.live_component + module={CanneryWeb.AmmoGroupLive.FormComponent} + id={@ammo_group.id || :new} + title={@page_title} + action={@live_action} + ammo_group={@ammo_group} + return_to={Routes.ammo_group_index_path(Endpoint, :index)} + current_user={@current_user} + /> + -<%= if @live_action in [:add_shot_group] do %> - <.modal return_to={Routes.ammo_group_index_path(Endpoint, :index)}> - <.live_component - module={CanneryWeb.Components.AddShotGroupComponent} - id={:new} - title={@page_title} - action={@live_action} - ammo_group={@ammo_group} - return_to={Routes.ammo_group_index_path(Endpoint, :index)} - current_user={@current_user} - /> - + <% @live_action == :add_shot_group -> %> + <.modal return_to={Routes.ammo_group_index_path(Endpoint, :index)}> + <.live_component + module={CanneryWeb.Components.AddShotGroupComponent} + id={:new} + title={@page_title} + action={@live_action} + ammo_group={@ammo_group} + return_to={Routes.ammo_group_index_path(Endpoint, :index)} + current_user={@current_user} + /> + + + <% @live_action == :move -> %> + <.modal return_to={Routes.ammo_group_index_path(Endpoint, :index)}> + <.live_component + module={CanneryWeb.Components.MoveAmmoGroupComponent} + id={@ammo_group.id} + title={@page_title} + action={@live_action} + ammo_group={@ammo_group} + return_to={Routes.ammo_group_index_path(Endpoint, :index)} + current_user={@current_user} + /> + + + <% true -> %> <% end %> diff --git a/lib/cannery_web/live/ammo_group_live/show.ex b/lib/cannery_web/live/ammo_group_live/show.ex index 776348c9..a3da36f9 100644 --- a/lib/cannery_web/live/ammo_group_live/show.ex +++ b/lib/cannery_web/live/ammo_group_live/show.ex @@ -14,26 +14,11 @@ defmodule CanneryWeb.AmmoGroupLive.Show do end @impl true - def handle_params(params, _url, %{assigns: %{live_action: live_action}} = socket) do - socket |> assign(page_title: page_title(live_action)) |> apply_action(live_action, params) - end - - defp apply_action( - %{assigns: %{current_user: current_user}} = socket, - :add_shot_group, - %{"id" => id} - ) do - socket - |> assign(:page_title, gettext("Add Shot group")) - |> assign(:ammo_group, Ammo.get_ammo_group!(id, current_user)) - end - - defp apply_action( - %{assigns: %{live_action: live_action, current_user: current_user}} = socket, - action, - %{"id" => id} - ) - when action == :edit or action == :show do + def handle_params( + %{"id" => id}, + _url, + %{assigns: %{live_action: live_action, current_user: current_user}} = socket + ) do ammo_group = Ammo.get_ammo_group!(id, current_user) |> Repo.preload([:container, :ammo_type]) {:noreply, socket |> assign(page_title: page_title(live_action), ammo_group: ammo_group)} end @@ -64,6 +49,8 @@ defmodule CanneryWeb.AmmoGroupLive.Show do {:noreply, socket |> assign(ammo_group: ammo_group)} end + defp page_title(:add_shot_group), do: gettext("Add Shot group") + defp page_title(:move), do: gettext("Move Ammo group") defp page_title(:show), do: gettext("Show Ammo group") defp page_title(:edit), do: gettext("Edit Ammo group") end diff --git a/lib/cannery_web/live/ammo_group_live/show.html.heex b/lib/cannery_web/live/ammo_group_live/show.html.heex index b8322647..7d853263 100644 --- a/lib/cannery_web/live/ammo_group_live/show.html.heex +++ b/lib/cannery_web/live/ammo_group_live/show.html.heex @@ -25,11 +25,11 @@
<%= live_patch(dgettext("actions", "Ammo Details"), - to: Routes.ammo_type_show_path(@socket, :show, @ammo_group.ammo_type), + to: Routes.ammo_type_show_path(Endpoint, :show, @ammo_group.ammo_type), class: "btn btn-primary" ) %> - <%= live_patch to: Routes.ammo_group_show_path(@socket, :edit, @ammo_group), + <%= live_patch to: Routes.ammo_group_show_path(Endpoint, :edit, @ammo_group), class: "text-primary-500 link" do %> <% end %> @@ -44,6 +44,11 @@ + + <%= live_patch(dgettext("actions", "Move to different container"), + to: Routes.ammo_group_show_path(Endpoint, :move, @ammo_group), + class: "btn btn-primary" + ) %>

@@ -61,30 +66,45 @@ -<%= if @live_action in [:edit] do %> - <.modal return_to={Routes.ammo_group_show_path(Endpoint, :show, @ammo_group)}> - <.live_component - module={CanneryWeb.AmmoGroupLive.FormComponent} - id={@ammo_group.id} - title={@page_title} - action={@live_action} - ammo_group={@ammo_group} - return_to={Routes.ammo_group_show_path(Endpoint, :show, @ammo_group)} - current_user={@current_user} - /> - -<% end %> +<%= case @live_action do %> + <% :edit -> %> + <.modal return_to={Routes.ammo_group_show_path(Endpoint, :show, @ammo_group)}> + <.live_component + module={CanneryWeb.AmmoGroupLive.FormComponent} + id={@ammo_group.id} + title={@page_title} + action={@live_action} + ammo_group={@ammo_group} + return_to={Routes.ammo_group_show_path(Endpoint, :show, @ammo_group)} + current_user={@current_user} + /> + -<%= if @live_action in [:add_shot_group] do %> - <.modal return_to={Routes.ammo_group_show_path(Endpoint, :show, @ammo_group)}> - <.live_component - module={CanneryWeb.Components.AddShotGroupComponent} - id={:new} - title={@page_title} - action={@live_action} - ammo_group={@ammo_group} - return_to={Routes.ammo_group_show_path(Endpoint, :show, @ammo_group)} - current_user={@current_user} - /> - + <% :add_shot_group -> %> + <.modal return_to={Routes.ammo_group_show_path(Endpoint, :show, @ammo_group)}> + <.live_component + module={CanneryWeb.Components.AddShotGroupComponent} + id={:new} + title={@page_title} + action={@live_action} + ammo_group={@ammo_group} + return_to={Routes.ammo_group_show_path(Endpoint, :show, @ammo_group)} + current_user={@current_user} + /> + + + <% :move -> %> + <.modal return_to={Routes.ammo_group_show_path(Endpoint, :show, @ammo_group)}> + <.live_component + module={CanneryWeb.Components.MoveAmmoGroupComponent} + id={@ammo_group.id} + title={@page_title} + action={@live_action} + ammo_group={@ammo_group} + return_to={Routes.ammo_group_show_path(Endpoint, :show, @ammo_group)} + current_user={@current_user} + /> + + + <% _show -> %> <% end %> diff --git a/lib/cannery_web/live/live_helpers.ex b/lib/cannery_web/live/live_helpers.ex index 3510da58..1df5f04c 100644 --- a/lib/cannery_web/live/live_helpers.ex +++ b/lib/cannery_web/live/live_helpers.ex @@ -63,7 +63,7 @@ defmodule CanneryWeb.LiveHelpers do <% end %> -
+
<%= render_slot(@inner_block) %>
diff --git a/lib/cannery_web/live/range_live/index.html.heex b/lib/cannery_web/live/range_live/index.html.heex index 1854696e..4ebf4775 100644 --- a/lib/cannery_web/live/range_live/index.html.heex +++ b/lib/cannery_web/live/range_live/index.html.heex @@ -45,6 +45,10 @@ <%= gettext("No shots recorded") %> 😔 <% else %> +

+ <%= gettext("Shot log") %> +

+
diff --git a/lib/cannery_web/router.ex b/lib/cannery_web/router.ex index 858aa6bf..1c84ffb5 100644 --- a/lib/cannery_web/router.ex +++ b/lib/cannery_web/router.ex @@ -73,10 +73,12 @@ defmodule CanneryWeb.Router do live "/ammo_groups/new", AmmoGroupLive.Index, :new live "/ammo_groups/:id/edit", AmmoGroupLive.Index, :edit live "/ammo_groups/:id/add_shot_group", AmmoGroupLive.Index, :add_shot_group + live "/ammo_groups/:id/move", AmmoGroupLive.Index, :move live "/ammo_groups/:id", AmmoGroupLive.Show, :show live "/ammo_groups/:id/show/edit", AmmoGroupLive.Show, :edit live "/ammo_groups/:id/show/add_shot_group", AmmoGroupLive.Show, :add_shot_group + live "/ammo_groups/:id/show/move", AmmoGroupLive.Show, :move live "/range", RangeLive.Index, :index live "/range/:id/edit", RangeLive.Index, :edit diff --git a/priv/gettext/actions.pot b/priv/gettext/actions.pot index ff9abd17..dbd3fa29 100644 --- a/priv/gettext/actions.pot +++ b/priv/gettext/actions.pot @@ -11,7 +11,7 @@ msgid "" msgstr "" #, elixir-format, ex-autogen -#: lib/cannery_web/live/ammo_group_live/index.ex:28 +#: lib/cannery_web/live/ammo_group_live/index.ex:34 msgid "Add Ammo" msgstr "" @@ -168,3 +168,18 @@ msgstr "" #: lib/cannery_web/live/ammo_group_live/show.html.heex:27 msgid "Ammo Details" msgstr "" + +#, elixir-format, ex-autogen +#: lib/cannery_web/components/move_ammo_group_component.html.heex:11 +msgid "Add another container!" +msgstr "" + +#, elixir-format, ex-autogen +#: lib/cannery_web/live/ammo_group_live/show.html.heex:48 +msgid "Move to different container" +msgstr "" + +#, elixir-format, ex-autogen +#: lib/cannery_web/components/move_ammo_group_component.html.heex:59 +msgid "Select" +msgstr "" diff --git a/priv/gettext/default.pot b/priv/gettext/default.pot index a26288bf..c904879e 100644 --- a/priv/gettext/default.pot +++ b/priv/gettext/default.pot @@ -33,7 +33,7 @@ msgstr "" #, elixir-format, ex-autogen #: lib/cannery_web/components/topbar.ex:47 #: lib/cannery_web/live/ammo_group_live/index.html.heex:3 -#: lib/cannery_web/live/range_live/index.html.heex:53 +#: lib/cannery_web/live/range_live/index.html.heex:57 msgid "Ammo" msgstr "" @@ -100,6 +100,7 @@ msgid "Case material" msgstr "" #, elixir-format, ex-autogen +#: lib/cannery_web/components/move_ammo_group_component.html.heex:21 #: lib/cannery_web/live/ammo_group_live/form_component.html.heex:48 #: lib/cannery_web/live/ammo_group_live/index.html.heex:41 msgid "Container" @@ -152,8 +153,8 @@ msgid "Easy to Use:" msgstr "" #, elixir-format, ex-autogen -#: lib/cannery_web/live/ammo_group_live/index.ex:22 -#: lib/cannery_web/live/ammo_group_live/show.ex:68 +#: lib/cannery_web/live/ammo_group_live/index.ex:28 +#: lib/cannery_web/live/ammo_group_live/show.ex:55 msgid "Edit Ammo group" msgstr "" @@ -254,6 +255,7 @@ msgid "Listing Tags" msgstr "" #, elixir-format, ex-autogen +#: lib/cannery_web/components/move_ammo_group_component.html.heex:29 #: lib/cannery_web/live/container_live/form_component.html.heex:42 msgid "Location" msgstr "" @@ -359,7 +361,7 @@ msgstr "" #: lib/cannery_web/live/ammo_group_live/form_component.html.heex:41 #: lib/cannery_web/live/ammo_group_live/index.html.heex:35 #: lib/cannery_web/live/range_live/form_component.html.heex:29 -#: lib/cannery_web/live/range_live/index.html.heex:59 +#: lib/cannery_web/live/range_live/index.html.heex:63 msgid "Notes" msgstr "" @@ -437,7 +439,7 @@ msgid "Settings" msgstr "" #, elixir-format, ex-autogen -#: lib/cannery_web/live/ammo_group_live/show.ex:67 +#: lib/cannery_web/live/ammo_group_live/show.ex:54 msgid "Show Ammo group" msgstr "" @@ -467,7 +469,7 @@ msgid "Steel" msgstr "" #, elixir-format, ex-autogen -#: lib/cannery_web/live/ammo_group_live/show.html.heex:54 +#: lib/cannery_web/live/ammo_group_live/show.html.heex:59 msgid "Stored in" msgstr "" @@ -494,7 +496,7 @@ msgid "The self-hosted firearm tracker website" msgstr "" #, elixir-format, ex-autogen -#: lib/cannery_web/live/ammo_group_live/show.html.heex:59 +#: lib/cannery_web/live/ammo_group_live/show.html.heex:64 msgid "This ammo group is not in a container" msgstr "" @@ -505,6 +507,7 @@ msgid "Tracer" msgstr "" #, elixir-format, ex-autogen +#: lib/cannery_web/components/move_ammo_group_component.html.heex:25 #: lib/cannery_web/live/container_live/form_component.html.heex:35 msgid "Type" msgstr "" @@ -561,7 +564,7 @@ msgid "Range day" msgstr "" #, elixir-format, ex-autogen -#: lib/cannery_web/live/range_live/index.html.heex:62 +#: lib/cannery_web/live/range_live/index.html.heex:66 msgid "Date" msgstr "" @@ -595,7 +598,7 @@ msgid "Staging" msgstr "" #, elixir-format, ex-autogen -#: lib/cannery_web/live/ammo_group_live/show.ex:27 +#: lib/cannery_web/live/ammo_group_live/show.ex:52 msgid "Add Shot group" msgstr "" @@ -611,7 +614,7 @@ msgid "Ammo Types" msgstr "" #, elixir-format, ex-autogen -#: lib/cannery_web/live/ammo_group_live/index.ex:33 +#: lib/cannery_web/live/ammo_group_live/index.ex:39 msgid "Ammo groups" msgstr "" @@ -642,7 +645,7 @@ msgid "Rounds left" msgstr "" #, elixir-format, ex-autogen -#: lib/cannery_web/live/range_live/index.html.heex:56 +#: lib/cannery_web/live/range_live/index.html.heex:60 msgid "Rounds shot" msgstr "" @@ -650,3 +653,24 @@ msgstr "" #: lib/cannery_web/live/range_live/index.ex:46 msgid "Shot Records" msgstr "" + +#, elixir-format, ex-autogen +#: lib/cannery_web/live/ammo_group_live/index.ex:22 +#: lib/cannery_web/live/ammo_group_live/show.ex:53 +msgid "Move Ammo group" +msgstr "" + +#, elixir-format, ex-autogen +#: lib/cannery_web/components/move_ammo_group_component.html.heex:3 +msgid "Move ammo" +msgstr "" + +#, elixir-format, ex-autogen +#: lib/cannery_web/components/move_ammo_group_component.html.heex:8 +msgid "No other containers" +msgstr "" + +#, elixir-format, ex-autogen +#: lib/cannery_web/live/range_live/index.html.heex:49 +msgid "Shot log" +msgstr "" diff --git a/priv/gettext/prompts.pot b/priv/gettext/prompts.pot index 03dee4f5..be15ac97 100644 --- a/priv/gettext/prompts.pot +++ b/priv/gettext/prompts.pot @@ -67,8 +67,8 @@ msgid "Ammo group created successfully" msgstr "" #, elixir-format, ex-autogen -#: lib/cannery_web/live/ammo_group_live/index.ex:40 -#: lib/cannery_web/live/ammo_group_live/show.ex:49 +#: lib/cannery_web/live/ammo_group_live/index.ex:46 +#: lib/cannery_web/live/ammo_group_live/show.ex:34 msgid "Ammo group deleted succesfully" msgstr "" @@ -97,7 +97,7 @@ msgid "Are you sure you want to delete the invite for %{name}?" msgstr "" #, elixir-format, ex-autogen -#: lib/cannery_web/live/ammo_group_live/index.html.heex:102 +#: lib/cannery_web/live/ammo_group_live/index.html.heex:107 #: lib/cannery_web/live/ammo_group_live/show.html.heex:40 #: lib/cannery_web/live/ammo_type_live/index.html.heex:104 msgid "Are you sure you want to delete this ammo?" @@ -210,7 +210,7 @@ msgid "Ammo group unstaged succesfully" msgstr "" #, elixir-format, ex-autogen -#: lib/cannery_web/live/range_live/index.html.heex:97 +#: lib/cannery_web/live/range_live/index.html.heex:101 msgid "Are you sure you want to delete this shot record?" msgstr "" @@ -228,3 +228,8 @@ msgstr "" #: lib/cannery_web/controllers/user_confirmation_controller.ex:37 msgid "%{email} confirmed successfully." msgstr "" + +#, elixir-format, ex-autogen +#: lib/cannery_web/components/move_ammo_group_component.ex:47 +msgid "Ammo moved to %{name} successfully" +msgstr ""