location_component.ex 5.7 KB

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