Преглед изворни кода

Allow filtering zones from query string

theenglishway (time) пре 4 година
родитељ
комит
57f277c93b
2 измењених фајлова са 19 додато и 1 уклоњено
  1. 18 0
      lib/vaccins_web/live/index.ex
  2. 1 1
      lib/vaccins_web/live/index_live.html.leex

+ 18 - 0
lib/vaccins_web/live/index.ex

@@ -19,6 +19,7 @@ defmodule VaccinsWeb.IndexLive do
        location_cs: LocationStore.LocationRaw.changeset(%{}),
        display_cs: false
      )
+     |> init_area_filters()
      |> set_title()}
   end
 
@@ -34,6 +35,13 @@ defmodule VaccinsWeb.IndexLive do
   def handle_params(%{"noob" => _}, _url, socket),
     do: {:noreply, socket |> assign(noob_mode: true)}
 
+  def handle_params(%{"geographic_areas" => areas}, _url, socket) when is_list(areas) do
+    all = get_all_areas(socket)
+    areas |> MapSet.new() |> MapSet.intersection(all)
+
+    {:noreply, socket |> assign(area_filters: areas)}
+  end
+
   @impl true
   def handle_event("add_location", %{"location_raw" => params}, socket) do
     case params |> LocationStore.add_location() do
@@ -85,6 +93,11 @@ defmodule VaccinsWeb.IndexLive do
     {:noreply, socket}
   end
 
+  defp filter(locations, assigns = %{area_filters: filters}),
+    do:
+      locations
+      |> Enum.filter(&(&1.geographic_area in filters))
+
   defp locations_by_availability(%{
          locations: locations,
          locations_with_slots: with_slots,
@@ -105,4 +118,9 @@ defmodule VaccinsWeb.IndexLive do
       end
     end)
   end
+
+  defp init_area_filters(socket), do: socket |> assign(area_filters: socket |> get_all_areas)
+
+  defp get_all_areas(%{assigns: %{locations: locations}}),
+    do: locations |> Enum.map(& &1.geographic_area) |> MapSet.new()
 end

+ 1 - 1
lib/vaccins_web/live/index_live.html.leex

@@ -65,7 +65,7 @@
     <tr><%= VaccinsWeb.LocationComponent.render_table_header(@is_local?) %></tr>
   </thead>
   <tbody>
-    <%= for l <- assigns |> locations_by_availability do %>
+    <%= for l <- assigns |> locations_by_availability |> filter(assigns) do %>
       <tr><%= live_component @socket, VaccinsWeb.LocationComponent, id: l.id, location: l, render_as: :table_row, is_local?: @is_local? %></tr>
     <% end %>
   </tbody>