index.ex 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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("toggle_form", _, socket = %{assigns: %{display_cs: display}}),
  44. do: {:noreply, socket |> assign(display_cs: not display)}
  45. def handle_event("trigger_noob_mode", _, socket = %{assigns: %{noob_mode: true}}),
  46. do:
  47. {:noreply,
  48. socket |> assign(noob_mode: false) |> push_patch(to: Routes.index_path(socket, :index))}
  49. def handle_event("trigger_noob_mode", _, socket = %{assigns: %{noob_mode: false}}),
  50. do: {:noreply, socket |> push_patch(to: Routes.index_path(socket, :index, noob: true))}
  51. @impl true
  52. def handle_info({:location_has_slots, id, true}, socket) do
  53. {:noreply,
  54. socket |> update(:locations_with_early_slots, &(&1 |> MapSet.put(id))) |> set_title}
  55. end
  56. def handle_info({:location_has_slots, id, false}, socket) do
  57. {:noreply, socket |> update(:locations_with_slots, &(&1 |> MapSet.put(id))) |> set_title}
  58. end
  59. def handle_info({:location_no_more_slots, id}, socket) do
  60. {:noreply,
  61. socket
  62. |> update(:locations_with_slots, &(&1 |> MapSet.delete(id)))
  63. |> update(:locations_with_early_slots, &(&1 |> MapSet.delete(id)))
  64. |> set_title}
  65. end
  66. def handle_info({:location_deleted, _}, socket),
  67. do: socket |> push_patch(to: Routes.index_path(socket, :index))
  68. @impl true
  69. def handle_info({:new_availabilities, id, res}, socket = %{assigns: %{locations: valid}}) do
  70. send_update(VaccinsWeb.LocationComponent, id: id, availabilities: res)
  71. {:noreply, socket}
  72. end
  73. defp locations_by_availability(
  74. assigns = %{
  75. locations: locations,
  76. locations_with_slots: with_slots,
  77. locations_with_early_slots: with_early_slots
  78. }
  79. ) do
  80. locations
  81. |> Enum.sort(fn e1, e2 ->
  82. e1_has_early_spot? = e1.id in with_early_slots
  83. e1_has_spot? = e1.id in with_slots
  84. e2_has_early_spot? = e2.id in with_early_slots
  85. e2_has_spot? = e2.id in with_slots
  86. res =
  87. cond do
  88. e2_has_early_spot? -> false
  89. e2_has_spot? and not e1_has_early_spot? -> false
  90. e2_has_spot? and e1_has_spot? -> false
  91. true -> true
  92. end
  93. end)
  94. end
  95. end