diff --git a/CHANGELOG.md b/CHANGELOG.md
index 0ff83b41..ed2da98f 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,6 +1,7 @@
# v0.9.1
- Rename ammo type's "type" to "class" to avoid confusion
- Fixes ammo type search
+- Fixes shot records table disappearing after selecting an empty ammo class
- Code quality improvements
# v0.9.0
diff --git a/lib/cannery/activity_log.ex b/lib/cannery/activity_log.ex
index c46d19a8..69ba063e 100644
--- a/lib/cannery/activity_log.ex
+++ b/lib/cannery/activity_log.ex
@@ -92,6 +92,25 @@ defmodule Cannery.ActivityLog do
defp list_shot_groups_filter_type(query, _all), do: query
+ @doc """
+ Returns a count of shot records.
+
+ ## Examples
+
+ iex> get_shot_record_count!(%User{id: 123})
+ 3
+
+ """
+ @spec get_shot_record_count!(User.t()) :: integer()
+ def get_shot_record_count!(%User{id: user_id}) do
+ Repo.one(
+ from sg in ShotGroup,
+ where: sg.user_id == ^user_id,
+ select: count(sg.id),
+ distinct: true
+ ) || 0
+ end
+
@spec list_shot_groups_for_pack(Pack.t(), User.t()) :: [ShotGroup.t()]
def list_shot_groups_for_pack(
%Pack{id: pack_id, user_id: user_id},
diff --git a/lib/cannery_web/live/range_live/index.ex b/lib/cannery_web/live/range_live/index.ex
index ad914fe9..7bb2b2c3 100644
--- a/lib/cannery_web/live/range_live/index.ex
+++ b/lib/cannery_web/live/range_live/index.ex
@@ -127,6 +127,7 @@ defmodule CanneryWeb.RangeLive.Index do
original_counts = packs |> Ammo.get_original_counts(current_user)
cprs = packs |> Ammo.get_cprs(current_user)
last_used_dates = packs |> ActivityLog.get_last_used_dates(current_user)
+ shot_record_count = ActivityLog.get_shot_record_count!(current_user)
socket
|> assign(
@@ -135,7 +136,8 @@ defmodule CanneryWeb.RangeLive.Index do
cprs: cprs,
last_used_dates: last_used_dates,
chart_data: chart_data,
- shot_groups: shot_groups
+ shot_groups: shot_groups,
+ shot_record_count: shot_record_count
)
end
diff --git a/lib/cannery_web/live/range_live/index.html.heex b/lib/cannery_web/live/range_live/index.html.heex
index 20f879b1..16f9943f 100644
--- a/lib/cannery_web/live/range_live/index.html.heex
+++ b/lib/cannery_web/live/range_live/index.html.heex
@@ -50,7 +50,7 @@
- <%= if @shot_groups |> Enum.empty?() and @search |> is_nil() do %>
+ <%= if @shot_record_count == 0 do %>
<%= gettext("No shots recorded") %>
<%= display_emoji("😔") %>
diff --git a/priv/gettext/de/LC_MESSAGES/default.po b/priv/gettext/de/LC_MESSAGES/default.po
index 63ce2593..29026466 100644
--- a/priv/gettext/de/LC_MESSAGES/default.po
+++ b/priv/gettext/de/LC_MESSAGES/default.po
@@ -808,7 +808,7 @@ msgstr ""
msgid "%{percentage}%"
msgstr ""
-#: lib/cannery_web/live/range_live/index.ex:152
+#: lib/cannery_web/live/range_live/index.ex:154
#, elixir-autogen, elixir-format, fuzzy
msgid "Rounds shot: %{count}"
msgstr "Patronen abgefeuert"
diff --git a/priv/gettext/default.pot b/priv/gettext/default.pot
index 7a7834f9..0559f319 100644
--- a/priv/gettext/default.pot
+++ b/priv/gettext/default.pot
@@ -802,7 +802,7 @@ msgstr ""
msgid "%{percentage}%"
msgstr ""
-#: lib/cannery_web/live/range_live/index.ex:152
+#: lib/cannery_web/live/range_live/index.ex:154
#, elixir-autogen, elixir-format
msgid "Rounds shot: %{count}"
msgstr ""
diff --git a/priv/gettext/en/LC_MESSAGES/default.po b/priv/gettext/en/LC_MESSAGES/default.po
index 1750d833..e0db64fd 100644
--- a/priv/gettext/en/LC_MESSAGES/default.po
+++ b/priv/gettext/en/LC_MESSAGES/default.po
@@ -802,7 +802,7 @@ msgstr ""
msgid "%{percentage}%"
msgstr ""
-#: lib/cannery_web/live/range_live/index.ex:152
+#: lib/cannery_web/live/range_live/index.ex:154
#, elixir-autogen, elixir-format, fuzzy
msgid "Rounds shot: %{count}"
msgstr ""
diff --git a/priv/gettext/es/LC_MESSAGES/default.po b/priv/gettext/es/LC_MESSAGES/default.po
index 482b4a42..aa352f03 100644
--- a/priv/gettext/es/LC_MESSAGES/default.po
+++ b/priv/gettext/es/LC_MESSAGES/default.po
@@ -810,7 +810,7 @@ msgstr "Mostrar usadas"
msgid "%{percentage}%"
msgstr "%{percentage}%"
-#: lib/cannery_web/live/range_live/index.ex:152
+#: lib/cannery_web/live/range_live/index.ex:154
#, elixir-autogen, elixir-format, fuzzy
msgid "Rounds shot: %{count}"
msgstr "Balas disparadas: %{count}"
diff --git a/priv/gettext/fr/LC_MESSAGES/default.po b/priv/gettext/fr/LC_MESSAGES/default.po
index 32b78e48..ed4aa041 100644
--- a/priv/gettext/fr/LC_MESSAGES/default.po
+++ b/priv/gettext/fr/LC_MESSAGES/default.po
@@ -811,7 +811,7 @@ msgstr ""
msgid "%{percentage}%"
msgstr ""
-#: lib/cannery_web/live/range_live/index.ex:152
+#: lib/cannery_web/live/range_live/index.ex:154
#, elixir-autogen, elixir-format, fuzzy
msgid "Rounds shot: %{count}"
msgstr "Cartouches tirées"
diff --git a/priv/gettext/ga/LC_MESSAGES/default.po b/priv/gettext/ga/LC_MESSAGES/default.po
index 62477de1..9f214003 100644
--- a/priv/gettext/ga/LC_MESSAGES/default.po
+++ b/priv/gettext/ga/LC_MESSAGES/default.po
@@ -804,7 +804,7 @@ msgstr ""
msgid "%{percentage}%"
msgstr ""
-#: lib/cannery_web/live/range_live/index.ex:152
+#: lib/cannery_web/live/range_live/index.ex:154
#, elixir-autogen, elixir-format, fuzzy
msgid "Rounds shot: %{count}"
msgstr ""
diff --git a/test/cannery/activity_log_test.exs b/test/cannery/activity_log_test.exs
index ac371d83..29e0cadb 100644
--- a/test/cannery/activity_log_test.exs
+++ b/test/cannery/activity_log_test.exs
@@ -33,6 +33,26 @@ defmodule Cannery.ActivityLogTest do
]
end
+ test "get_shot_record_count!/1 returns the correct amount of shot records",
+ %{pack: pack, current_user: current_user} do
+ assert ActivityLog.get_shot_record_count!(current_user) == 1
+
+ shot_group_fixture(%{count: 1, date: ~N[2022-02-13 03:17:00]}, current_user, pack)
+ assert ActivityLog.get_shot_record_count!(current_user) == 2
+
+ shot_group_fixture(%{count: 1, date: ~N[2022-02-13 03:17:00]}, current_user, pack)
+ assert ActivityLog.get_shot_record_count!(current_user) == 3
+
+ other_user = user_fixture()
+ assert ActivityLog.get_shot_record_count!(other_user) == 0
+
+ container = container_fixture(other_user)
+ ammo_type = ammo_type_fixture(other_user)
+ {1, [pack]} = pack_fixture(%{count: 25}, ammo_type, container, other_user)
+ shot_group_fixture(%{count: 1, date: ~N[2022-02-13 03:17:00]}, other_user, pack)
+ assert ActivityLog.get_shot_record_count!(other_user) == 1
+ end
+
test "get_shot_group!/2 returns the shot_group with given id",
%{shot_group: shot_group, current_user: current_user} do
assert ActivityLog.get_shot_group!(shot_group.id, current_user) == shot_group