index.ex 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. Phoenix.PubSub.subscribe(Vaccins.PubSub, "locations")
  9. {:ok,
  10. socket
  11. |> assign(
  12. noob_mode: false,
  13. is_local?: is_local?,
  14. locations: locations,
  15. locations_with_early_slots: MapSet.new(),
  16. locations_with_slots: MapSet.new(),
  17. pending: %{},
  18. location_cs: LocationStore.LocationRaw.changeset(%{}),
  19. display_cs: false
  20. )
  21. |> set_title()}
  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(params, _url, socket) when params == %{}, do: {:noreply, socket}
  30. def handle_params(%{"noob" => _}, _url, socket),
  31. do: {:noreply, socket |> assign(noob_mode: true)}
  32. @impl true
  33. def handle_event("add_location", %{"location_raw" => params}, socket) do
  34. case params |> LocationStore.add_location() do
  35. :ok -> {:noreply, socket |> push_patch(to: Routes.index_path(socket, :index))}
  36. {:error, cs} -> {:noreply, socket |> assign(location_cs: cs)}
  37. end
  38. end
  39. def handle_event("reload_file", _, socket) do
  40. :ok = LocationStore.reload()
  41. {:noreply, socket |> assign(locations: LocationStore.get_locations())}
  42. end
  43. def handle_event("trigger_all", _, socket) do
  44. {:noreply, socket}
  45. end
  46. def handle_event("toggle_form", _, socket = %{assigns: %{display_cs: display}}),
  47. do: {:noreply, socket |> assign(display_cs: not display)}
  48. def handle_event("trigger_noob_mode", _, socket = %{assigns: %{noob_mode: true}}),
  49. do:
  50. {:noreply,
  51. socket |> assign(noob_mode: false) |> push_patch(to: Routes.index_path(socket, :index))}
  52. def handle_event("trigger_noob_mode", _, socket = %{assigns: %{noob_mode: false}}),
  53. do: {:noreply, socket |> push_patch(to: Routes.index_path(socket, :index, noob: true))}
  54. @impl true
  55. def handle_info({:location_has_slots, id, true}, socket) do
  56. {:noreply,
  57. socket |> update(:locations_with_early_slots, &(&1 |> MapSet.put(id))) |> set_title}
  58. end
  59. def handle_info({:location_has_slots, id, false}, socket) do
  60. {:noreply, socket |> update(:locations_with_slots, &(&1 |> MapSet.put(id))) |> set_title}
  61. end
  62. def handle_info({:location_no_more_slots, id}, socket) do
  63. {:noreply,
  64. socket
  65. |> update(:locations_with_slots, &(&1 |> MapSet.delete(id)))
  66. |> update(:locations_with_early_slots, &(&1 |> MapSet.delete(id)))
  67. |> set_title}
  68. end
  69. def handle_info({:location_deleted, _}, socket),
  70. do: socket |> push_patch(to: Routes.index_path(socket, :index))
  71. @impl true
  72. def handle_info({:new_availabilities, id, res}, socket = %{assigns: %{locations: valid}}) do
  73. send_update(VaccinsWeb.LocationComponent, id: id, availabilities: res)
  74. {:noreply, socket}
  75. end
  76. defp locations_by_availability(
  77. assigns = %{
  78. locations: locations,
  79. locations_with_slots: with_slots,
  80. locations_with_early_slots: with_early_slots
  81. }
  82. ) do
  83. locations
  84. |> Enum.sort(fn e1, e2 ->
  85. e1_has_early_spot? = e1.id in with_early_slots
  86. e1_has_spot? = e1.id in with_slots
  87. e2_has_early_spot? = e2.id in with_early_slots
  88. e2_has_spot? = e2.id in with_slots
  89. res =
  90. cond do
  91. e2_has_early_spot? -> false
  92. e2_has_spot? and not e1_has_early_spot? -> false
  93. e2_has_spot? and e1_has_spot? -> false
  94. true -> true
  95. end
  96. end)
  97. end
  98. end