location_component.ex 6.0 KB

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