Просмотр исходного кода

Remove code that handled refresh liveview-side

theenglishway (time) 4 лет назад
Родитель
Сommit
204a03fc89

+ 0 - 5
lib/vaccins/poller.ex

@@ -19,11 +19,6 @@ defmodule Vaccins.Poller do
     {:noreply, state}
   end
 
-  @impl true
-  def handle_info({:query_result, id, res}, state = %{locations: locations}) do
-    {:noreply, state}
-  end
-
   @impl true
   def handle_info({:location_availabilities, id, res}, state = %{locations: locations}) do
     Phoenix.PubSub.broadcast(Vaccins.PubSub, "locations", {:new_availabilities, id, res})

+ 0 - 1
lib/vaccins/search.ex

@@ -10,7 +10,6 @@ defmodule Vaccins.Search do
 
     with {:ok, _} <-
            Task.start(fn ->
-             send(target, {:query_result, ref, l |> Location.query_availability()})
              send(target, {:location_availabilities, l.id, l |> Location.query_availability()})
            end),
          do: ref

+ 1 - 39
lib/vaccins_web/live/index.ex

@@ -50,7 +50,7 @@ defmodule VaccinsWeb.IndexLive do
   end
 
   def handle_event("trigger_all", _, socket) do
-    {:noreply, socket |> trigger_global_refresh}
+    {:noreply, socket}
   end
 
   def handle_event("toggle_form", _, socket = %{assigns: %{display_cs: display}}),
@@ -65,20 +65,6 @@ defmodule VaccinsWeb.IndexLive do
     do: {:noreply, socket |> push_patch(to: Routes.index_path(socket, :index, noob: true))}
 
   @impl true
-  def handle_info({:query_sent, id, ref}, socket = %{assigns: %{pending: pending}}) do
-    {:noreply, socket}
-  end
-
-  def handle_info(:periodic_refresh, socket = %{assigns: %{display_cs: false}}),
-    do:
-      {:noreply,
-       socket
-       |> trigger_global_refresh
-       |> trigger_periodic_refresh}
-
-  def handle_info(:periodic_refresh, socket = %{assigns: %{display_cs: true}}),
-    do: {:noreply, socket |> trigger_periodic_refresh}
-
   def handle_info({:location_has_slots, id, true}, socket) do
     {:noreply,
      socket |> update(:locations_with_early_slots, &(&1 |> MapSet.put(id))) |> set_title}
@@ -100,35 +86,11 @@ defmodule VaccinsWeb.IndexLive do
     do: socket |> push_patch(to: Routes.index_path(socket, :index))
 
   @impl true
-  def handle_info(
-        {:query_result, ref, res},
-        socket = %{assigns: %{locations: valid, pending: pending}}
-      ) do
-    {:noreply, socket}
-  end
-
-  def handle_info({:location_availabilities, id, res}, socket = %{assigns: %{locations: valid}}) do
-    send_update(VaccinsWeb.LocationComponent, id: id, availabilities: res)
-    {:noreply, socket}
-  end
-
   def handle_info({:new_availabilities, id, res}, socket = %{assigns: %{locations: valid}}) do
     send_update(VaccinsWeb.LocationComponent, id: id, availabilities: res)
     {:noreply, socket}
   end
 
-  defp trigger_global_refresh(socket = %{assigns: %{locations: locations}}) do
-    locations
-    |> Enum.each(&send_update(VaccinsWeb.LocationComponent, id: &1.id, force_refresh: true))
-
-    socket
-  end
-
-  defp trigger_periodic_refresh(socket) do
-    Process.send_after(self(), :periodic_refresh, @refresh_period_ms)
-    socket
-  end
-
   defp locations_by_availability(
          assigns = %{
            locations: locations,

+ 5 - 22
lib/vaccins_web/live/location_component.ex

@@ -19,28 +19,17 @@ defmodule VaccinsWeb.LocationComponent do
 
   @impl true
   def update(assigns, socket) do
-    {force_refresh, assigns} = assigns |> Map.pop(:force_refresh)
     assigns = assigns |> integrate_availabilities
 
-    if is_nil(force_refresh) do
-      {:ok, socket |> assign(assigns) |> assign(loading: false) |> signal_availabilities}
-    else
-      trigger_and_signal_query(
-        assigns |> Map.get(:location, socket.assigns.location),
-        assigns.id
-      )
+    socket =
+      if assigns |> Map.has_key?(:availabilities),
+        do: socket |> assign(last_refresh_date: get_refresh_time()),
+        else: socket
 
-      {:ok,
-       socket |> assign(assigns) |> assign(loading: true, last_refresh_date: get_refresh_time())}
-    end
+    {:ok, socket |> assign(assigns) |> signal_availabilities}
   end
 
   @impl true
-  def handle_event("trigger_query", _, socket = %{assigns: %{id: id, location: location}}) do
-    trigger_and_signal_query(location, id)
-    {:noreply, socket |> assign(loading: true, last_refresh_date: get_refresh_time())}
-  end
-
   def handle_event("delete", _, socket = %{assigns: %{id: id}}) do
     if :ok == LocationStore.delete_location(id), do: send(self(), {:location_deleted, id})
     {:noreply, socket}
@@ -119,7 +108,6 @@ defmodule VaccinsWeb.LocationComponent do
   defp render_action_list(assigns),
     do: ~L"""
       <ul class="actions-list">
-        <li><button phx-click="trigger_query" phx-target="<%= @myself %>">Trigger</button></li>
         <li><a href="<%= @location |> to_json_query %>"><button>Debug</button></a></li>
         <li><button class="alert-danger" phx-click="delete" phx-target="<%= @myself %>" data-confirm="Etes-vous sur?">Delete</button></li>
       </ul>
@@ -154,11 +142,6 @@ defmodule VaccinsWeb.LocationComponent do
   defp has_early_slots?(assigns = %{slots_before: before}),
     do: not (before |> Enum.empty?())
 
-  defp trigger_and_signal_query(location, id) do
-    ref = Search.async_trigger_query(location)
-    send(self(), {:query_sent, id, ref})
-  end
-
   defp get_refresh_time() do
     with {:ok, now} <-
            DateTime.utc_now() |> DateTime.shift_zone("Europe/Paris", Tzdata.TimeZoneDatabase),