location_component.ex 5.5 KB

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