Only show subsonic filter when subsonic products exist for caliber

This commit is contained in:
Graham McIntire 2026-03-12 09:01:16 -05:00
parent cd8c080846
commit d7c4772ad8
No known key found for this signature in database
3 changed files with 25 additions and 3 deletions

View file

@ -103,6 +103,12 @@ defmodule Ammoprices.Catalog do
|> Repo.all()
end
def has_subsonic_products?(caliber_id) do
Product
|> where([p], p.caliber_id == ^caliber_id and p.subsonic == true)
|> Repo.exists?()
end
def distinct_casings_for_caliber(caliber_id) do
Product
|> where([p], p.caliber_id == ^caliber_id)

View file

@ -119,7 +119,8 @@ defmodule AmmopricesWeb.CaliberLive.Show do
assign(socket,
available_casings: Catalog.distinct_casings_for_caliber(caliber_id),
available_grain_weights: Catalog.distinct_grain_weights_for_caliber(caliber_id)
available_grain_weights: Catalog.distinct_grain_weights_for_caliber(caliber_id),
has_subsonic_products: Catalog.has_subsonic_products?(caliber_id)
)
end
@ -310,6 +311,7 @@ defmodule AmmopricesWeb.CaliberLive.Show do
<%!-- Subsonic toggle --%>
<button
:if={@has_subsonic_products}
id="filter-subsonic"
phx-click="toggle_subsonic_filter"
class={[

View file

@ -118,9 +118,13 @@ defmodule AmmopricesWeb.CaliberLive.ShowTest do
assert has_element?(view, "#filter-grain-147")
end
test "renders subsonic filter toggle", %{conn: conn, caliber: caliber, retailer: retailer} do
test "renders subsonic filter toggle when subsonic products exist", %{
conn: conn,
caliber: caliber,
retailer: retailer
} do
now = DateTime.truncate(DateTime.utc_now(), :second)
product = product_fixture(%{caliber: caliber, retailer: retailer, in_stock: true})
product = product_fixture(%{caliber: caliber, retailer: retailer, in_stock: true, subsonic: true})
snapshot_fixture(%{product: product, price_per_round_cents: 25, in_stock: true, recorded_at: now})
{:ok, view, _html} = live(conn, "/calibers/#{caliber.slug}")
@ -128,6 +132,16 @@ defmodule AmmopricesWeb.CaliberLive.ShowTest do
assert has_element?(view, "#filter-subsonic")
end
test "hides subsonic filter when no subsonic products", %{conn: conn, caliber: caliber, retailer: retailer} do
now = DateTime.truncate(DateTime.utc_now(), :second)
product = product_fixture(%{caliber: caliber, retailer: retailer, in_stock: true, subsonic: false})
snapshot_fixture(%{product: product, price_per_round_cents: 25, in_stock: true, recorded_at: now})
{:ok, view, _html} = live(conn, "/calibers/#{caliber.slug}")
refute has_element?(view, "#filter-subsonic")
end
test "displays grain weight and brand in product table", %{conn: conn, caliber: caliber, retailer: retailer} do
now = DateTime.truncate(DateTime.utc_now(), :second)