Parcourir la source

Add locations_displayed assign

theenglishway (time) il y a 4 ans
Parent
commit
79f365360b
2 fichiers modifiés avec 22 ajouts et 13 suppressions
  1. 21 12
      lib/vaccins_web/live/index.ex
  2. 1 1
      lib/vaccins_web/live/index_live.html.leex

+ 21 - 12
lib/vaccins_web/live/index.ex

@@ -13,6 +13,7 @@ defmodule VaccinsWeb.IndexLive do
        noob_mode: false,
        is_local?: is_local?,
        all_locations: all_locations,
+       locations_displayed: all_locations,
        locations_with_early_slots: MapSet.new(),
        locations_with_slots: MapSet.new(),
        pending: %{},
@@ -35,7 +36,8 @@ defmodule VaccinsWeb.IndexLive do
     do:
       {:noreply,
        socket
-       |> assign(noob_mode: false, area_filters: socket.assigns.all_areas, params: params)}
+       |> assign(noob_mode: false, params: params)
+       |> update_area_filters(socket.assigns.all_areas)}
 
   def handle_params(params, _url, socket)
       when is_map_key(params, "noob") or is_map_key(params, "geographic_areas") do
@@ -46,9 +48,9 @@ defmodule VaccinsWeb.IndexLive do
      socket
      |> assign(
        noob_mode: params |> Map.has_key?("noob"),
-       area_filters: areas |> MapSet.new() |> MapSet.intersection(all_areas),
        params: params
-     )}
+     )
+     |> update_area_filters(areas |> MapSet.new() |> MapSet.intersection(all_areas))}
   end
 
   @impl true
@@ -108,21 +110,18 @@ defmodule VaccinsWeb.IndexLive do
 
   @impl true
   def handle_info({:new_availabilities, id, res}, socket) do
-    send_update(VaccinsWeb.LocationComponent, id: id, availabilities: res)
+    if id in (socket.assigns.locations_displayed |> Enum.map(& &1.id)),
+      do: send_update(VaccinsWeb.LocationComponent, id: id, availabilities: res)
+
     {:noreply, socket}
   end
 
-  defp filter(locations, %{area_filters: filters}),
-    do:
-      locations
-      |> Enum.filter(&(&1.geographic_area in filters))
-
   defp locations_by_availability(%{
-         all_locations: all_locations,
+         locations_displayed: locations_displayed,
          locations_with_slots: with_slots,
          locations_with_early_slots: with_early_slots
        }) do
-    all_locations
+    locations_displayed
     |> Enum.sort(fn e1, e2 ->
       e1_has_early_spot? = e1.id in with_early_slots
       e1_has_spot? = e1.id in with_slots
@@ -140,7 +139,17 @@ defmodule VaccinsWeb.IndexLive do
 
   defp set_all_areas(socket), do: socket |> assign(all_areas: socket |> get_all_areas)
 
-  defp init_area_filters(socket), do: socket |> assign(area_filters: socket.assigns.all_areas)
+  defp init_area_filters(socket), do: socket |> update_area_filters(socket.assigns.all_areas)
+
+  defp update_area_filters(socket, filter_value),
+    do:
+      socket
+      |> assign(
+        area_filters: filter_value,
+        locations_displayed:
+          socket.assigns.all_locations
+          |> Enum.filter(&(&1.geographic_area in filter_value))
+      )
 
   defp get_all_areas(%{assigns: %{all_locations: all_locations}}),
     do: all_locations |> Enum.map(& &1.geographic_area) |> MapSet.new()

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

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