Selaa lähdekoodia

Display latest refresh time

theenglishway (time) 4 vuotta sitten
vanhempi
commit
b018be5b31
1 muutettua tiedostoa jossa 12 lisäystä ja 4 poistoa
  1. 12 4
      lib/vaccins_web/live/location_component.ex

+ 12 - 4
lib/vaccins_web/live/location_component.ex

@@ -11,7 +11,8 @@ defmodule VaccinsWeb.LocationComponent do
          has_availabilities: false,
          slots_after: [],
          slots_before: [],
-         loading: false
+         loading: false,
+         last_refresh_date: nil
        )}
 
   @impl true
@@ -27,14 +28,15 @@ defmodule VaccinsWeb.LocationComponent do
         assigns.id
       )
 
-      {:ok, socket |> assign(assigns) |> assign(loading: true)}
+      {:ok,
+       socket |> assign(assigns) |> assign(loading: true, last_refresh_date: get_refresh_time())}
     end
   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)}
+    {:noreply, socket |> assign(loading: true, last_refresh_date: get_refresh_time())}
   end
 
   @impl true
@@ -43,7 +45,7 @@ defmodule VaccinsWeb.LocationComponent do
       <dl class="location">
         <dt>id</dt>
         <dd><%= @location.id %></dd>
-        <dt>Status</dt>
+        <dt>Status (<%= if @last_refresh_date, do: @last_refresh_date |> Time.to_string() %>) </dt>
         <dd>
           <%= cond do %>
             <%= @loading -> %>...
@@ -96,4 +98,10 @@ defmodule VaccinsWeb.LocationComponent 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),
+         do: now |> DateTime.to_time() |> Time.truncate(:second)
+  end
 end