@@ -150,6 +163,8 @@ defmodule PolarWeb.RootLive do
end
def mount(_params, _session, socket) do
+ tabs = load_tabs()
+
socket =
socket
|> assign(:versions, [])
@@ -158,50 +173,20 @@ defmodule PolarWeb.RootLive do
|> assign(:arch_colors, @arch_colors)
|> assign(:os_colors, @os_colors)
|> assign(:filter, %{})
+ |> assign(:tabs, tabs)
{:ok, socket}
end
- def handle_params(%{"os" => os} = params, _uri, socket) do
- versions =
- from(
- v in Version,
- join: p in assoc(v, :product),
- where: v.current_state == ^"active" and p.os == ^os,
- preload: [{:product, p}],
- order_by: [asc: p.os],
- order_by: [desc: p.release],
- order_by: [desc: v.inserted_at]
- )
- |> Repo.all()
-
+ def handle_params(params, _uri, socket) do
filter = Map.take(params, ["os"])
- socket =
- socket
- |> assign(:versions, versions)
- |> assign(:filter, filter)
-
- {:noreply, socket}
- end
-
- def handle_params(_, _uri, socket) do
- versions =
- from(
- v in Version,
- where: v.current_state == ^"active",
- join: p in assoc(v, :product),
- preload: [{:product, p}],
- order_by: [asc: p.os],
- order_by: [desc: p.release],
- order_by: [desc: v.inserted_at]
- )
- |> Repo.all()
+ versions = load_versions(filter)
socket =
socket
|> assign(:versions, versions)
- |> assign(:filter, %{})
+ |> assign(:filter, filter)
{:noreply, socket}
end
diff --git a/lib/polar_web/live/root_live/data_loader.ex b/lib/polar_web/live/root_live/data_loader.ex
new file mode 100644
index 0000000..dfdfa6a
--- /dev/null
+++ b/lib/polar_web/live/root_live/data_loader.ex
@@ -0,0 +1,43 @@
+defmodule PolarWeb.RootLive.DataLoader do
+ alias Polar.Repo
+ alias Polar.Streams.Product
+ alias Polar.Streams.Version
+
+ import Ecto.Query, only: [from: 2]
+
+ def load_tabs do
+ from(
+ p in Product,
+ select: %{os: p.os, count: count(p.id)},
+ group_by: [:os],
+ order_by: [:os]
+ )
+ |> Repo.all()
+ |> Enum.with_index(1)
+ end
+
+ def load_versions(filter) do
+ query =
+ from(
+ v in Version,
+ join: p in assoc(v, :product),
+ preload: [{:product, p}],
+ where: v.current_state == ^"active",
+ order_by: [asc: p.os],
+ order_by: [desc: p.release],
+ order_by: [desc: v.inserted_at]
+ )
+
+ Enum.reduce(filter, query, &filter/2)
+ |> Repo.all()
+ end
+
+ defp filter({"os", os}, queryable) do
+ from(v in queryable,
+ join: p in assoc(v, :product),
+ where: p.os == ^os
+ )
+ end
+
+ defp filter(_, queryable), do: queryable
+end