index.ex 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. defmodule VaccinsWeb.IndexLive do
  2. use VaccinsWeb, :live_view
  3. alias Vaccins.{LocationStore, Search}
  4. @refresh_period_ms 5 * 1000
  5. @impl true
  6. def mount(_params, %{"is_local?" => is_local?}, socket) do
  7. locations = LocationStore.get_locations()
  8. {:ok,
  9. socket
  10. |> assign(
  11. is_local?: is_local?,
  12. locations: locations,
  13. locations_with_early_slots: MapSet.new(),
  14. locations_with_slots: MapSet.new(),
  15. pending: %{},
  16. location_cs: LocationStore.LocationRaw.changeset(%{}),
  17. display_cs: false
  18. )
  19. |> set_title()
  20. |> trigger_global_refresh
  21. |> trigger_periodic_refresh}
  22. end
  23. defp set_title(socket = %{assigns: %{locations_with_early_slots: early}}) do
  24. if early |> Enum.empty?(),
  25. do: socket |> assign(page_title: "Disponibilités vaccins"),
  26. else: socket |> assign(page_title: "(!!) Disponibilités vaccins")
  27. end
  28. @impl true
  29. def handle_params(%{}, _url, socket), do: {:noreply, socket}
  30. @impl true
  31. def handle_event("add_location", %{"location_raw" => params}, socket) do
  32. case params |> LocationStore.add_location() do
  33. :ok -> {:noreply, socket |> push_patch(to: Routes.index_path(socket, :index))}
  34. {:error, cs} -> {:noreply, socket |> assign(location_cs: cs)}
  35. end
  36. end
  37. def handle_event("reload_file", _, socket) do
  38. :ok = LocationStore.reload()
  39. {:noreply, socket |> assign(locations: LocationStore.get_locations())}
  40. end
  41. def handle_event("trigger_all", _, socket) do
  42. {:noreply, socket |> trigger_global_refresh}
  43. end
  44. def handle_event("toggle_form", _, socket = %{assigns: %{display_cs: display}}),
  45. do: {:noreply, socket |> assign(display_cs: not display)}
  46. @impl true
  47. def handle_info({:query_sent, id, ref}, socket = %{assigns: %{pending: pending}}) do
  48. {:noreply, socket |> assign(pending: pending |> Map.put(ref, id))}
  49. end
  50. def handle_info(:periodic_refresh, socket = %{assigns: %{display_cs: false}}),
  51. do:
  52. {:noreply,
  53. socket
  54. |> trigger_global_refresh
  55. |> trigger_periodic_refresh}
  56. def handle_info(:periodic_refresh, socket = %{assigns: %{display_cs: true}}),
  57. do: {:noreply, socket |> trigger_periodic_refresh}
  58. def handle_info({:location_has_slots, id, true}, socket) do
  59. {:noreply,
  60. socket |> update(:locations_with_early_slots, &(&1 |> MapSet.put(id))) |> set_title}
  61. end
  62. def handle_info({:location_has_slots, id, false}, socket) do
  63. {:noreply, socket |> update(:locations_with_slots, &(&1 |> MapSet.put(id))) |> set_title}
  64. end
  65. def handle_info({:location_no_more_slots, id}, socket) do
  66. {:noreply,
  67. socket
  68. |> update(:locations_with_slots, &(&1 |> MapSet.delete(id)))
  69. |> update(:locations_with_early_slots, &(&1 |> MapSet.delete(id)))
  70. |> set_title}
  71. end
  72. def handle_info({:location_deleted, _}, socket),
  73. do: socket |> push_patch(to: Routes.index_path(socket, :index))
  74. @impl true
  75. def handle_info(
  76. {:query_result, ref, res},
  77. socket = %{assigns: %{locations: valid, pending: pending}}
  78. ) do
  79. id = pending |> Map.get(ref)
  80. send_update(VaccinsWeb.LocationComponent, id: id, availabilities: res)
  81. {:noreply, socket |> assign(pending: pending |> Map.delete(ref))}
  82. end
  83. defp trigger_global_refresh(socket = %{assigns: %{locations: locations}}) do
  84. locations
  85. |> Enum.each(&send_update(VaccinsWeb.LocationComponent, id: &1.id, force_refresh: true))
  86. socket
  87. end
  88. defp trigger_periodic_refresh(socket) do
  89. Process.send_after(self(), :periodic_refresh, @refresh_period_ms)
  90. socket
  91. end
  92. defp locations_by_availability(
  93. assigns = %{
  94. locations: locations,
  95. locations_with_slots: with_slots,
  96. locations_with_early_slots: with_early_slots
  97. }
  98. ) do
  99. locations
  100. |> Enum.sort(fn e1, e2 ->
  101. e1_has_early_spot? = e1.id in with_early_slots
  102. e1_has_spot? = e1.id in with_slots
  103. e2_has_early_spot? = e2.id in with_early_slots
  104. e2_has_spot? = e2.id in with_slots
  105. res =
  106. cond do
  107. e2_has_early_spot? -> false
  108. e2_has_spot? and not e1_has_early_spot? -> false
  109. e2_has_spot? and e1_has_spot? -> false
  110. true -> true
  111. end
  112. end)
  113. end
  114. end