prompt to create first ammo type before trying to create first ammo
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2022-07-04 21:41:07 -04:00
parent 5836a82ff7
commit f246b9db93
26 changed files with 233 additions and 146 deletions

View File

@ -25,6 +25,25 @@ defmodule Cannery.Ammo do
def list_ammo_types(%User{id: user_id}),
do: Repo.all(from at in AmmoType, where: at.user_id == ^user_id, order_by: at.name)
@doc """
Returns a count of ammo_types.
## Examples
iex> get_ammo_types_count!(%User{id: 123})
3
"""
@spec get_ammo_types_count!(User.t()) :: integer()
def get_ammo_types_count!(%User{id: user_id}) do
Repo.one(
from at in AmmoType,
where: at.user_id == ^user_id,
select: count(at.id),
distinct: true
)
end
@doc """
Gets a single ammo_type.

View File

@ -30,6 +30,25 @@ defmodule Cannery.Containers do
)
end
@doc """
Returns a count of containers.
## Examples
iex> get_containers_count!(%User{id: 123})
3
"""
@spec get_containers_count!(User.t()) :: integer()
def get_containers_count!(%User{id: user_id}) do
Repo.one(
from c in Container,
where: c.user_id == ^user_id,
select: count(c.id),
distinct: true
)
end
@doc """
Gets a single container.

View File

@ -22,7 +22,8 @@ defmodule CanneryWeb.AmmoGroupLive.FormComponent do
@spec update(Socket.t()) :: {:ok, Socket.t()}
def update(%{assigns: %{current_user: current_user}} = socket) do
%{assigns: %{ammo_types: ammo_types, containers: containers}} = socket =
%{assigns: %{ammo_types: ammo_types, containers: containers}} =
socket =
socket
|> assign(:ammo_group_create_limit, @ammo_group_create_limit)
|> assign(:ammo_types, Ammo.list_ammo_types(current_user))

View File

@ -74,7 +74,8 @@ 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])
containers = Containers.list_containers(current_user)
ammo_types_count = Ammo.get_ammo_types_count!(current_user)
containers_count = Containers.get_containers_count!(current_user)
columns = [
%{label: gettext("Ammo type"), key: "ammo_type"},
@ -92,7 +93,13 @@ defmodule CanneryWeb.AmmoGroupLive.Index do
|> Enum.map(fn ammo_group -> ammo_group |> get_row_data_for_ammo_group(columns) end)
socket
|> assign(ammo_groups: ammo_groups, containers: containers, columns: columns, rows: rows)
|> assign(
ammo_groups: ammo_groups,
ammo_types_count: ammo_types_count,
containers_count: containers_count,
columns: columns,
rows: rows
)
end
@spec get_row_data_for_ammo_group(AmmoGroup.t(), [map()]) :: [map()]

View File

@ -8,8 +8,10 @@
<%= gettext("No Ammo") %>
<%= display_emoji("😔") %>
</h2>
<% end %>
<%= if @containers |> Enum.empty?() do %>
<%= cond do %>
<% @containers_count == 0 -> %>
<div class="flex justify-center items-center">
<h2 class="m-2 title text-md text-primary-600">
<%= dgettext("prompts", "You'll need to") %>
@ -20,31 +22,30 @@
class: "btn btn-primary"
) %>
</div>
<% else %>
<% @ammo_types_count == 0 -> %>
<div class="flex justify-center items-center">
<h2 class="m-2 title text-md text-primary-600">
<%= dgettext("prompts", "You'll need to") %>
</h2>
<%= live_patch(dgettext("actions", "add an ammo type first"),
to: Routes.ammo_type_index_path(Endpoint, :new),
class: "btn btn-primary"
) %>
</div>
<% @ammo_groups |> Enum.empty?() -> %>
<%= live_patch(dgettext("actions", "Add your first box!"),
to: Routes.ammo_group_index_path(Endpoint, :new),
class: "btn btn-primary"
) %>
<% end %>
<% else %>
<%= if @containers |> Enum.empty?() do %>
<div class="flex justify-center items-center">
<h2 class="m-2 title text-md text-primary-600">
<%= dgettext("prompts", "You'll need to") %>
</h2>
<%= live_patch(dgettext("actions", "add a container first"),
to: Routes.container_index_path(Endpoint, :new),
class: "btn btn-primary"
) %>
</div>
<% else %>
<% true -> %>
<%= live_patch(dgettext("actions", "Add Ammo"),
to: Routes.ammo_group_index_path(Endpoint, :new),
class: "btn btn-primary"
) %>
<% end %>
<% end %>
<%= unless @ammo_groups |> Enum.empty?() do %>
<.live_component
module={CanneryWeb.Components.TableComponent}
id="ammo_groups_index_table"
@ -66,7 +67,6 @@
ammo_group={@ammo_group}
return_to={Routes.ammo_group_index_path(Endpoint, :index)}
current_user={@current_user}
containers={@containers}
/>
</.modal>
<% @live_action == :add_shot_group -> %>