location_component.ex 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. )}
  16. @impl true
  17. def update(assigns, socket) do
  18. {force_refresh, assigns} = assigns |> Map.pop(:force_refresh)
  19. assigns = assigns |> integrate_availabilities
  20. if is_nil(force_refresh) do
  21. {:ok, socket |> assign(assigns) |> assign(loading: false) |> signal_availabilities}
  22. else
  23. trigger_and_signal_query(
  24. assigns |> Map.get(:location, socket.assigns.location),
  25. assigns.id
  26. )
  27. {:ok,
  28. socket |> assign(assigns) |> assign(loading: true, last_refresh_date: get_refresh_time())}
  29. end
  30. end
  31. @impl true
  32. def handle_event("trigger_query", _, socket = %{assigns: %{id: id, location: location}}) do
  33. trigger_and_signal_query(location, id)
  34. {:noreply, socket |> assign(loading: true, last_refresh_date: get_refresh_time())}
  35. end
  36. def handle_event("delete", _, socket = %{assigns: %{id: id}}) do
  37. if LocationStore.delete_location(id), do: send(self(), {:location_deleted, id})
  38. {:noreply, socket}
  39. end
  40. @impl true
  41. def render(assigns) do
  42. ~L"""
  43. <dl class="location">
  44. <dt>id</dt>
  45. <dd><%= @location.id %></dd>
  46. <dt>Status (<%= if @last_refresh_date, do: @last_refresh_date |> Time.to_string() %>) </dt>
  47. <dd>
  48. <%= cond do %>
  49. <%= @loading -> %>...
  50. <%= 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 %>
  51. <%= has_early_slots?(assigns) -> %><span class="alert-danger">Des dispos sous 24h !</span>
  52. <%= has_slots?(assigns) -> %>Des dispos !
  53. <% end %>
  54. </dd>
  55. <dt>booking page</dt>
  56. <dd><%= link @location.booking_page, to: @location.booking_page %></dd>
  57. <%= if has_slots?(assigns) do %>
  58. <dt>Avant 24h</dt>
  59. <dd>
  60. <ul class="slots-list"><%= for d <- @slots_before do %><li><%= d |> DateTime.to_string %></li><% end %></ul>
  61. </dd>
  62. <dt>Après 24h</dt>
  63. <dd>
  64. <ul class="slots-list"><%= for d <- @slots_after do %><li><%= d |> DateTime.to_string %></li><% end %></ul>
  65. </dd>
  66. <% end %>
  67. <dt>actions</dt>
  68. <dd>
  69. <ul class="actions-list">
  70. <li><button phx-click="trigger_query" phx-target="<%= @myself %>">Trigger</button></li>
  71. <li><a href="<%= @location |> to_json_query %>"><button>Debug</button></a></li>
  72. <li><button class="alert-danger" phx-click="delete" phx-target="<%= @myself %>" data-confirm="Etes-vous sur?">Delete</button></li>
  73. </ul>
  74. </dd>
  75. </dl>
  76. """
  77. end
  78. defp integrate_availabilities(assigns = %{availabilities: {:error, reason}}),
  79. do:
  80. assigns
  81. |> Map.put(:slots_after, [])
  82. |> Map.put(:slots_before, [])
  83. defp integrate_availabilities(assigns = %{availabilities: {:ok, after_slots}})
  84. when is_list(after_slots),
  85. do:
  86. assigns
  87. |> Map.put(:slots_after, after_slots |> Enum.take(5))
  88. |> Map.put(:slots_before, [])
  89. defp integrate_availabilities(assigns = %{availabilities: {:ok, before_slots, after_slots}})
  90. when is_list(before_slots),
  91. do:
  92. assigns
  93. |> Map.put(:slots_after, after_slots |> Enum.take(5))
  94. |> Map.put(:slots_before, before_slots)
  95. |> Map.put(:last_early_slot_seen, DateTime.utc_now)
  96. defp integrate_availabilities(assigns), do: assigns
  97. defp has_slots?(assigns = %{slots_before: before, slots_after: after_}),
  98. do: not (before |> Enum.empty?() and after_ |> Enum.empty?())
  99. defp has_early_slots?(assigns = %{slots_before: before}),
  100. do: not (before |> Enum.empty?())
  101. defp trigger_and_signal_query(location, id) do
  102. ref = Search.async_trigger_query(location)
  103. send(self(), {:query_sent, id, ref})
  104. end
  105. defp get_refresh_time() do
  106. with {:ok, now} <-
  107. DateTime.utc_now() |> DateTime.shift_zone("Europe/Paris", Tzdata.TimeZoneDatabase),
  108. do: now |> DateTime.to_time() |> Time.truncate(:second)
  109. end
  110. defp signal_availabilities(socket = %{assigns: %{loading: false}}) do
  111. cond do
  112. socket.assigns |> has_slots? ->
  113. send(self(), {:location_has_slots, socket.assigns.id, socket.assigns |> has_early_slots?})
  114. true ->
  115. send(self(), {:location_no_more_slots, socket.assigns.id})
  116. end
  117. socket
  118. end
  119. defp signal_availabilities(socket), do: socket
  120. defp to_json_query(l = %{availability_query: q, provider: provider}),
  121. do: q |> provider.to_url() |> URI.to_string()
  122. end