location_component.ex 4.1 KB

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