From 9773ccc6ffe91cd6145840930812969346340afc Mon Sep 17 00:00:00 2001 From: shibao Date: Wed, 23 Feb 2022 20:34:07 -0500 Subject: [PATCH] add prompt to create container before creating ammo group --- lib/cannery_web/live/ammo_group_live/index.ex | 13 +++--- .../live/ammo_group_live/index.html.heex | 43 +++++++++++++++---- 2 files changed, 43 insertions(+), 13 deletions(-) diff --git a/lib/cannery_web/live/ammo_group_live/index.ex b/lib/cannery_web/live/ammo_group_live/index.ex index 0a030d8c..d7e6eed0 100644 --- a/lib/cannery_web/live/ammo_group_live/index.ex +++ b/lib/cannery_web/live/ammo_group_live/index.ex @@ -4,7 +4,7 @@ defmodule CanneryWeb.AmmoGroupLive.Index do """ use CanneryWeb, :live_view - alias Cannery.{Ammo, Ammo.AmmoGroup, Repo} + alias Cannery.{Ammo, Ammo.AmmoGroup, Containers, Repo} alias CanneryWeb.Endpoint @impl true @@ -17,9 +17,11 @@ defmodule CanneryWeb.AmmoGroupLive.Index do {:noreply, apply_action(socket, live_action, params)} end - defp apply_action(%{assigns: %{current_user: current_user}} = socket, :add_shot_group, %{ - "id" => id - }) do + defp apply_action( + %{assigns: %{current_user: current_user}} = socket, + :add_shot_group, + %{"id" => id} + ) do socket |> assign(:page_title, gettext("Record shots")) |> assign(:ammo_group, Ammo.get_ammo_group!(id, current_user)) @@ -72,6 +74,7 @@ defmodule CanneryWeb.AmmoGroupLive.Index do defp display_ammo_groups(%{assigns: %{current_user: current_user}} = socket) do ammo_groups = Ammo.list_ammo_groups(current_user) |> Repo.preload([:ammo_type, :container]) - socket |> assign(:ammo_groups, ammo_groups) + containers = Containers.list_containers(current_user) + socket |> assign(ammo_groups: ammo_groups, containers: containers) end end 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 c2fab220..a4b59a2a 100644 --- a/lib/cannery_web/live/ammo_group_live/index.html.heex +++ b/lib/cannery_web/live/ammo_group_live/index.html.heex @@ -9,15 +9,41 @@ <%= display_emoji("😔") %> - <%= live_patch(dgettext("actions", "Add your first box!"), - to: Routes.ammo_group_index_path(Endpoint, :new), - class: "btn btn-primary" - ) %> + <%= if @containers |> Enum.empty?() do %> +
+

+ <%= dgettext("prompts", "You'll need to") %> +

+ + <%= live_patch(dgettext("actions", "add a container first"), + to: Routes.container_index_path(Endpoint, :new), + class: "btn btn-primary" + ) %> +
+ <% else %> + <%= live_patch(dgettext("actions", "Add your first box!"), + to: Routes.ammo_group_index_path(Endpoint, :new), + class: "btn btn-primary" + ) %> + <% end %> <% else %> - <%= live_patch(dgettext("actions", "New Ammo group"), - to: Routes.ammo_group_index_path(Endpoint, :new), - class: "btn btn-primary" - ) %> + <%= if @containers |> Enum.empty?() do %> +
+

+ <%= dgettext("prompts", "You'll need to") %> +

+ + <%= live_patch(dgettext("actions", "add a container first"), + to: Routes.container_index_path(Endpoint, :new), + class: "btn btn-primary" + ) %> +
+ <% else %> + <%= live_patch(dgettext("actions", "New Ammo group"), + to: Routes.ammo_group_index_path(Endpoint, :new), + class: "btn btn-primary" + ) %> + <% end %>
@@ -143,6 +169,7 @@ ammo_group={@ammo_group} return_to={Routes.ammo_group_index_path(Endpoint, :index)} current_user={@current_user} + containers={@containers} /> <% @live_action == :add_shot_group -> %>