location_component.ex 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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.location %></td>
  60. <td><%= if @last_refresh_date, do: @last_refresh_date |> Time.to_string() %></td>
  61. <td><%= render_status(assigns) %></td>
  62. <td><%= link "Résa.", to: @location.booking_page %></td>
  63. <td><%= render_slots_before(assigns) %></td>
  64. <td><%= render_slots_after(assigns) %></td>
  65. <%= if @is_local? do %><td><%= render_action_list(assigns) %></td><% end %>
  66. """
  67. end
  68. def render_table_header(is_local?),
  69. do: ~E"""
  70. <th>Nom</th>
  71. <th>Lieu</th>
  72. <th>Dernier refresh</th>
  73. <th>Status</th>
  74. <th>Lien résa</th>
  75. <th>Slots avant 24h</th>
  76. <th>Slots après 24h</th>
  77. <%= if is_local? do %><th>Actions</th><% end %>
  78. """
  79. defp render_status(assigns),
  80. do: ~L"""
  81. <%= cond do %>
  82. <%= @loading -> %>...
  83. <%= 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 %>
  84. <%= has_early_slots?(assigns) -> %><span class="alert-danger">Des dispos sous 24h !</span>
  85. <%= has_slots?(assigns) -> %>Des dispos !
  86. <% end %>
  87. """
  88. defp render_slots_before(assigns),
  89. do: ~L"""
  90. <ul class="slots-list"><%= for d <- @slots_before do %><li><%= d |> Calendar.strftime("%d/%m/%Y %H:%M") %></li><% end %></ul>
  91. """
  92. defp render_slots_after(assigns),
  93. do: ~L"""
  94. <ul class="slots-list"><%= for d <- @slots_after do %><li><%= d |> Calendar.strftime("%d/%m/%Y %H:%M") %></li><% end %></ul>
  95. """
  96. defp render_action_list(assigns),
  97. do: ~L"""
  98. <ul class="actions-list">
  99. <li><a href="<%= @location |> to_json_query %>"><button>Debug</button></a></li>
  100. <li><button class="alert-danger" phx-click="delete" phx-target="<%= @myself %>" data-confirm="Etes-vous sur?">Delete</button></li>
  101. </ul>
  102. """
  103. defp integrate_availabilities(assigns = %{availabilities: {:error, reason}}),
  104. do:
  105. assigns
  106. |> Map.put(:slots_after, [])
  107. |> Map.put(:slots_before, [])
  108. defp integrate_availabilities(assigns = %{availabilities: {:ok, after_slots}})
  109. when is_list(after_slots),
  110. do:
  111. assigns
  112. |> Map.put(:slots_after, after_slots |> Enum.take(2))
  113. |> Map.put(:slots_before, [])
  114. defp integrate_availabilities(assigns = %{availabilities: {:ok, before_slots, after_slots}})
  115. when is_list(before_slots),
  116. do:
  117. assigns
  118. |> Map.put(:slots_after, after_slots |> Enum.take(2))
  119. |> Map.put(:slots_before, before_slots |> Enum.take(2))
  120. |> Map.put(:last_early_slot_seen, DateTime.utc_now())
  121. defp integrate_availabilities(assigns), do: assigns
  122. defp has_slots?(assigns = %{slots_before: before, slots_after: after_}),
  123. do: not (before |> Enum.empty?() and after_ |> Enum.empty?())
  124. defp has_early_slots?(assigns = %{slots_before: before}),
  125. do: not (before |> Enum.empty?())
  126. defp get_refresh_time() do
  127. with {:ok, now} <-
  128. DateTime.utc_now() |> DateTime.shift_zone("Europe/Paris", Tzdata.TimeZoneDatabase),
  129. do: now |> DateTime.to_time() |> Time.truncate(:second)
  130. end
  131. defp signal_availabilities(socket = %{assigns: %{loading: false}}) do
  132. cond do
  133. socket.assigns |> has_slots? ->
  134. send(self(), {:location_has_slots, socket.assigns.id, socket.assigns |> has_early_slots?})
  135. true ->
  136. send(self(), {:location_no_more_slots, socket.assigns.id})
  137. end
  138. socket
  139. end
  140. defp signal_availabilities(socket), do: socket
  141. defp to_json_query(l = %{availability_query: q, provider: provider}),
  142. do: q |> provider.to_url() |> URI.to_string()
  143. end