location_component.ex 4.9 KB

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