location_component.ex 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. defmodule VaccinsWeb.LocationComponent do
  2. use VaccinsWeb, :live_component
  3. alias Vaccins.{LocationStore, Search}
  4. @impl true
  5. def mount(socket),
  6. do:
  7. {:ok,
  8. socket
  9. |> assign(
  10. slots_after: [],
  11. slots_before: [],
  12. loading: false,
  13. last_refresh_date: nil,
  14. last_early_slot_seen: nil,
  15. render_as: :description_list,
  16. is_local?: false
  17. )}
  18. @impl true
  19. def update(assigns, socket) do
  20. {force_refresh, assigns} = assigns |> Map.pop(:force_refresh)
  21. assigns = assigns |> integrate_availabilities
  22. if is_nil(force_refresh) do
  23. {:ok, socket |> assign(assigns) |> assign(loading: false) |> signal_availabilities}
  24. else
  25. trigger_and_signal_query(
  26. assigns |> Map.get(:location, socket.assigns.location),
  27. assigns.id
  28. )
  29. {:ok,
  30. socket |> assign(assigns) |> assign(loading: true, last_refresh_date: get_refresh_time())}
  31. end
  32. end
  33. @impl true
  34. def handle_event("trigger_query", _, socket = %{assigns: %{id: id, location: location}}) do
  35. trigger_and_signal_query(location, id)
  36. {:noreply, socket |> assign(loading: true, last_refresh_date: get_refresh_time())}
  37. end
  38. def handle_event("delete", _, socket = %{assigns: %{id: id}}) do
  39. if :ok == LocationStore.delete_location(id), do: send(self(), {:location_deleted, id})
  40. {:noreply, socket}
  41. end
  42. @impl true
  43. def render(assigns = %{render_as: :description_list}) do
  44. ~L"""
  45. <dl class="location">
  46. <dt>name</dt>
  47. <dd><%= @location.name %></dd>
  48. <dt>location</dt>
  49. <dd><%= @location.location %></dd>
  50. <dt>Status (<%= if @last_refresh_date, do: @last_refresh_date |> Time.to_string() %>) </dt>
  51. <dd><%= render_status(assigns) %></dd>
  52. <dt>booking page</dt>
  53. <dd><%= link @location.booking_page, to: @location.booking_page %></dd>
  54. <%= if has_slots?(assigns) do %>
  55. <dt>Avant 24h</dt>
  56. <dd><%= render_slots_before(assigns) %></dd>
  57. <dt>Après 24h</dt>
  58. <dd><%= render_slots_after(assigns) %></dd>
  59. <% end %>
  60. <dt>actions</dt>
  61. <dd><%= render_action_list(assigns) %></dd>
  62. </dl>
  63. """
  64. end
  65. @impl true
  66. def render(assigns = %{render_as: :table_row}) do
  67. ~L"""
  68. <td><%= @location.name %></td>
  69. <td><%= @location.location %></td>
  70. <td><%= if @last_refresh_date, do: @last_refresh_date |> Time.to_string() %></td>
  71. <td><%= render_status(assigns) %></td>
  72. <td><%= link "Résa.", to: @location.booking_page %></td>
  73. <td><%= render_slots_before(assigns) %></td>
  74. <td><%= render_slots_after(assigns) %></td>
  75. <%= if @is_local? do %><td><%= render_action_list(assigns) %></td><% end %>
  76. """
  77. end
  78. def render_table_header(is_local?),
  79. do: ~E"""
  80. <th>Nom</th>
  81. <th>Lieu</th>
  82. <th>Dernier refresh</th>
  83. <th>Status</th>
  84. <th>Lien résa</th>
  85. <th>Slots avant 24h</th>
  86. <th>Slots après 24h</th>
  87. <%= if is_local? do %><th>Actions</th><% end %>
  88. """
  89. defp render_status(assigns),
  90. do: ~L"""
  91. <%= cond do %>
  92. <%= @loading -> %>...
  93. <%= not has_slots?(assigns) -> %>Pas de créneau <%= if @last_early_slot_seen do %>(<%= @last_early_slot_seen |> DateTime.to_time() |> Time.truncate(:second) |> Time.to_string %>)<% end %>
  94. <%= has_early_slots?(assigns) -> %><span class="alert-danger">Des dispos sous 24h !</span>
  95. <%= has_slots?(assigns) -> %>Des dispos !
  96. <% end %>
  97. """
  98. defp render_slots_before(assigns),
  99. do: ~L"""
  100. <ul class="slots-list"><%= for d <- @slots_before do %><li><%= d |> Calendar.strftime("%d/%m/%Y %H:%M") %></li><% end %></ul>
  101. """
  102. defp render_slots_after(assigns),
  103. do: ~L"""
  104. <ul class="slots-list"><%= for d <- @slots_after do %><li><%= d |> Calendar.strftime("%d/%m/%Y %H:%M") %></li><% end %></ul>
  105. """
  106. defp render_action_list(assigns),
  107. do: ~L"""
  108. <ul class="actions-list">
  109. <li><button phx-click="trigger_query" phx-target="<%= @myself %>">Trigger</button></li>
  110. <li><a href="<%= @location |> to_json_query %>"><button>Debug</button></a></li>
  111. <li><button class="alert-danger" phx-click="delete" phx-target="<%= @myself %>" data-confirm="Etes-vous sur?">Delete</button></li>
  112. </ul>
  113. """
  114. defp integrate_availabilities(assigns = %{availabilities: {:error, reason}}),
  115. do:
  116. assigns
  117. |> Map.put(:slots_after, [])
  118. |> Map.put(:slots_before, [])
  119. defp integrate_availabilities(assigns = %{availabilities: {:ok, after_slots}})
  120. when is_list(after_slots),
  121. do:
  122. assigns
  123. |> Map.put(:slots_after, after_slots |> Enum.take(2))
  124. |> Map.put(:slots_before, [])
  125. defp integrate_availabilities(assigns = %{availabilities: {:ok, before_slots, after_slots}})
  126. when is_list(before_slots),
  127. do:
  128. assigns
  129. |> Map.put(:slots_after, after_slots |> Enum.take(2))
  130. |> Map.put(:slots_before, before_slots |> Enum.take(2))
  131. |> Map.put(:last_early_slot_seen, DateTime.utc_now())
  132. defp integrate_availabilities(assigns), do: assigns
  133. defp has_slots?(assigns = %{slots_before: before, slots_after: after_}),
  134. do: not (before |> Enum.empty?() and after_ |> Enum.empty?())
  135. defp has_early_slots?(assigns = %{slots_before: before}),
  136. do: not (before |> Enum.empty?())
  137. defp trigger_and_signal_query(location, id) do
  138. ref = Search.async_trigger_query(location)
  139. send(self(), {:query_sent, id, ref})
  140. end
  141. defp get_refresh_time() do
  142. with {:ok, now} <-
  143. DateTime.utc_now() |> DateTime.shift_zone("Europe/Paris", Tzdata.TimeZoneDatabase),
  144. do: now |> DateTime.to_time() |> Time.truncate(:second)
  145. end
  146. defp signal_availabilities(socket = %{assigns: %{loading: false}}) do
  147. cond do
  148. socket.assigns |> has_slots? ->
  149. send(self(), {:location_has_slots, socket.assigns.id, socket.assigns |> has_early_slots?})
  150. true ->
  151. send(self(), {:location_no_more_slots, socket.assigns.id})
  152. end
  153. socket
  154. end
  155. defp signal_availabilities(socket), do: socket
  156. defp to_json_query(l = %{availability_query: q, provider: provider}),
  157. do: q |> provider.to_url() |> URI.to_string()
  158. end