소스 검색

Add a button to trigger all queries

theenglishway (time) 4 년 전
부모
커밋
baa5bd2e98
3개의 변경된 파일32개의 추가작업 그리고 2개의 파일을 삭제
  1. 8 0
      lib/vaccins_web/live/index.ex
  2. 2 0
      lib/vaccins_web/live/index_live.html.leex
  3. 22 2
      lib/vaccins_web/live/location_component.ex

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

@@ -24,6 +24,14 @@ defmodule VaccinsWeb.IndexLive do
     end
   end
 
+  @impl true
+  def handle_event("trigger_all", _, socket = %{assigns: %{locations: locations}}) do
+    locations
+    |> Enum.each(&send_update(VaccinsWeb.LocationComponent, id: &1.id, force_refresh: true))
+
+    {:noreply, socket}
+  end
+
   @impl true
   def handle_info({:query_sent, id, ref}, socket = %{assigns: %{pending: pending}}) do
     {:noreply, socket |> assign(pending: pending |> Map.put(ref, id))}

+ 2 - 0
lib/vaccins_web/live/index_live.html.leex

@@ -14,6 +14,8 @@
   <%= submit "send" %>
 </form>
 
+<button phx-click="trigger_all">Trigger all</button>
+
 <ul>
   <%= for l <- @locations do %>
     <li><%= live_component @socket, VaccinsWeb.LocationComponent, id: l.id, location: l %></li>

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

@@ -5,10 +5,25 @@ defmodule VaccinsWeb.LocationComponent do
   @impl true
   def mount(socket), do: {:ok, socket |> assign(availabilities: [])}
 
+  @impl true
+  def update(assigns, socket) do
+    {force_refresh, assigns} = assigns |> Map.pop(:force_refresh)
+
+    if is_nil(force_refresh) do
+      {:ok, socket |> assign(assigns)}
+    else
+      trigger_and_signal_query(
+        assigns |> Map.get(:location, socket.assigns.location),
+        assigns.id
+      )
+
+      {:ok, socket |> assign(availabilities: "...") |> assign(assigns)}
+    end
+  end
+
   @impl true
   def handle_event("trigger_query", _, socket = %{assigns: %{id: id, location: location}}) do
-    ref = Search.async_trigger_query(location)
-    send(self(), {:query_sent, id, ref})
+    trigger_and_signal_query(location, id)
     {:noreply, socket |> assign(availabilities: "...")}
   end
 
@@ -27,4 +42,9 @@ defmodule VaccinsWeb.LocationComponent do
       </dl>
     """
   end
+
+  defp trigger_and_signal_query(location, id) do
+    ref = Search.async_trigger_query(location)
+    send(self(), {:query_sent, id, ref})
+  end
 end